php - 删除查询正确,但无法删除

标签 php mysql

我正在制作一个 PHP 代码片段,它将更新和删除同一页面中的管理数据。我的更新查询很好,我的删除查询也是正确的,只是按下删除按钮时无法删除数据。 代码:

<?php
    require("connect.inc");
    session_start();

    if(isset($_POST['update_admin'])){
        $id = $_POST['admin_id'];
        $pass = htmlspecialchars(stripslashes(trim($_POST['admin_pass'])));
        $name = htmlspecialchars(stripslashes(trim($_POST['admin_name'])));

        $up_ad = "update admins set a_name = '$name', a_pass = '$pass' where a_id = '$id'";
        mysql_query($up_ad);
    }

    if(isset($_POST['deladmin'])){
        mysql_query("delete from admins where a_id = '$_POST[admin_id]'");
    }

    $admin_sql = "select * from admins order by a_name";
    $adminres = mysql_query($admin_sql);

    echo "
    <table border = 1>
        <tr>
            <td>Admin ID</td>
            <td>Admin Name</td>
            <td>Password</td>
            <td colspan = 3>Options</td>
        </tr>
    "; 
    while($admin_rows = mysql_fetch_array($adminres)){
    ?>
        <tr>
            <form action = 'editdel_admin.php' method = post>
                <td><?php echo $admin_rows['a_id'] ?></td>
                <td><input type = text name = 'admin_name' placeholder = 'Type here' value = "<?php echo htmlspecialchars_decode($admin_rows['a_name']); ?>" required></td>
                <td><input type = text name = 'admin_pass' placeholder = 'Maximum of 25 characters' maxlength = '25' value = "<?php echo htmlspecialchars_decode($admin_rows['a_pass']); ?>" required></td>
                <?php 
                echo "
                <td><input type = hidden name = 'admin_id' value = ".$admin_rows['a_id']."></td>
                <td><input type = submit name = 'update_admin' value = 'Edit'></td>
                <td><input type = submit name = 'deladmin' value = 'Delete'></td>
            </form>
        </tr>
    ";
    }
    echo "
    </table>";
?>

最佳答案

检查用户是否有删除数据库记录的权限。

将删除查询更改为

"delete from admins where a_id = '" . $_POST['admin_id'] . "'";

关于php - 删除查询正确,但无法删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22353162/

相关文章:

php - PDO rowCount() 与 SELECT mysql,为什么一个有效而另一个无效?

mysql - 名称 [jdbc/mydb] 未在此上下文中绑定(bind)。找不到 [jdbc]

mysql - 创建用于创建基于分组依据的序列号生成 View 的函数

PHP Laravel Eloquent,foreach 破坏了我的关系

php - 从数据库中的字段中减去值

php - 如何通过 AJAX 请求发布包含特殊字符(例如 '&' 特别)的长字符串?

php - 如何使用 Cakephp2 制定将数据与 SQL 分离的逻辑?

php - MySQL 具有多个类别 id 的搜索

php - MYSQL:创建查询以显示条目的父级/层次结构(面包屑)

MySQL:获取前 1 个 ID