2014-10-07 116 views
0

我目前有这些PHP页面可以让我添加一条记录到数据库。 (在这种情况下,它的成员)它可以完美地工作,因为我可以添加,删除和查看。但林不知道如何让编辑(或更新功能的工作编辑记录PHP表格

这里是我的数据库连接代码:

<?php 

// Server Info 
$server = 'localhost'; 
$username = 'root'; 
$password = ''; 
$database = 'gamgam'; 

// Connect to database 
$connection = new mysqli($server, $username, $password, $database); 

?> 

这是我添加代码:

<!DOCTYPE html> 
<html> 
<head><title>Insert Users</title></head> 
<body> 
<h2>Insert User Confirmation</h2> 

<form action="<?php $_SERVER['PHP_SELF']?>" method="post"/> <br> 
<?php 

require_once('connection.php'); 

echo "<label for='memberID' >Member ID:</label>"; 
echo "<input type='text' name='memberID' id='memberID' />"; 
echo "<br /><br />"; 

echo "<label for='username' >Username:</label>"; 
echo "<input type='text' name='username' id='username' />"; 
echo "<br /><br />"; 

echo "<label for='password' >Password:</label>"; 
echo "<input type='password' name='password' id='password' />"; 
echo "<br /><br />"; 

echo "<label for='fName' >Firstname:</label>"; 
echo "<input type='text' name='fName' id='fName' />"; 
echo "<br /><br />"; 

echo "<label for='lName' >Lastname:</label>"; 
echo "<input type='text' name='lName' id='lName' />"; 
echo "<br /><br />"; 

echo "<label for='address' >Address:</label>"; 
echo "<input type='text' name='address' id='address' />"; 
echo "<br /><br />"; 

echo "<label for='email' >Email:</label>"; 
echo "<input type='text' name='email' id='email' />"; 
echo "<br /><br />"; 

echo "<input type='submit' name='submit' value='Submit' />"; 
echo "<input type='reset' value='Clear' />"; 
echo "<br /><br />"; 
?> 
</form> 
</section> 

<p><a href='login.php'>Login</a></p>  

<?php 
if(!isset($_POST['submit'])) { 
    echo 'Please Register'; 
} else { 

    $memberID = $_POST['memberID']; 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $fName = $_POST['fName']; 
    $lName = $_POST['lName']; 
    $address = $_POST['address']; 
    $email = $_POST['email']; 


    $query = "INSERT INTO `members` 
        (MemberID, Username, Password, FirstName, LastName, 
        StreetAddress, Email) 
       VALUES ('$memberID', '$username', '$password', '$fName', 
         '$lName', '$address', '$email')"; 
     mysqli_query($connection, $query) 
     or die(mysqli_error($connection)); 

     $rc = mysqli_affected_rows($connection); 
     if ($rc==1) 
     { 
      echo '<h4>The database has been updated with the following details: </h4> '; 
      echo 'MemberID: '.$memberID.'<br />'; 
      echo 'Username: '.$username.'<br />'; 
      echo 'Password: '.$password.'<br />'; 
      echo 'First Name: '.$fName.'<br />'; 
      echo 'Last Name: '.$lName.'<br />'; 
      echo 'Address: '.$address.'<br />'; 
      echo 'Email: '.$email.'<br />'; 
     } else {  
      echo '<p>The data was not entered into the database this time.</p>'; 
     } 
    } 
?> 
</body> 
</html> 

这是我的看法代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <title>View Records</title> 
</head> 
<body> 

<table border="1" style="width:100%" > 

<?php 
/* 
    VIEW.PHP 
    Displays all data from 'players' table 
*/ 

    // connect to the database 
    include('connection.php'); 

    // get results from database 
    $result = mysqli_query($connection, "SELECT * FROM members") 
     or die(mysqli_error()); 

    // loop through results of database query, displaying them in the table 
    while($row = mysqli_fetch_array($result)) { 

     // echo out the contents of each row into a table 
     echo "<tr>"; 
     echo '<td>' . $row['MemberID'] . '</td>'; 
     echo '<td>' . $row['Username'] . '</td>'; 
     echo '<td>' . $row['Password'] . '</td>'; 
     echo '<td>' . $row['FirstName'] . '</td>'; 
     echo '<td>' . $row['StreetAddress'] . '</td>'; 
     echo '<td>' . $row['Email'] . '</td>'; 

     echo '<td><a href="edit.php?MemberID=' . $row['MemberID'] . '">Edit</a></td>'; 
     echo '<td><a href="delete.php?MemberID=' . $row['MemberID'] . '">Delete</a></td>'; 
     echo "</tr>"; 
    } 

    // close table> 
    echo "</table>"; 
?> 
<p><a href="insert_user.php">Add a new record</a></p> 

</body> 
</html> 

这里是删除代码:

<?php 

    // Connect to the database 
    include('connection.php'); 

    // Confirm that the 'code' variable has been set 
    if (isset($_GET['MemberID'])) 
    { 
     // Get the 'MemberID' variable from the URL 
     $MemberID = $_GET['MemberID']; 

     // Delete record from database 
     if ($stmt = $connection->prepare("DELETE FROM members WHERE MemberID = ? LIMIT 1")) { 
      $stmt->bind_param("i",$MemberID); 
      $stmt->execute(); 
      $stmt->close(); 

     } else { 
      echo "ERROR: could not prepare SQL statement."; 
     } 
     $connection->close(); 

     // Redirect user after delete is successful 
     header("Location: view.php"); 
    } else { 
    // If the 'code' variable isn't set, redirect the user 
     header("Location: view.php"); 
    } 

?> 

我已经经历了许多基本的PHP表单模板在线试图纳入他们已经做了什么来取得成果,但没有任何成功。需要为我的网站编写什么代码才能具有编辑已在数据库中创建的记录的功能,而无需通过phpmyadmin。任何帮助都是有好处的。

+1

坏人坏事可怕的代码查询。切勿在简单的GET请求后放置像'delete'这样的破坏性操作。考虑一下,如果你的浏览器决定预取你页面上的所有链接并点击每一个删除按钮,会发生什么?繁荣去你的数据库。 – 2014-10-07 14:31:22

+0

这不会是一个真正的网站。我只需要为我的分配标准提供删除功能。我明白,代码可能是可怕的,我不是最好的PHP,但我只需要更新功能工作 – 2014-10-07 14:37:52

+1

我很想说'那么这不会是一个真正的答案,然后',但基本上,**如果你正在关注班级**,这应该很简单。所以,阅读你的笔记,然后走开,如果你失败了,那么当你有什么需要证明我们没有为你做功课的时候,你会找到帮助。 – RiggsFolly 2014-10-07 14:44:06

回答

0

Edit将与Add一样,但您需要先阅读记录并填写字段值。

先从add代码,这样做:

<?php $MemberID = (int) $_GET['MemberID']; ?> 
<form action="<?php $_SERVER['PHP_SELF']?>" method="post"/> <br> 
<input type="hidden" name="MemberID" value="<?php echo $MemberID; ?>" 
<?php 
require_once('connection.php'); 
$result = mysqli_query($connection, "SELECT * FROM members where MemberID = $MemberID") or die(mysqli_error()); 

// loop through results of database query, displaying them in the table 
$row = mysqli_fetch_assoc($result); 
extract($row); 

echo "<label for='memberID' >Member ID:</label>"; 
echo "$memberID"; // member ID should not be editable 
echo "<br /><br />"; 

echo "<label for='username' >Username:</label>"; 
echo "<input type='text' name='username' id='username' value="$username" />"; 
echo "<br /><br />"; 

的PHP代码将有一个像

`UPDATE `members` SET `username` = '$username' ... WHERE `MemberID` = '$MemberID'"