0

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);
12
  • 1
    Use type="hidden" for hidden inputs. Commented Jun 5, 2020 at 21:20
  • 1
    Do you really want to create a new table for each row of results, rather than just a table row? Commented Jun 5, 2020 at 21:21
  • Where is $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. Commented Jun 5, 2020 at 21:22
  • What's your DELETE statement? BTW, you should probably set <input type="hidden"... instead of type="text" with style="display:none;". Also, with such loop, you are creating new table for every row. Commented Jun 5, 2020 at 21:23
  • 1
    It might have been easier than expected, seems like you have another <form> opening tag instead of closing one </form> so technically there is only one form with just last $log_id value, overrided many times. Commented Jun 5, 2020 at 21:48

1 Answer 1

1

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 inputs 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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.