Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-SQL-DELETE查询不起作用_Php_Mysql_Sql - Fatal编程技术网

PHP-SQL-DELETE查询不起作用

PHP-SQL-DELETE查询不起作用,php,mysql,sql,Php,Mysql,Sql,瞄准 删除选中ID的所有数据 问题 数据不会被删除 错误 开发人员控制台中未提示任何错误 作品描述 根据我对codes函数的理解,检索用户ID delete_userID,然后使用mysqli_real_escape_string$conn,$_POST['delete_userID']将其传递到$delete_userID;。但是我不明白为什么删除查询不起作用。我在网上看到了几个关于这个的问题,其中一些是因为缺少数据库代码mysql\u select\u dbdata2017,$conn;但这

瞄准

删除选中ID的所有数据

问题

数据不会被删除

错误

开发人员控制台中未提示任何错误

作品描述

根据我对codes函数的理解,检索用户ID delete_userID,然后使用mysqli_real_escape_string$conn,$_POST['delete_userID']将其传递到$delete_userID;。但是我不明白为什么删除查询不起作用。我在网上看到了几个关于这个的问题,其中一些是因为缺少数据库代码mysql\u select\u dbdata2017,$conn;但这似乎不是我的问题。此外,我还将其设置为一旦单击了输入按钮,它将发送一个删除调用,并且if语句将检查按钮是否已单击以及调用是否已收到。除了检索用户ID外,还通过使用JavaScript函数传递了要删除的用户ID,如下所示

function bindList() {
    var $table = $('#eventsTable');
    $table.bootstrapTable({
        url: 'list-phaUser.php',
        search: true,
        pagination: true,
        columns: [{
            field: 'userID',
            title: 'User ID',
            sortable: true,
            },{
            field: 'operate',
            title: 'Action',
            align: 'center',
            sortable: true,
            events: operateEvents,
            formatter: operateFormatter
        },],
    });
    $(".fixed-table-toolbar").append("<div id='table-title' class='columns columns-left btn-group pull-left'>User List</div>");
    $("#divFooter").remove();
    $("<div class='row' id='divFooter' style='margin-left:-4%;margin-right:-4%;margin-bottom:-2%;'></div>").insertAfter("#divtb1");
    $("#divFooter").load('footer.php');
}

    window.operateEvents = {
    'click .icon_edit': function (e, value, row, index) {
        $("#edit_userID").val(row.userID);
        $('#edit_model').modal('show');
    },
    'click .icon_delete': function (e, value, row, index) {
        $("#delete_userID").val(row.userID);
        $('#delete_model').modal('show');
    }
};
删除代码

更新

delete_用户ID是通过JavaScript函数传入的,如下所示

function bindList() {
    var $table = $('#eventsTable');
    $table.bootstrapTable({
        url: 'list-phaUser.php',
        search: true,
        pagination: true,
        columns: [{
            field: 'userID',
            title: 'User ID',
            sortable: true,
            },{
            field: 'operate',
            title: 'Action',
            align: 'center',
            sortable: true,
            events: operateEvents,
            formatter: operateFormatter
        },],
    });
    $(".fixed-table-toolbar").append("<div id='table-title' class='columns columns-left btn-group pull-left'>User List</div>");
    $("#divFooter").remove();
    $("<div class='row' id='divFooter' style='margin-left:-4%;margin-right:-4%;margin-bottom:-2%;'></div>").insertAfter("#divtb1");
    $("#divFooter").load('footer.php');
}

    window.operateEvents = {
    'click .icon_edit': function (e, value, row, index) {
        $("#edit_userID").val(row.userID);
        $('#edit_model').modal('show');
    },
    'click .icon_delete': function (e, value, row, index) {
        $("#delete_userID").val(row.userID);
        $('#delete_model').modal('show');
    }
};

你没有填写表格

<div class="modal-body">
    <p>Do you want to delete?</p>
</div>
<form method="POST" action="">
    <div class="form-group">
        <label for="delete_userID" class="control-label">User ID:</label>
        <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
    </div>
    <div class="modal-footer">
        <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
    </div>
</form>
<?php
if (isset($_POST['delete'])) {
    $delete_userID = mysqli_real_escape_string($conn,$_POST['delete_userID']);
    mysql_select_db("data2017", $conn);
    $sql = "DELETE FROM pha_user WHERE id ='".$delete_userID."'";
    $Deletequery = mysqli_query($conn,$sql);
    if ($Deletequery) {
        echo "<script>alert('Delete Successful.');</script>";
    } else {
        echo "<script>alert('Delete Failed.');</script>";
    }
}
?>

你没有填写表格

<div class="modal-body">
    <p>Do you want to delete?</p>
</div>
<form method="POST" action="">
    <div class="form-group">
        <label for="delete_userID" class="control-label">User ID:</label>
        <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
    </div>
    <div class="modal-footer">
        <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
    </div>
</form>
<?php
if (isset($_POST['delete'])) {
    $delete_userID = mysqli_real_escape_string($conn,$_POST['delete_userID']);
    mysql_select_db("data2017", $conn);
    $sql = "DELETE FROM pha_user WHERE id ='".$delete_userID."'";
    $Deletequery = mysqli_query($conn,$sql);
    if ($Deletequery) {
        echo "<script>alert('Delete Successful.');</script>";
    } else {
        echo "<script>alert('Delete Failed.');</script>";
    }
}
?>

尝试使用PDO,因为最新的PHP版本不推荐使用MySQL DB连接。 首先,您必须声明表单方法和操作,然后还要关闭它

请尝试下面的代码

<?php
$config = array(
 'host'     => 'localhost',
 'username' => 'db_username',
 'password' => 'db_password',
 'dbname'   => 'data2017'
);

$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if(isset($_POST['delete_userID'])){

 $delete_userID = $_POST['delete_userID'];

 // No need to use mysqli_real_escape_string when we using prepared statements

 $stmt = $db->prepare("DELETE FROM pha_user WHERE id = '$delete_userID' ");
 $stmt->execute();

 if($stmt){
    header("Location:appropriate_page.php?del_success");
 }else{
    header("Location:appropriate_page.php?del_failure");
 }
}
?>

<form method="POST" action="">
<div class="form-group">
  <label for="delete_userID" class="control-label">User ID:</label>
  <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
</div>
<div class="modal-footer">
  <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
</div>
</form>

尝试使用PDO,因为最新的PHP版本不推荐使用MySQL DB连接。 首先,您必须声明表单方法和操作,然后还要关闭它

请尝试下面的代码

<?php
$config = array(
 'host'     => 'localhost',
 'username' => 'db_username',
 'password' => 'db_password',
 'dbname'   => 'data2017'
);

$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if(isset($_POST['delete_userID'])){

 $delete_userID = $_POST['delete_userID'];

 // No need to use mysqli_real_escape_string when we using prepared statements

 $stmt = $db->prepare("DELETE FROM pha_user WHERE id = '$delete_userID' ");
 $stmt->execute();

 if($stmt){
    header("Location:appropriate_page.php?del_success");
 }else{
    header("Location:appropriate_page.php?del_failure");
 }
}
?>

<form method="POST" action="">
<div class="form-group">
  <label for="delete_userID" class="control-label">User ID:</label>
  <input type="text" class="form-control" id="delete_userID" name="delete_userID" readonly/>
</div>
<div class="modal-footer">
  <input type="submit" id="delete" name="delete" class="btn btn-block bt-login" value="Delete" />
</div>
</form>

我们可以看更多的元素吗?delete_userID里面没有值。@Scuzzy我没有在里面放一个,我意识到那是我的problem@saddam该值由javascript函数传入。我将编辑我的问题。但是Riccardo指出我没有,这是我的问题。我们可以看看更多的元素吗?delete_userID里面没有值。@Scuzzy我没有在里面放一个,我意识到那是我的problem@saddam该值由javascript函数传入。我将编辑我的问题。但是里卡多指出,我没有这个,这是我的问题。非常感谢里卡多!!!我刚试了一下,效果很好=圣诞快乐,新年快乐:也祝你圣诞快乐。非常感谢你,里卡多!!!我刚试了一下,效果很好=圣诞快乐,新年快乐:也祝你圣诞快乐。谢谢你的帮助,我回去工作的时候一定要试试。圣诞快乐,新年快乐:不客气。使用PDO bcz现在一天服务器也会反复提示升级您的php版本。注意并感谢您的建议。我对PHP非常陌生,我会仔细阅读=d感谢你的帮助,我回去工作后会尝试一下。圣诞快乐,新年快乐:不客气。使用PDO bcz现在一天服务器也会反复提示升级您的php版本。注意并感谢您的建议。我对PHP非常陌生,我将阅读关于=D的文章