php - 在 PHP 和 MySQL 中使用准备好的查询删除行

标签 php mysql json

<分区>

这是删除 MySQL 数据库中记录的网络服务调用的一部分 但是我收到一条错误消息“数据库错误。无法删除帖子!” 我的代码哪里出错了。

 <?php
        
            //load and connect to MySQL database stuff
            require("config.inc.php");
            
            //initial query
            $query = 'DELETE FROM messages WHERE id =? AND receiver =?';
            $query_params = array($_GET['id'], $_GET['receiver']);
            //execute query
            try {
                $stmt   = $db->prepare($query);
                $result = $stmt->execute(array());

                $response["success"] = 1;
                $response["message"] = "Post Successfully DELETED!";
                echo json_encode($response);        
            }
                
            catch (PDOException $ex) {
                $response["success"] = 0;
                $response["message"] = "Database Error. Couldn't delete post!";
                die(json_encode($response));
            }
        ?> 

更新:

$stmt->execute(array()) 应替换为 $stmt->execute($query_params) 缺少参数

最佳答案

您已经为您的语句准备了参数,但您实际上从未在语句中使用它们。

尝试改变:

$result = $stmt->execute(array());

到:

$result = $stmt->execute($query_params);

关于php - 在 PHP 和 MySQL 中使用准备好的查询删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26248428/

相关文章:

php - 将所有 Mysql 值相加 PHP

php - Yii2,如何使用另一个模型在 View 文件中显示

php - 在mysqli中把这行代码改成什么

PHP, MySQL 如何对两个不同日期范围内的数据求和

javascript - 在多个值后过滤

PHP 将 Windows-1256 编码的文本转换为 UTF-8

php - AngularJS $http.post 404

mysql - MySQL 中何时使用单引号、双引号和反引号

android - 在 http url 中发送 json 字符串

ruby-on-rails - Rails API - 期望显示已创建对象的 json。获取没有对象的 302 状态码