使用PHP将多行插入mysql数据库


Using PHP to INSERT multiple rows into mysql DB

我正试图将数据插入到一个表中,而数据是从另一个表提取的。目前我的代码如下:

$result3 = mysql_query('SELECT order_no 
                    FROM orders 
                    WHERE ord_date = "' . ($_POST["ord_date"]) . '"');
while($row=mysql_fetch_array($result3)){ $order=$row['order_no'];}
$result4 = mysql_query('SELECT door_product_no 
                    FROM estimateDescribesDoorProduct 
                    WHERE estimate_no = "' . ($_GET["estimate_no"]) . '"');
while($row=mysql_fetch_array($result4)){ $door=$row['door_product_no'];}
$result5 = mysql_query('SELECT quantity 
                    FROM estimateDescribesDoorProduct 
                    WHERE estimate_no = "' . ($_GET["estimate_no"]) . '"');
while($row=mysql_fetch_array($result5)){ $dquantity=$row['quantity'];}

$sql2="INSERT INTO orderConsistsOfDoor (order_no, door_product_no, product_quantity)
VALUES ('$order','$door','$dquantity')";

多亏了这个网站上的一些建议,我昨天用了这个方法。我今天的问题是我需要插入多行。除了第一列(order_no/estimate_no)之外,表"orderConsistensOfDoor"answers"estimateDescribesDoorProduct"完全相同。基本上,如果一个估计(或订单)由例如3种产品组成,那么表中将有3行具有该估计_no(但不同的产品_no和数量)。

我认为我的代码只会在orderConsistensOfDoor中插入一行,但我需要它在estimate_no所在的每行插入($_GET["estimate_no["])。我认为这可以用foreach或其他东西来完成,但我从未使用过,也不知道它是如何工作的。

有人能帮我吗?

要用一个查询插入多条记录,可以执行以下操作:

INSERT INTO `table_name` (`foo`, `bar`) VALUES (1, 2), (3, 4), (5, 6);

参见INSERT语法

不过,你应该使用图书馆,现在是2012年了!