0

我假设我要这样做是完全错误的方式,有人可以帮忙吗?

我想从一个表中取出所有具有特定产品编号的行,并在销售时将其放入另一个表中。

提前致谢。

$addinfo1 ="";
    //Connect to the database through our include
include_once "connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM tm_interested_buyers WHERE fk_product_id='$productid");

while($row = mysql_fetch_array($sql)){
$interestedbuyersid = $row["pk_interested_buyers_id"];
$fkproductid = $row["fk_product_id"];
$fkcustomerid = $row["fk_customer_id"];

    $addinfo1 .= mysql_query("INSERT INTO tm_int_buyers_complete (fk_interested_buyers_id, fk_product_id, fk_customer_id) VALUES('$interestedbuyersid','$fkproductid','$fkcustomerid')") or die (mysql_error());
echo $addinfo1;
}
4

3 回答 3

1

您只需要一个查询:

INSERT INTO tm_int_buyers_complete (fk_interested_buyers_id, fk_product_id, fk_customer_id) 
SELECT pk_interested_buyers_id, fk_product_id, fk_customer_id 
   FROM tm_interested_buyers
   WHERE fk_product_id='$productid'

有了这个选择语句(第 2 行及以下)是您的原始选择,具有您想要指定的值。这允许您将所有选定的行插入到您的表格中(顶行)。

于 2012-06-13T19:23:23.587 回答
0

仅使用一个查询 INSERT from SELECT:

INSERT INTO `tm_int_buyers_complete` (fk_interested_buyers_id, fk_product_id,     fk_customer_id)
  SELECT fk_interested_buyers_id, fk_product_id, fk_customer_id
  FROM `tm_interested_buyers` WHERE fk_product_id='$productid";
于 2012-06-13T19:24:16.737 回答
0

使用插入选择。

这要容易得多。

INSERT INTO tm_int_buyers_complete (`fk_interested_buyers_id`, `fk_product_id`,
`fk_customer_id`) SELECT tm_interested_buyers.pk_interested_buyers_id,
tm_interested_buyers.fk_product_id, tm_interested_buyers.fk_customer_id
FROM tm_interested_buyers WHERE tm_interested_buyers.fk_product_id='$productid'
于 2012-06-13T19:28:40.643 回答