使用mysqli_multi_query()删除表

时间:2018-02-04 03:29:50

标签: php mysql mysqli mysqli-multi-query

我试图删除我的原则表' eleve',删除其他表中有其主键的表。我尝试了附加的代码,但是我收到了一个错误:

  

(Erreur de syntaxepr�sde' from bource,其中ID_BOURCE = 1delete from class,其中ID_CLASS = 1delete来自p'�la ligne 1)

有什么想法吗?

if (isset($_POST['butAj4'])) {
  $queryDel = "delete from inscription where NUM_INSCRIPTION = $NUM_INSCRIPTION";
  $queryDel. = "delete from bource where ID_BOURCE = $ID_BOURCE";
  $queryDel. = "delete from class where ID_CLASS = $ID_CLASS";
  $queryDel. = "delete from project where ID_PROJECT = $ID_PROJECT";
  $queryDel. = "delete from annee_scolaire where ID_ANNEE = $ID_ANNEE";
  $queryDel. = "delete from eleve where CIN_ELEVE = '$InputCIN'";

  if (mysqli_multi_query($con, $queryDel)) {
    $msg3 = "<div class='alert alert-success'>Bien suppression</div>";
  } else {
    $msg3 = "<div class='alert alert-danger'>error dans la suppression</div>".mysqli_error($con);

  }
}

1 个答案:

答案 0 :(得分:2)

不确定为什么你想这样做有更好的方法,但回答你的问题是这样的:

$queryDel = "
delete from inscription where NUM_INSCRIPTION= $NUM_INSCRIPTION ;
delete from bource where ID_BOURCE = $ID_BOURCE ;
delete from class where ID_CLASS = $ID_CLASS ;
delete from project where ID_PROJECT = $ID_PROJECT ;
delete from annee_scolaire where ID_ANNEE = $ID_ANNEE ;
delete from eleve where CIN_ELEVE = '$InputCIN'; ";

$result=mysqli_multi_query($con,$queryDel);

并且还要记得清除结果,否则您将无法执行其他查询,但我不认为删除会返回结果。

while(mysqli_next_result($con)){;} //clear any remaining query results.

还要记住,如果一个查询失败,其他所有查询都将无法运行。所以调试尝试首先单独运行每个查询,并确保它一切正常,因为它的delete语句在运行查询之前备份了数据库,并在需要时恢复。