[英]How to insert two different array values into a table in a single query
我想在一個查詢中將兩個數組的值插入表中。 有可能做那樣的事情。
表格式
id | product_id | category_id | blog_id | updated_by | created_date
類別_id
Array
(
[0] => 4
[1] => 15
[2] => 18
)
PRODUCT_ID
Array
(
[0] => 2260
[1] => 1401
)
Blog id = mysql_insert_id();
結果
id | product_id | category_id | blog_id | updated_by
1 2260 4 15 xyz
2 1401 15 15 xyz
3 null 18 15 xyz
對我的任何建議也是為了改善這個更好的插入查詢。
INSERT INTO `Table_Name` (`product_id`, `category_id`, `blog_id`, `updated_by`) VALUES (2260, 4, 15, 'xyz'), (1401, 15, 15, 'xyz'), (, 18, 15, 'xyz');
我假設id
列是一個自動遞增的列。
您可以在現在使用的同一查詢中一次插入多行。 只需添加一個逗號,並輸入更多值:
Insert into myTable values ('field1-var1','field2-var2'), ('field1-var2','field2-var2') ..
$combain = array_merge($cat , $proid);
for($i = 0; $i <count($combain); $i++ )
{
if(isset($proid[$i])) {
$product_id = $proid[$i];
}
else{
$product_id = "''";
}
if(isset($cat[$i])){
$category_id = $cat[$i];
}
else{
$category_id = "''";
}
if(!empty($cat[$i]) || !empty($proid[$i])) {
@$values[] ="('',". $blogid.",".$category_id.",".$product_id.","."'".$updated_by."'".",now())";
}
}
$query = $this->db->query("insert into blog_details (id , blog_id , cat_id, pro_id, updated_by , created_date) values" . implode(',', $values));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.