对两个不同的表使用两个insert语句

时间:2012-05-20 05:05:42

标签: php mysql

我想使用两个不同的插入语句和两个不同的表,例如

<?
mysql_query("INSERT INTO Customer (ID,Name,Subject, OrderDate) VALUES ('$ID', '$name', '$status', '$ODate')");
mysql_query("INSERT INTO Order (ID,Subject, Department, Status, OrderDate, Receive, Notes) VALUES ('$ID', '$status', 'Financial', 'Financial Department', '$ODate', 'NO', 'Notes')");
?>

它只适用于第一个表,不适用于第二个表。

有人可以帮助解决这个问题吗?

由于

2 个答案:

答案 0 :(得分:2)

您需要检查错误:

<?php
$query1 = "INSERT INTO Customer (ID,Name,Subject, OrderDate) VALUES ('$ID', '$name', '$status', '$ODate')";
if(!mysql_query($query1)) {
  throw new Exception(mysql_error());
}

$query2 = "INSERT INTO Order (ID,Subject, Department, Status, OrderDate, Receive, Notes) VALUES ('$ID', '$status', 'Financial', 'Financial Department', '$ODate', 'NO', 'Notes')";
if(!mysql_query($query2)) {
  throw new Exception(mysql_error());
}

我猜你得到了一个错误,因为Order在MySQL中是reserved word,应该相应地进行转义:

$query2 = "INSERT INTO `Order` (ID,Subject, Department, Status, OrderDate, Receive, Notes) VALUES ('$ID', '$status', 'Financial', 'Financial Department', '$ODate', 'NO', 'Notes')";

在我看来,你似乎正在插入一个固定值作为主键 - 你确定这是你想要的吗?


正如我在评论中所说,你应该完全停止使用mysql_函数,而是使用MySQLi或PDO。

答案 1 :(得分:0)

首先感谢DCoder帮助我解决问题并建议我使用PDO和MySQLi。

问题在于表格名Order,当我用新名称替换它时,它运行正常。

我认为使用两个mysql_query存在问题,但事实并非如此。我使用的表名是MySQL中的保留字。

由于