Php 从Simplecart数组创建MySQL Insert语句
我已经实现了Simplecart,正在将购物车的内容传递到另一个页面,在那里我想将产品标识符与数量和唯一标识符一起保存到MySQL数据库表中,这样我就可以根据该标识符提取所需的产品和数量。Simplecart正在将以下信息发送到我的页面Php 从Simplecart数组创建MySQL Insert语句,php,mysql,simplecart,Php,Mysql,Simplecart,我已经实现了Simplecart,正在将购物车的内容传递到另一个页面,在那里我想将产品标识符与数量和唯一标识符一起保存到MySQL数据库表中,这样我就可以根据该标识符提取所需的产品和数量。Simplecart正在将以下信息发送到我的页面 Array ( [currency] => USD [shipping] => 0 [tax] => 0 [taxRate] => 0 [itemCount] => 5 [item_name_1] => ProductA [
Array ( [currency] => USD [shipping] => 0 [tax] => 0 [taxRate] => 0 [itemCount] => 5
[item_name_1] => ProductA [item_quantity_1] => 1 [item_options_1] => identifier: 0057
[item_name_2] => ProductB [item_quantity_2] => 3 [item_options_2] => identifier: 0024
[item_name_3] => ProductC [item_quantity_3] => 1 [item_options_3] => identifier: 0059
[item_name_4] => ProductD [item_quantity_4] => 1 [item_options_4] => identifier: 0106
[item_name_5] => ProductE [item_quantity_5] => 1 [item_options_5] => identifier: 1031 )
我将这里看到的各种脚本拼凑在一起,生成了如下打印的sql语句
insert into products values (51ca3d6e580c6,1,identifier: 0057)
insert into products values (51ca3d6e580c6,3,identifier: 0024)
insert into products values (51ca3d6e580c6,1,identifier: 0059)
insert into products values (51ca3d6e580c6,1,identifier: 0106)
insert into products values (51ca3d6e580c6,1,identifier: 1031)
这至少告诉我,我已经创建了一个循环,每次都填充了正确的数据,但是当我尝试在每次循环时插入记录时,我只得到最后一次迭代。我也不明白如何在循环中分解Item_Options变量
下面是乱七八糟的代码
$content = $_POST;
$item_number = array();
$item = array();
for($i=1; $i < $content['itemCount'] + 1; $i++)
{
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$options = 'item_options_'.$i;
$item_number['total'] = $i;
$item[$i]['name'] = $content[$name];
$item[$i]['quantity'] = $content[$quantity];
$item[$i]['options'] = $content[$options];
}
$total = $item_number['total'];
$line = 0;
while ($line <= $total -1)
{
$line++;
$statement = $myid . "," . $item[$line]['quantity'] . "," . $item[$line]['options'];
$sql = "insert into products values ($statement)";
$result=mysql_query($sql);
}
mysql_close();
$content=$\u POST;
$item_number=array();
$item=array();
对于($i=1;$i<$content['itemCount']+1;$i++)
{
$name='item_name.'$i;
$quantity='项目数量.$i;
$options='item_options_uu.$i;
$item_number['total']=$i;
$item[$i]['name']=$content[$name];
$item[$i]['quantity']=$content[$quantity];
$item[$i]['options']=$content[$options];
}
$total=$item_number['total'];
$line=0;
而($line尝试-
然后在内爆()
上,查询变为-
insert into products values (id,qty,product),(id,qty,product),...
编辑
根据您的编辑,尝试-
for($i=1; $i < $content['itemCount'] + 1; $i++){
$quantity = 'item_quantity_'.$i;
$options = 'item_options_'.$i;
$item_number['total'] = $i;
$exploded = explode(' ', $content[$options]); // need to use the $content[] here
$item_id = $exploded[1];
$item[$i]['quantity'] = $content[$quantity];
$item[$i]['options'] = $item_id; // use the exploded value, not the $content[]
}
$total = $item_number['total'];
$line = 0;
while ($line <= $total -1){
$line++;
$statement[] = "('".$myid . "','" . $item[$line]['quantity'] . "','" . $item[$line]['options'] ."')";
}
for($i=1;$i<$content['itemCount']+1;$i++){
$quantity='项目数量.$i;
$options='item_options_uu.$i;
$item_number['total']=$i;
$explode=explode(“”,$content[$options]);//需要在此处使用$content[]
$item_id=$exploded[1];
$item[$i]['quantity']=$content[$quantity];
$item[$i]['options']=$item\u id;//使用分解值,而不是$content[]
}
$total=$item_number['total'];
$line=0;
虽然我很感谢Michael,但我不知道你是如何在这里添加一个长评论的,所以我在下面问了另一个问题。。。
insert into products values (id,qty,product),(id,qty,product),...
for($i=1; $i < $content['itemCount'] + 1; $i++){
$quantity = 'item_quantity_'.$i;
$options = 'item_options_'.$i;
$item_number['total'] = $i;
$exploded = explode(' ', $content[$options]); // need to use the $content[] here
$item_id = $exploded[1];
$item[$i]['quantity'] = $content[$quantity];
$item[$i]['options'] = $item_id; // use the exploded value, not the $content[]
}
$total = $item_number['total'];
$line = 0;
while ($line <= $total -1){
$line++;
$statement[] = "('".$myid . "','" . $item[$line]['quantity'] . "','" . $item[$line]['options'] ."')";
}