I have a form that adds records to a database. Each row has an 'Edit' button. When a user clicks on the 'Edit' button, it takes the user to another page that has the form specifically for that id that was chosen to Edit, with the original information in it to Update. The problem: When the Update button is clicked, the page adds a new record to the first page, instead of updating and replacing the information for the same id. Thanks for the help! :D
Code Snippet of first page with list of records with 'Edit' option next to each:
<?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();
?>
Code of 'Edit' page (second page):
<?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();
?>