删除查询是正确的,但无法删除

时间:2014-03-12 13:33:06

标签: 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>";
?>

3 个答案:

答案 0 :(得分:2)

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

将删除查询更改为

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

答案 1 :(得分:0)

试试这个

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

而不是

mysql_query("delete from admins where a_id = '$_POST[admin_id]'");

答案 2 :(得分:0)

你错了这一行

 mysql_query("delete from admins where a_id = '$_POST[admin_id]'");

$_POST[admin_id]此处未正确指定键值。一定是这样的   $_POST['admin_id']。并在嵌入字符串中用户括号,以避免贪婪的方法字符串替换。像这样改变你的代码将解决你的问题

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

请注意,不推荐使用mysql_ *方法,而是使用mysqli_ *或pdo方法