我有一个将记录添加到数据库的表单。每行都有一个“编辑”按钮。当用户单击“编辑”按钮时,它会将用户带到另一个页面,该页面具有专门用于选择“编辑”的ID的表单,其中的原始信息将更新为“更新”。问题:单击“更新”按钮时,页面会向第一页添加新记录,而不是更新和替换相同ID的信息。 谢谢您的帮助! :d
第一页的代码段,其中包含每个旁边都有“修改”选项的记录列表:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users ORDER BY lastname ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class='container'>";
echo "<table class='table'>";
echo "<tr id='hover'>";
echo "<td id='lastname'>".$row["lastname"]."</td>";
echo "<td id='firstname'>".$row["firstname"]."</td>";
echo "<td id='username'>".$row["username"]."</td>";
echo "<td id='email'>".$row["email"]."</td>";
echo "<td id='editdelete'>
<form action='Edit.php?id=" . $row['id'] . "' method='post'>
<input type='submit' name='edit' value='Edit' id='Edit'><input type='hidden' name='id' value='" . $row['id'] . "'>
<input type='submit' name='delete' value='Delete' id='delete'><input type='hidden' name='id' value='" . $row['id'] . "'>
</form></td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo '<br>';
}
}
else {
echo "0 results";
}
$conn->close();
?>
“编辑”页面代码(第二页):
<?php
// DB Connection Info
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Code for EDIT button
if (isset($_POST['edit'])) {
$userid = $_POST['id'];
}
// Code for UPDATE button
if (isset($_POST['update'])) {
$userid = $_POST['id'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$sql = "UPDATE users SET firstname = '$firstname', lastname = '$lastname', username = '$username', password = '$password', email = '$email' WHERE id = '$userid';";
$conn->query($sql);
}
// SQL Query
$sql = "SELECT * FROM users WHERE id = '$userid';";
$result = $conn->query($sql);
//Loop through and echo all the records
while ($row = $result->fetch_assoc()){
echo "<form method='post' action='Index.php'>";
echo "<input type='hidden' name='id' value='" . $row['id'] . "'>";
echo "First Name: <input type='text' name='firstname' value='" . $row['firstname'] . "'> <br>";
echo "Last Name: <input type='text' name='lastname' value='" . $row['lastname'] . "'> <br>";
echo "Username: <input type='text' name='username' value='" . $row['username'] . "'> <br>";
echo "Password: <input type='password' name='password' value='" . $row['password'] . "'> <br>";
echo "Email: <input type='text' name='email' value='" . $row['email'] . "'> <br> ";
echo "<input type='submit' name='update' value='Update'>";
echo "</form>";
}
$conn->close();
?>
答案 0 :(得分:2)
在edit.php
中,您的表单操作会链接到index.php
而不是edit.php
。