简体   繁体   English

MYSQL INSERT循环-使用PHP

[英]MYSQL INSERT Loop - With PHP

I am assuming I am going about this is the completely wrong fashion, can anyone assist? 我以为这是完全错误的方式,有人可以协助吗?

I want to take all the rows with a certain product number out of one table and put it in another table when it sells. 我想从一个表中取出具有特定产品编号的所有行,然后在销售时将其放入另一个表中。

Thanks in advance. 提前致谢。

$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;
}

You just need the one query: 您只需要一个查询:

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'

With this the select statement (2nd line and down) is your original select with the values you want specified. 这样,select语句(第二行和第二行)便是您最初选择的内容,其中包含您要指定的值。 This allows all your selected lines to be inserted into your table (top line). 这允许您将所有选定的行插入到表格中(顶行)。

Use only one query INSERT from SELECT: 仅使用一个查询从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";

Use INSERT INTO SELECT. 使用INSERT INTO SELECT。

It is much easier. 这要容易得多。

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM