【发布时间】:2015-06-08 18:15:19
【问题描述】:
我的数组,即 $_SESSION['cart_array'] 包含:
Array ( [0] => Array ( [item_id] => abc123 [quantity] => 2 [unit_price] => 500 ) [1] => Array ( [item_id] => def456 [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 and unit_price=500
到表,因为我在$_SESSION['cart_array'] 中有两个项目。并且mysql错误显示:
错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“1”附近使用正确的语法
如何将两个或多个项目输入数据库?
【问题讨论】:
-
这个问题非常广泛......到目前为止你有什么 php 代码?你试过什么?你在使用什么库?你的数据库连接信息是什么?
标签: php jquery session multidimensional-array