【问题标题】:DELETE rows with prepared query in PHP and MySQL [closed]在 PHP 和 MySQL 中使用准备好的查询删除行 [关闭]
【发布时间】:2014-10-08 02:42:58
【问题描述】:

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

 <?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-&gt;execute(array()) 应替换为 $stmt-&gt;execute($query_params) 参数丢失

【问题讨论】:

  • 尝试将$ex-&gt;getMessage() 添加到您的失败消息中。这应该告诉你出了什么问题。
  • 你没有对$query_params做任何事情
  • @MikeW 那应该读作$ex-&gt;getMessage() ;)
  • @Fred-ii- 应该如此。这些新眼镜上的痘痘!
  • 对不起,这是一个不太集中的tnx

标签: php mysql json


【解决方案1】:

你已经为你的语句准备了参数,但你从来没有真正将它们与语句一起使用。

尝试改变:

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

到:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多