I was trying with the following code in PHP for generating row and delete button. But instead of deleting the selected ones, it deletes from the bottom of the row.
<?php
$connection = mysqli_connect('localhost','root','','rohit');
{
$query = "SELECT * FROM user";
$result = mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$name = $row['name'];
$lname = $row['lname'];
$dept = $row['department'];
$dob = $row['DOB'];
$doj = $row['DOJ'];
$mobile = $row['mobile'];
$email = $row['email'];
$salary = $row['salary'];
$gender = $row['gender'];
echo "<tr>";
echo "<td>{$id}</td>";
echo "<td>{$name}</td>";
echo "<td>{$lname}</td>";
echo "<td>{$dept}</td>";
echo "<td>{$dob}</td>";
echo "<td>{$doj}</td>";
echo "<td>{$mobile}</td>";
echo "<td>{$email}</td>";
echo "<td>{$salary}</td>";
echo "<td>{$gender}</td>";
echo '<td>' . '<form method="post"><button class="mb-1 mr-1 btn btn-primary" name="edit"><i class="fa fa-edit"></i></button>' .
'<button class="mb-1 mr-1 btn btn-danger" name="delete"><i class="fa fa-times"></i></button></form>'
. '</td>';
echo "</tr>";
}
}
?>
<?php
if (isset($_POST['delete'])) {
$query1 = "DELETE FROM user ";
$query1 .= "WHERE id = $id LIMIT 1";
$result1 = mysqli_query($connection,$query1);
if (!$result1) {
die("FAILED" . mysqli_error($connection));
}
}
?>
Any kind of help is appreaciated. Thanks in advance. :)
"WHERE id = $id LIMIT 1"- Where are you defining$id? You should also look into using parameterized prepared statements instead of injecting the user data directly into the queries like that.method="post"while yourif-statement are looking for$_GET['delete']?$_GET['delete']their, there actually$_POST['delete']and it deletes from the bottom.$idwhich you use in the query? I don't even see you passing any id through the form at all?