-1

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="#">&laquo;</a> <a class="active" href="#">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">&raquo;</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.
  • Possible duplicate of https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – 04FS Jun 12 '19 at 07:38
  • You are calling a function like php file. You have to know how to call function in href tag look in to this [link](https://stackoverflow.com/questions/43719877/how-to-call-php-function-from-anchor-tag) – Rasa Mohamed Jun 12 '19 at 07:39
  • It looks as if you assume that clicking the link, calls your Php function. But Php isn't wired like that. Also if you surround the delete($id) call in Php tags, you'll instantly delete those records (before any clicking/activating of the link), assuming the code works ($this looks to be a non-object reference in your function). – Progrock Jun 12 '19 at 10:31

2 Answers2

0

I don't know if you're using some sort of framework to map url parts to function parameters, but you could something like

<a href='delete/<?= $id ?>'>Delete</a>

or even

<a href='delete?id=<?= $id ?>'>Delete</a>

And then use $_REQUEST to retrieve the parameter.

At least that will send the parameter to your server. I don't know how you've mapped your functions to routes, so I don't know if you're getting the right url..

Kyrre
  • 670
  • 1
  • 5
  • 19
0

I think you should use this for delete record. in while loop

<a href='?id=<?= $id ?>'>Delete</a>

Remove function and use $_REQUEST like below code.

if(isset($_REQUEST['id']) && !empty($_REQUEST['id']))
{
    $dbh = connectDb();
    $stmt = $dbh->prepare( "DELETE FROM users WHERE id =:id" );
    $stmt->bindParam(':id', $_REQUEST['id']);
    $stmt->execute();
}
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119