我使用内部联接运行此sql语句以从数据库中删除,但是我收到了太多错误
错误
致命错误:未捕获的异常' PDOException'与消息 ' SQLSTATE [42000]:语法错误或访问冲突:1064您有 SQL语法错误;查看与您的手册相对应的手册 MariaDB服务器版本,用于在&JOAH; INNER JOIN附近使用正确的语法 ipaddress_likes_map ON ipaddress_likes_map.postat = blog_post.BID INN' 在第3行'在C:\ xampp \ htdocs \ codejail.net_inc \ dbcontroller.php:57 堆栈跟踪:#0 C:\ XAMPP \ htdocs中\ codejail.net_inc \ dbcontroller.php(57): PDOStatement-> execute()#1 C:\ XAMPP \ htdocs中\ codejail.net \用户\ deleteaccount.php(88): DBController-> execute()#2 {main}引入 第57行的C:\ xampp \ htdocs \ codejail.net_inc \ dbcontroller.php
这是我的代码
<?php
if(isset($_POST['delete'])){
$pass = $_POST['pasyske'];
$userkey = $_POST['username'];
$db_delete = new DBController();
$db_delete->prepare("DELETE
FROM blog_post
INNER JOIN ipaddress_likes_map ON ipaddress_likes_map.postat = blog_post.BID
INNER JOIN flagpost ON flagpost.postId = blog_post.BID
INNER JOIN postviewmap ON postviewmap.viewpostId = blog_post.BID
INNER JOIN replys ON replys.rid = blog_post.BID
INNER JOIN votepoint_map ON votepoint_map.postlike_id = blog_post.BID
WHERE blog_post.UserName = :alluserpost");
$db_delete->bind(":alluserpost", $userkey);
//$db_delete->bind(":password", $encrypt_password);
$db_delete->execute();
$pdeleted = $db_delete->getAll();
$db_delete->free();
if($pdeleted){
print_r($pdeleted);
}
}?>
我确定我的数据库控制器没问题,因为当我运行单个删除语句时它工作得很好
答案 0 :(得分:0)
DELETE之后你应该知道从哪个表中删除行。试试这个问题:
$db_delete->prepare("DELETE blog_post
FROM blog_post
INNER JOIN ipaddress_likes_map ON ipaddress_likes_map.postat = blog_post.BID
INNER JOIN flagpost ON flagpost.postId = blog_post.BID
INNER JOIN postviewmap ON postviewmap.viewpostId = blog_post.BID
INNER JOIN replys ON replys.rid = blog_post.BID
INNER JOIN votepoint_map ON votepoint_map.postlike_id = blog_post.BID
WHERE blog_post.UserName = :alluserpost");
答案 1 :(得分:0)
为每个表添加一个别名,并在单词DELETE
后面引用它。另外,使用LEFT JOIN
而不是INNER JOIN
,因为如果某些连接的表没有匹配的行,我猜你仍然希望删除工作事件。
db_delete->prepare("DELETE bp, i, f, p, r, v
FROM blog_post AS bp
LEFT JOIN ipaddress_likes_map AS i ON i.postat = bp.BID
LEFT JOIN flagpost AS f ON f.postId = bp.BID
LEFT JOIN postviewmap AS p ON p.viewpostId = bp.BID
LEFT JOIN replys AS r ON r.rid = bp.BID
LEFT JOIN votepoint_map AS v ON v.postlike_id = bp.BID
WHERE bp.UserName = :alluserpost");