Reputation: 23
So I have pulled all information from MySQL with PHP by searching for all rows where the ID is equal to an ID that matches a specific user.
I've then displayed it with a while loop into a nice table. At the end of the table I've added a form with a button to delete the row.
Now, I'm not sure how to accomplish this and I'm not sure what to really search for on the web, but I want the button to delete the specific row that is displayed, but i'm displaying many rows and so the delete button really just deletes the last displayed row. I've tried putting it into an array but that just does the same thing as well.
How might I be able to accomplish this so it deletes the specific row that it is accompanied with? Because Ive generated this button iteratively its essentially useless.
Also the form is set to GET so I could see the error that was occurring in what was passed.
if($logquery->num_rows){
while($logquery->fetch()){
echo "<table><tr><th>Date</th></tr>
<tr><td>".$date."</td></tr>
<tr><th>User</th></tr>
<tr><td>".$user."</td></tr>
<tr><th>Log</th></tr>
<tr><td><p>".$log."</p></td></tr>
<tr><th>Comment</th></tr>
<tr><td>".$comment."</td></tr>
<tr><td><form action='viewuser.php' method='get'>
<input style='display: none;' type='text' name='delog_' id='delog' value='".$log_id."'>
<input type='submit' name='delsub' value='Delete?'>
<form></td></tr>
</table><br>
";
}
}
On delete:
if(isset($_GET['delsub'])){
$delquery = $con->prepare("DELETE FROM logs WHERE log_id = ? ");
$delquery->bind_param("i", $_GET['delog_']);
$delquery->execute();
echo "Log Delete. Returning to user search";
header("refresh:5; users.php");
}
Query:
$logquery = $con->prepare(" SELECT * FROM logs WHERE user_id = ? ORDER BY log_id DESC ");
$logquery->bind_param("s", $userid);
$logquery->execute();
$logquery->store_result();
$logquery->bind_result($log_id, $user_id, $log, $date, $user, $comment);
Upvotes: 0
Views: 53
Reputation: 2617
You have another <form>
opening tag instead of closing one.
That is why you can only delete row that matches last log id - there is technically only one form with multiple input
s named delog_
. Performing click on any of submit buttons sends that last, overrided id.
It's equivalent to:
<form action="viewuser.php" method="get">
<input type="hidden" name="delog_" id="delog" value="1"> <!-- overrided -->
<input type="submit" name="delsub" value="Delete">
<input type="hidden" name="delog_" id="delog" value="2"> <!-- overrided -->
<input type="submit" name="delsub" value="Delete">
<input type="hidden" name="delog_" id="delog" value="3"> <!-- overrided -->
<input type="submit" name="delsub" value="Delete">
<input type="hidden" name="delog_" id="delog" value="4"> <!-- last id, actually sent -->
<input type="submit" name="delsub" value="Delete">
</form>
Upvotes: 1