我有一个包含许多表的数据库。在第1面,我有一个按钮,将我传到第2侧。在第2面,我希望代码删除每个命名表中的每一行,这些行具有我存储在变量中的特定pid
。我在第2面有一个代码,但它没有做任何事情。
第2面的代码:
$pid3 = $_POST['pid2'];
$del = "DELETE FROM answer_det WHERE pid='$pid3'";
$del .= "DELETE FROM project WHERE pid='$pid3'";
$del .= "DELETE FROM question WHERE pid='$pid3'";
$del .= "DELETE FROM respondent WHERE pid='$pid3'";
$del .= "DELETE FROM result WHERE pid='$pid3'";
$del .= "DELETE FROM users WHERE pid='$pid3'";
$run = mysqli_query($mysqli,$del);
if($run)
{
echo "<h1>Project is deleted!</h1>";
}
答案 0 :(得分:4)
$pid3 = $_POST['pid2'];
$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";
$del .= "DELETE FROM users WHERE pid='$pid3';";
$run = mysqli_multi_query($mysqli,$del);
if($run)
{
echo "<h1>Project is deleted!</h1>";
}
答案 1 :(得分:1)
您可以考虑更改存在pid
的mysql表,以将该字段作为在删除时级联的外键。像
ALTER TABLE `answer_det`
ADD FOREIGN KEY (pid)
REFERENCES table_where_pid_is_the_primary_key (pid)
ON DELETE CASCADE;
将answer_det
替换为其他表名,以便在那里创建密钥。如果您无权更改表格,那么您的查询可能会失败,因为您缺少分号来分隔它们:
$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";
$del .= "DELETE FROM users WHERE pid='$pid3';";
答案 2 :(得分:0)
或者,你可以运行一个带有连接的查询,如下所示:
$pid3 = $_POST['pid2'];
$del = "DELETE a,b,c,d,e,f FROM answer_det a "
$del .= "JOIN project b on b.pid=a.pid "
$del .= "JOIN question c on c.pid=a.pid "
$del .= "JOIN respondent d on d.pid=a.pid "
$del .= "JOIN result e on e.pid=a.pid "
$del .= "JOIN users f on f.pid=a.pid "
$del .= "WHERE a.pid='$pid3'";
$run = mysqli_query($mysqli,$del);
if($run)
{
echo "<h1>Project is deleted!</h1>";
}
答案 3 :(得分:0)
简化您的查询,如下所示
$pid3 = $_POST['pid2'];
$del= DELETE FROM answer_det 't1',project 't2',question 't3' USING t1, t2,t3 WHERE t3.pid = t2.pid and t1.pid = t2.pid AND t1.pid = $pid3
$run = mysqli_query($mysqli,$del);
if($run)
{
echo "<h1>Project is deleted!</h1>";
}
答案 4 :(得分:0)
There是一个很好的解决方案:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
答案 5 :(得分:-1)
您在查询结尾处缺少分号。你需要把&#39 ;;&#39;在每次查询之后,为了一起执行多个查询。
试试这个:
$pid3="1";
$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";
$del .= "DELETE FROM users WHERE pid='$pid3';";
echo $del