How to change the delete function so that it deletes specific users instead of the entire user list
P粉087074897
P粉087074897 2023-09-05 19:14:48
0
1
581
<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粉087074897
P粉087074897

reply all(1)
P粉083785014

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)

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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!