【问题标题】:Adding a delete button in PHP on each row of a MySQL table在 MySQL 表的每一行上添加 PHP 中的删除按钮
【发布时间】:2017-09-03 07:49:42
【问题描述】:

我正在尝试在每一行上添加一个删除按钮,以便在按下按钮时删除一条记录。我是 PHP 和 MySQL 以及 Stack Overflow 的新手。

下面是我的表格,它从我的 MySQL 数据库中提取信息并且有效。

       <table class="table" >
       <tr>
       <th> Staff ID </th>
       <th> Staff Name </th>
       <th> Class </th>
       <th> Action </th>

       </tr>   

       <?php

       while($book = mysqli_fetch_assoc($records)){

       echo "<tr>";
       echo "<td>".$book['Staff_ID']."</td>";
       echo "<td>".$book['Staff_Name']."</td>";
       echo "<td>".$book['Class']."</td>";
       echo "</tr>";
       }// end while loop

【问题讨论】:

标签: php html mysql


【解决方案1】:

简单使用PHP如下(可以使用JS)

while($book = mysqli_fetch_assoc($records)){

echo "<tr>";
echo "<td>".$book['Staff_ID']."</td>";
echo "<td>".$book['Staff_Name']."</td>";
echo "<td>".$book['Class']."</td>";
echo "<td><a href='delete.php?id=".$book['Staff_ID']."'></a></td>"; //if you want to delete based on staff_id
echo "</tr>";
}// end while loop

在您的delete.php 文件中,

$id = $_GET['id'];
//Connect DB
//Create query based on the ID passed from you table
//query : delete where Staff_id = $id
// on success delete : redirect the page to original page using header() method
$dbname = "your_dbname";
$conn = mysqli_connect("localhost", "usernname", "password", $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM Bookings WHERE Staff_ID = $id"; 

if (mysqli_query($conn, $sql)) {
    mysqli_close($conn);
    header('Location: book.php'); //If book.php is your main page where you list your all records
    exit;
} else {
    echo "Error deleting record";
}

【讨论】:

  • 非常感谢,这已经奏效了:) 希望mu大学的导师能有这样的回应!干杯:)
  • @webDev 我在这里做错了吗?对此感到抱歉
  • 查看更新的代码,您不需要 book.php @HamzahAmir
  • 谢谢你解决了它!:)
  • 这个很简单但是很强大!!
【解决方案2】:

创建一个接收 $_GET['id'] 的 delete.php 文件,然后运行 ​​sql 以在他们转到该页面时删除该记录。通过两种方式完成:如下所示的锚标记,

或者制作一个按钮而不是锚点运行 ajax(通过 jquery)发送该 id 并运行我上面提到的 delete.php 脚本。

table class="table" >
       <tr>
       <th> Staff ID </th>
       <th> Staff Name </th>
       <th> Class </th>
       <th> Action </th>

       </tr>   

       <?php

       while($book = mysqli_fetch_assoc($records)){

       echo "<tr>";
       echo "<td>".$book['Staff_ID']."</td>";
       echo "<td>".$book['Staff_Name']."</td>";
       echo "<td>".$book['Class']."</td>";
       echo "<td><a href='delete.php?id=".$book['Staff_ID']."'>Delete</a></td>";
       echo "</tr>";
       }// end while loop

【讨论】:

    【解决方案3】:

    我建议您使用 Ajax 进行调用,例如 @webDev,但不要调用 PHP,而是使用 AjaxCall 调用 Javascript,然后使用 JS 隐藏/删除有问题的行,这样,用户不必重新加载整个页面。

    您可以使用此代码而不是 href 来补充您选择的正确答案:

    echo   '<td><button onclick="deleteRow('.$book['Staff_ID'].')">Delete</button></td>';
    

    并在&lt;head&gt;部分添加如下函数:

    <script>
        function deleteRow(StaffID){
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {                     
    
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                      alert("Deleted!");
                }
            };
            document.getElementById("table").deleteRow(x);
            xhttp.open("GET", "delete.php", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send("id="+StaffID);
            }       
    </script>
    

    【讨论】:

    • 是的,我知道,但是发布问题的用户是初学者,对于初学者来说,最好尽可能简单地回答。在学习的同时更好地理解首先使用 PHP
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多