如何将多维数组插入数据库?

How to insert multidimensional array into database?

我的数组即 $_SESSION['cart_array'] 包含

Array ( [0] => Array ( [item_id] => qwerty [quantity] => 2 [unit_price] => 500 ) [1] => Array ( [item_id] => skjbm [quantity] => 3 [unit_price] => 100 ) ) 

要插入到我的数据库代码中的代码是

 foreach($_SESSION['cart_array'] as $each_item){ $sql=mysql_query("INSERT INTO product_added(id,ip_address,order_id,email,item_id,unit_price,quantity,total,pay_status)values('','','','','".$each_item['item_id']."','".$each_item['quantity']."','".$each_item['unit_price']."','','')");
 if(!mysql_query( $sql)){
    // maybe not the best use of `die` here?
    die('Error: ' . mysql_error());
}echo "record added"; }

我的问题是当我 运行 脚本时它只添加了一项,即:item_id=qwerty,quantity=2 和 unit_price=500 到 table我在 $_SESSION['cart_array'].

中有两个项目

并且mysql错误显示:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

如何将两个和多个项目输入数据库?

这样试试。顺便说一句 mysql 已弃用 mysqli

$insert_query = 'INSERT INTO product_added (id,ip_address,order_id,email,item_id,unit_price,quantity,total,pay_status) values ';

 foreach($_SESSION['cart_array'] as $each_item){ 

    $insert_query .= "('','','','','".$each_item['item_id']."','".$each_item['quantity']."','".$each_item['unit_price']."','',''),";        

 }

$query = rtrim($insert_query, ',');
if(!mysql_query($query)){
    die('Error: ' . mysql_error());
}else{
    echo "record added";
}