I’m learning PHP with SQL statments, but I’ve encountered a problem when trying to create a delete button.
The problem is that nothing is happening when i’m pressing the “delete” button.
The query is functioning properly, it gets the correct userid from the loop.
<?php
require_once 'db_connect.php';
$select = "SELECT * FROM Users";
$result = mysqli_query($dbc, $select) or die('could not query database');
while ($row = mysqli_fetch_assoc($result)) {
echo '
<tr>
<td>' . $row['userid'] . '</td>
<td>' . $row['fnamn'] . ' ' . $row['enamn'] . '</td>
<td>' . $row['username'] . '</td>
<td>' . $row['email'] . '</td>
<td>' . '<form action="" method="post">
<input type="button" name="delete" value="Radera" />
</form>' . '</td>
</tr>';
if(isset($_POST['delete'])) {
$userid = $row['userid'];
$delete = "DELETE FROM Users WHERE userid = '$userid' ";
$result = mysqli_query($dbc, $delete) or die('could not query database');
}
}
mysqli_close($dbc);
?>
You need to:
if (isset($_POST['delete']))statement outside andbefore your
whileloop.hidden input).
Something like this:
I wouldn’t advise you to just insert
$_POST['user_id']directly into your query like above; you’ll want to read up on prepared statements