douyun4524 2017-07-20 09:56
浏览 135
已采纳

DELETE查询的SQL语法错误

I'm working on a website connected to a database managed by MySQL. These are the structures of tables pagamenti and prenotazione:

Pagamenti

table_pagamenti

Prenotazione

table_prenotazione

In my PHP code I want to delete a record from both tables using the field IDPrenotazione. I could use two different queries, as in the following code

$query1 = "DELETE FROM pagamenti WHERE IDPrenotazione='$ID'";
$query2 = "DELETE FROM prenotazione WHERE IDPrenotazione='$ID'";

but it would be much better if I use only one query. I've learned that in SQL language queries are separated by ;, so I tried this code

$query = "DELETE FROM pagamenti WHERE IDPrenotazione='$ID';
          DELETE FROM prenotazione WHERE IDPrenotazione='$ID'";

but it returns this error

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELETE FROM prenotazione WHERE IDPrenotazione='2017-0006'' at line 1

This is the complete code, with the execution

$query = "DELETE FROM pagamenti WHERE IDPrenotazione='$ID';
          DELETE FROM prenotazione WHERE IDPrenotazione='$ID'";
if (mysqli_query($connessione, $query))
{
    //code
}
else
{
    echo mysqli_error($connessione);
}

If I execute this code on phpMyAdmin it works as I want with no errors, so the query must be correct.

Why doesn't it work via PHP? How can I make it work?

  • 写回答

3条回答 默认 最新

  • donglan6777 2017-07-20 10:04
    关注

    Use mysqli_multi_query()

    The mysqli_multi_query() function performs one or more queries against the database. The queries are separated with a semicolon.

    $query = "DELETE FROM pagamenti WHERE IDPrenotazione='$ID';DELETE FROM prenotazione WHERE IDPrenotazione='$ID'";
    
    mysqli_multi_query($connessione, $query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题