-3
我怎樣才能在一行中插入一個數組到數據庫之間用逗號插入數據庫陣列作爲一個行
如何在每個元素
prod_id | user_id | quantity
1111 | 5 | 123
1234 | 5 | 11
234 | 5 | 1
,如果我有這樣的表,要選擇PROD_ID爲每個用戶並將其插入另一個表中,因爲
prodid qun userid
1111,1234,234 123,11,1 5
我該如何得到這個結果?
我嘗試使用json,但它返回一個數組並沒有存儲在數據庫中。
<?php
$sql = "select * from cart where userid=5";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$prodid = $row['proid'];
$qun = array($row['qun']);
$prod[] = array('prodid' => $prodid);
$quantity[]=array('$qun' => $data);
}
$quntatity= json_decode(json_encode($qun));
$products= json_decode(json_encode($prod));
$sql = "insert into products(userid,products ,quantity) values('5',$products','$quntatity')";
mysql_query($sql) or die(mysql_error()) ;
?>
的可能重複[在一個SQL查詢中插入多行?(http://stackoverflow.com/questions/452859/inserting-multiple- rows-in-a-single-sql-query) –
使用至少MySQLi或PDO的安全 –
@SidiaStudios否我有用戶添加更多的產品我想插入所有產品ID在數據庫中的一行中,當我想檢索它我可以 – Rabab