多个SQL语句通过PHP导致错误

时间:2012-07-13 18:45:20

标签: php mysql sql

我正在尝试创建一个php日历,作为项目的一部分,在这里我用php执行一些SQL:

mysql_select_db("waycov_dtm", $con);

$data="'Placeholder Text'";

$sql="INSERT INTO waycov_dtm.calDates (dateID, day, month, year)
VALUES (" . $dateID . ", " . $day . ", " . $month . ", " . $year . ");
INSERT INTO waycov_dtm.calData (dateID, data, memberID)
VALUES (" . $dateID . ", " . $data . ", 1);";

echo $sql;

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);

执行此操作时,我得到以下内容(回显的sql,然后是错误):

INSERT INTO waycov_dtm.calDates (dateID, day, month, year) VALUES (7072012, 7, 07, 2012); INSERT INTO waycov_dtm.calData (dateID, data, memberID) VALUES (7072012, 'Placeholder Text', 1);Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO waycov_dtm.calData (dateID, data, memberID) VALUES (7072012, 'Placeh' at line 3

如果我取出任何一个插入语句,另一个运行,但两者一起导致这个?

1 个答案:

答案 0 :(得分:4)

像这样使用:

$sql="INSERT INTO waycov_dtm.calDates (dateID, day, month, year)
VALUES (" . $dateID . ", " . $day . ", " . $month . ", " . $year . ")";
$sql_second = "INSERT INTO waycov_dtm.calData (dateID, data, memberID)
VALUES (" . $dateID . ", " . $data . ", 1);";

if (!mysql_query($sql,$con) || !mysql_query($sql_second,$con))
{
  die('Error: ' . mysql_error());
}

从手册:

  

mysql_query()发送一个唯一的查询(多个查询不是   支持)

不鼓励使用mysql_query。使用MySQLi或PDO。