search

Home  >  Q&A  >  body text

How to change the delete function so that it deletes specific users instead of the entire user list

<p>I have a list of users registered to a website and I want to delete a specific user when the delete link is pressed, but I'm running into a problem where all users are deleted from the user list once I open a page of the website. </p> <p>This is a list of users, where <code>$users </code> is an array containing a list of users: </p> <pre class="brush:php;toolbar:false;"><?php foreach($users as $user): ?> <tr> <th><?= $user->name ?></th> <td><?= $user->email ?></td> <td><a href="<?php $user->delete()>?">Delete</a></td> <td><a href="">Update</a></td> </form> </tr> <?php endforeach?></pre> <p>Here is the delete function of <code>$user</code>: </p> <pre class="brush:php;toolbar:false;">public function delete(){ $result=$this->db->delete("users","id={$this->id}"); return $result; }</pre> <p>This is the database delete function:</p> <pre class="brush:php;toolbar:false;">public function delete(string $table,string $where,int $limit=1){ return $this->connection->exec("DELETE FROM $table WHERE $where LIMIT $limit"); }</pre> <p>How can I delete a specific user when the "Delete" link is pressed? ! </p> <p>I tried changing the delete functionality but got no results. </p>
P粉087074897P粉087074897487 days ago665

reply all(1)I'll reply

  • P粉083785014

    P粉0837850142023-09-06 16:38:27

    The problem is this code:

    <td><a href="<?php $user->delete()>?">Delete</a></td>

    The problem is that when you call that page, you delete the user directly instead of printing a link to the delete page.

    You need to create a separate route (or a .php file if you are not using a framework), such as delete_user.php?user_id={your_user_id} or /{user_id}/delete (in a framework scenario)< /p>

    This route/page must check the user passed in to the page for the presence of ads and then delete it by getting the user from the database and calling the delete() method.

    Afterwards, your code will look like the following in a php framework scenario:

    <td><a href="http://www.example.com/<?php $user->id?>"/delete>Delete</a></td>

    Or in a standalone scene like this:

    <td><a href="http://www.example.com/delete_user.php?user_id=<?php $user->id?>">Delete</a></td>

    reply
    0
  • Cancelreply