what I am trying to do is pass a value to a variable depending on which button the user presses. I created a table and delete button with a while loop, however, because of this, all the buttons have the same name and id. how can i pass the specific UserID to a variable when the appropriate button is pressed??
this is my PHP with embedded HTML
<?php
$servername = "localhost";
$username = "root";
$password = "A1128857795!";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else{
echo "Connected successfully";}
mysqli_select_db($conn,'test_schema');
$sql = "SELECT * FROM userinfo;";
$retvalues = mysqli_query($conn, $sql);
if(!$retvalues)
{
die("could not get data: " . mysqli_error());
}
print("<br>");
echo "<form action='delete.php' method='POST'>";
echo"<table border='1'>";
echo"<tr align='center'>";
echo "<th>ID</th>";
echo "<th>NAME</th>";
echo "<th>GENDER</th>";
echo "</tr>";
while($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC))
{
print("<tr>");
echo "<td><input type='text' name='id' value='{$row['UserID']}'/></td>". " " . "<td>{$row['FirstName']}" . " " . "{$row['LastName']}</td>" .
" " . "<td align='center'>{$row['gender']}</td>";
echo "<td><input name='Delete' value='Delete' type='submit'></td>";
print("</tr>");
}
echo "</table>";
echo "</form>";
When I press any of the buttons, it returns the ID for the last row of the table every time, causing the DELETE statement to always delete the last record from my database.
if(isset($_POST['id']))
{
$delUID = $_POST['id'];
$sql="DELETE FROM userinfo WHERE UserID = $delUID";
$query = mysqli_query($conn, $sql);
echo "DELETE FROM userinfo WHERE UserID = $delUID;";
}
$conn->close();
?>
Any advice will be welcomed. thank you.