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);
type="hidden"for hidden inputs.$log_id(or any of the other variables for that matter) being populated? It isn't clear. There's no obvious link between that and the query result. Have you bound it somewhere? If so please show us the code so we are clear.<input type="hidden"...instead oftype="text"withstyle="display:none;". Also, with such loop, you are creating new table for every row.<form>opening tag instead of closing one</form>so technically there is only one form with just last$log_idvalue, overrided many times.