对两个不同的表使用两个插入语句


Using two insert statements for two different tables

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

<?
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')");
?>

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

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

谢谢

您需要检查错误:

<?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());
}

我猜你收到一个错误,因为它是MySQL中的保留字Order应该相应地转义:

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

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


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

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

问题出在表名Order,当我用新名称替换它时,它工作正常。

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

谢谢