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.
You can either use an array in the name of the old school input-button or just use the <button>
tag to have different value and caption.
<form method="post">
<?php for($id = 1; $id <= 3 ; $id++): ?>
<input type="submit" name="delete_a[<?php echo $id; ?>]" value="Caption of delete button a">
<button type="submit" name="delete_b" value="<?php echo $id; ?>">Caption of delete button b</button>
<br>
<?php endfor; ?>
</form>
<?php
var_dump($_POST);
?>
With the array version you could even use checkboxes and any submit button to delete multiple rows at once. You are then interested in the keys of the array $_POST['delete_a']
.
I will think about doing something like this
<?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>");// I don't think u need to give user a chance to edit their UserID.. so I kept it in hidden type
echo "<td>{$row['UserID']}<input type='hidden' name='userid' 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>";
Receiving side
if(isset($_POST['Delete']))
{
$delUID = $_POST['userid'];
$sql="DELETE FROM userinfo WHERE UserID = $delUID";
$query = mysqli_query($conn, $sql);
echo "DELETE FROM userinfo WHERE UserID = $delUID;";
}
$conn->close();
@f_anto Has the right idea.
Create your table using query loop but attach a unique link or button to each row.
while($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC)) {
echo "<td><input type='text' name='id' value='{$row['UserID']}'/></td>"
. "<td>{$row['FirstName']} " . "{$row['LastName']}</td>" .
. "<td align='center'>{$row['gender']}</td>"
. "<td><a href='' id='$row['UserID']' class='delete-button'></td>";
}
Then use an AJAX call to do the dirty work.
$(".delete-button").on('click', function(e){
e.preventDefault();
var $id = $(this).attr("id");
$.ajax({
type: "POST",
url: "delete.php",
data: ["id": $id]
});
});
Or something like that. Light example: https://jsfiddle.net/mcmullen_greg/okvrbfnx/1/