【问题标题】:Creating MySQL Insert statement from Simplecart array从 Simplecart 数组创建 MySQL 插入语句
【发布时间】:2013-06-23 00:03:56
【问题描述】:

我已经实现了 Simplecart 并将购物车的内容传递到另一个页面,我想将产品标识符连同数量和唯一标识符一起保存到 MySQL 数据库表中,以便我可以根据请求提取产品和数量在那个标识符上。 Simplecart 正在向我的页面发送以下信息..

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();

所以,我的问题确实是,我如何将唯一 ID、数量和(爆炸的)标识符添加到 MySQL 中,每次循环时创建一条新记录?

【问题讨论】:

  • 谢谢迈克尔,我不知道你如何在这里添加长评论,所以我在下面问了另一个问题......

标签: php mysql simplecart


【解决方案1】:

试试-

while ($line <= $total -1){
    $line++;
    $statement[] = "(".$myid . "," . $item[$line]['quantity'] . "," . $item[$line]['options'].")";
}

$sql = "insert into products values ".implode(",",$statement);
$result=mysql_query($sql)

这将创建一个 $statement 数组,其值类似于

(id,qty,product)

然后在implode() 上查询变为-

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'] ."')";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-05
    • 2022-11-20
    • 2019-02-24
    • 2019-12-24
    • 2012-12-13
    • 1970-01-01
    • 2018-09-25
    • 2015-10-25
    相关资源
    最近更新 更多