I have a table with an foreach array of items
. I want to delete each item by a link. In the DB, the table is "users
" and the row is "id
".
PHP code of the table:
<table class="blueTable">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>MAIL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<div class="links"><a href="#">«</a> <a class="active" href="#">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">»</a></div>
</td>
</tr>
</tfoot>
<tbody>
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
echo "<tr>";
while($row = $query->fetch_array()){
$id = $row['id'];
echo "<tr>"; //put <tr> opening code inside, not outside of while()
echo "<td>".$row['id']."</td>";
echo "<td>".$row['username']."</td>"; //remove <tr></tr>
echo "<td>".$row['email']."</td>"; //remove <tr></tr>
echo "<td><a href='delete($id)'>Delete</a></td>"; //remove <tr></tr>
echo "</tr>"; //close tr only once at the end
}
?>
</tbody>
</table>
The function that delete the code is:
function delete($id) {
$sql = 'DELETE FROM users
WHERE id = :id';
$q = $this->pdo->prepare($sql);
return $q->execute([':id' => $id]);
}
I used the delete link with:
<a href='delete($id)'>Delete</a>
But didn't work. It gives the correct Id -for example (delete(5)- but didn't delete it because it shows:
The requested URL /sistema-login/admin/delete(5) was not found on this server.