php - 单击按钮执行 MySQL 删除

标签 php mysql database

在我的代码中,我在 HTML 表中显示数据库中名为 Staff 的表,并且我想向 HTML 表中的每一行添加一个删除按钮,单击该按钮时,它将删除与其关联的记录。

根据搜索其他解决方案,我的代码如下所示:

员工.php:

require_once('../connection.php');

          //delete row on button click
          if(isset($_GET["del"])){
                    $idc = $_GET["del"];
                    if($VisitorManagement->query("DELETE FROM staff WHERE id=$idc")){
                         header('Location: delete-thankyou.php');
                    } else { 
                        echo "Failed to delete staff member.";
                    }    
                }  

            $result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");

            echo "<table id='staff'>
            <thead>
            <tr>
            <th>Name</th>
            <th>Email</th>
            <th></th>
            </tr>
            </thead>";



            while($row = mysqli_fetch_array($result))
            {   
                echo "<tbody>";
                echo "<tr>";
                echo "<td>" . $row['fullName'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
                echo "<td><a class='button alert' href='staff.php?del=".$row["idc"]."'>Delete</a></td>";
                echo "</tr>";
            }
         echo "</tbody>";   
         echo "</table>";

连接.php:

$hostname_VisitorManagement = "localhost";
$database_VisitorManagement = "visitor-management";
$username_VisitorManagement = "***";
$password_VisitorManagement = "***";
$VisitorManagement = mysqli_connect($hostname_VisitorManagement, $username_VisitorManagement, $password_VisitorManagement, $database_VisitorManagement);

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

date_default_timezone_set('America/New_York');

不幸的是,当我点击其中一个按钮时,它无法删除记录并回显在staff.php中预定义的错误消息。我是否缺少一些东西来让它工作?

最佳答案

我可以通过将 idc 的所有实例更改为 id 来修复此问题。

新代码是:

require_once('../connection.php');

      //delete row on button click
      if(isset($_GET["del"])){
                $id = $_GET["del"];
                if($VisitorManagement->query("DELETE FROM staff WHERE id=$id")){
                     header('Location: delete-thankyou.php');
                } else { 
                    echo "Failed to delete staff member.";
                }    
            }  

        $result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");

        echo "<table id='staff'>
        <thead>
        <tr>
        <th>Name</th>
        <th>Email</th>
        <th></th>
        </tr>
        </thead>";



        while($row = mysqli_fetch_array($result))
        {   
            echo "<tbody>";
            echo "<tr>";
            echo "<td>" . $row['fullName'] . "</td>";
            echo "<td>" . $row['email'] . "</td>";
            echo "<td><a class='button alert' href='staff.php?del=".$row["id"]."'>Delete</a></td>";
            echo "</tr>";
        }
     echo "</tbody>";   
     echo "</table>";

关于php - 单击按钮执行 MySQL 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37143206/

相关文章:

php - Codeigniter 查询未正确形成

php - Laravel 未在日期时间字段上保存时区偏移量

mysql - 选择元素最小值的行(如果表中存在单行/多行)

MySQL 当时正在处理一个 SQL 查询吗?

android - 删除表时出现受限 API lint 错误。房间持久性

java - 如何用Java设计2/3层分布式应用程序?

javascript - 执行AJAX返回脚本

php - 使用范围内的价格过滤产品

mysql - MySQL RENAME 和 CREATE TABLE 语句冲突的问题

database - bcp 数据文件中遇到意外的 EOF