删除功能适用于localhost,但不适用于Web

时间:2017-04-07 17:10:04

标签: php slim

我创建了一个删除数据库中对象的函数。当我测试这个功能时,它适用于localhost,但是,它不能在网站上运行。有谁知道如何解决这个问题? P.S:我用过Slim

$app->delete('/places/:id', 'authenticate', function($place_id) use($app) {
            global $user_id;

            $db = new DbHandler();
            $response = array();
            $result = $db->deletePlace($user_id, $place_id);
            if ($result) {
                $response["error"] = false;
                $response["message"] = "Place deleted succesfully";
            } else {

                $response["error"] = true;
                $response["message"] = "Place failed to delete. Please try again!";
            }
            echoRespnse(200, $response);
        });

public function deletePlace($user_id, $place_id) {
        $stmt = $this->conn->prepare("DELETE p FROM places p, user_places up WHERE p.id = ? AND up.place_id = p.id AND up.user_id = ?");
        $stmt->bind_param("ii", $place_id, $user_id);
        $stmt->execute();
        $num_affected_rows = $stmt->affected_rows;
        $stmt->close();
        return $num_affected_rows > 0;
    }

1 个答案:

答案 0 :(得分:0)

DELETE p FROM places p似乎是SQL中的语法错误。

$stmt = $this->conn->prepare(
    "DELETE FROM places p, user_places up WHERE p.id = ? AND up.place_id = p.id AND up.user_id = ?"
);

为了避免将来遗漏这些错误,我建议将错误模式设置为PDO::ERRMODE_EXCEPTION

$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);