简体   繁体   中英

PHP Delete User Data From Table On Button Click

I have a code that works fine except one thing. When you click onto the Delete button, a modal box appers and it shows 2 buttons. When you click onto the OK button it should delete that user's data which you clicked. But this is deletes the user who has the lowest ID . But if I removes the modal box opening function and simpy just put a href='delete.php?id=".$roww['id']."' it works fine. Any idea how to solve this?

Code from the index.php:

include "dbConn.php"; // Using database connection file here
$records = mysqli_query($db,"select * from accounts"); // fetch data from database
while($data = mysqli_fetch_array($records)) {}

$conn = mysqli_connect("localhost", "root", "", "phplogin");
if ($conn-> connect_error) {
   die("Connection failed:". $conn-> connect_error);
}
$ssql = "SELECT * FROM accounts";
$result =  $conn-> query($ssql);
if ($result-> num_rows > 0) {
   while ($roww = $result-> fetch_assoc()) {
       $id = $roww['id'];
       echo "
           <span>".$roww['username']."</span>
           <span>".$roww['email']."</span>
           <a onclick='pop()'>Delete</a>

           // Delete modal
           <div id='box'>
                <img src='/assets/images/svg/rf-alert.svg' width='64px'>
                <h1>Attention!</h1>
                <p>You are going to delete this user permanently.</p>
                <a class='close' href='delete.php?id=".$roww['id']."' title='".$roww['id']."'>Delete</a> // This button should delete the data from the MySQL table
                <a class='close' onclick='pop()'>Cancel</a>
           </div>
       ";
} else { echo "0 result";}
}
$conn-> close();
<div id='box'>

All modals will have the same ID, may it be related to opening the first modal always? Try with something like:

<div id='box".$roww['id']."'>

Also you'll have to edit the pop() function to something like pop(id);

A better option would be create the modal dinamically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM