douji0108 2014-03-27 17:48
浏览 58
已采纳

如何使用php / pdo从MySQL数据库中删除友谊并检查它是否成功

Im trying to delete a friendship between two users from the db

the friendship table is simple:

friend_one |friend_two
100        |142
142        |100

Here is the code I have, but its not working:

if (!empty($_POST)) {

    $remover_id      = $_POST['remover_id'];
    $removed_id      = $_POST['removed_id'];

    try {

        $query = "DELETE * FROM 

                 `friendships` 

                 WHERE 

                 (friend_one = :remover_id AND friend_two = :removed_id)

                 OR 

                 (friend_two = :remover_id AND friend_one = :removed_id)

                 ";

        $sth = $connection->prepare($query);

        $sth->execute(
                      array(

                            ':remover_id' => $remover_id,
                            ':removed_id' => $removed_id

                            ));   

        if($sth->rowCount () >=0){
            $response["success"] = $http_response_success;
            die(json_encode($response));
            $connection = null; 
        } else {
            $response["success"] = $http_response_server_error;
            $response["message"] = $http_message_server_error;   
            die(json_encode($response));
            $connection = null;
        }

    } catch (PDOException $ex) {

        $response["success"] = $http_response_server_error;
        $response["message"] = $http_message_server_error;
        die(json_encode($response));
        $connection = null;

    }  


} else {
        $response["success"] = $http_response_bad_request;
        $response["message"] = $http_message_bad_request;   
        die(json_encode($response));
        $connection = null;
}

First of all I dont think the way I check for success is correct, second of all, the friendship doesnt get removed from the DB anyway.

When I run this I find myself in the else statement:

    if($sth->rowCount () >=0){
        $response["success"] = $http_response_success;
        die(json_encode($response));
        $connection = null; 
    } else {
        $response["success"] = $http_response_server_error;
        $response["message"] = $http_message_server_error;   
        die(json_encode($response));
        $connection = null;
    }
  • 写回答

1条回答 默认 最新

  • doudou20080720 2014-03-27 17:52
    关注

    You have an SQL error for your DELETE statement

    DELETE FROM `friendships` WHERE
       (friend_one = :remover_id AND friend_two = :removed_id)
       OR 
       (friend_two = :remover_id AND friend_one = :removed_id)
    

    You had an asterisk after delete, where there shouldn't be one. https://dev.mysql.com/doc/refman/5.0/en/delete.html

    As for checking for PDO Errors, you shouldn't use $sth->rowCount().

    if(!$sth->execute($data)) {
       // Error (SQL Error)
    }
    
    if($sth->rowCount() > 0) {
       // At least 1 record was updated / inserted / deleted / (Possibly Selected)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记