【问题标题】:Deleting tables with mysqli_multi_query()使用 mysqli_multi_query() 删除表
【发布时间】:2018-07-14 05:40:43
【问题描述】:

我正在尝试删除我的主表“eleve”,同时删除其他主键在此表中的表。我在附加代码中尝试过,但出现错误:

(Erreur de syntaxe pr�s de 'from bource where ID_BOURCE = 1delete from class where ID_CLASS = 1delete from 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);

  }
}

【问题讨论】:

  • 不要使用多查询,这几乎总是一个坏主意
  • @rtfm 好的,我将为每个表进行单个查询
  • 这应该可以,您只是忘记了每个查询末尾的;。例如:"delete from eleve where CIN_ELEVE = '$InputCIN';"

标签: php mysql mysqli mysqli-multi-query


【解决方案1】:

不知道你为什么要这样做,有更好的方法,但要回答你的问题,这样做:

$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);

还记得清除结果,否则您将无法执行另一个查询,但我认为 delete 不会返回结果。

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

还要记住,如果一个查询失败,其余的都不会运行。因此,要进行调试,请先尝试单独运行每个查询,并确保它一切正常,因为它的删除语句会在运行查询之前备份您的数据库,并在需要时恢复。

【讨论】:

  • 感谢它现在运行良好,您能提出更好的方法吗?
  • 我会建议使用 PDO,或者如果你对类不太擅长,请确保你实现了 mysqli_stmt_bind_param();请参阅:secure.php.net/manual/en/mysqli-stmt.bind-param.php 如果注入删除语句可能会致命,因此请采取所有必要步骤来保护您的数据库
  • 我真的很感激,我会检查一下
  • @medo100000S 多查询是危险的。带有字符串插值的多重查询从根本上和严重的缺陷。如果恶意用户能够找到将$NUM_INSCRIPTION 的值设置为0; DROP TABLE inscription; 之类的字符串的方法,那么您应该能够看到这里可能出现的严重错误。
  • @Michael-sqlbot 你是对的人,我曾经在每个请求中利用单个查询
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2020-07-05
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2019-07-17
  • 2020-05-21
相关资源
最近更新 更多