【问题标题】:how to select session array and database item to check如何选择要检查的会话数组和数据库项
【发布时间】:2015-04-05 06:58:34
【问题描述】:

我有一个会话数组 ie.$_SESSION['cart_array'] 并且该数组包含

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

现在我使用我的 sql 查询将此数组插入到数据库中。我的查询是

$insert_query = 'INSERT INTO product_added (id,order_id,email,item_id,unit_price,quantity,total,pay_status) values ';foreach($_SESSION['cart_array'] as $each_item){ 

$insert_query .= "('','$OrderId','','".$each_item['item_id']."','".$each_item['unit_price']."','".$each_item['quantity']."','".$each_item['unit_price']*$each_item['quantity']."',''),";}$query = rtrim($insert_query, ',');if(mysql_query($query)){echo 1;}else{
echo 0;}

我的数据库入口记录是

item_id unit_price quantity
qwerty      500       1
skjbm       100       2

到现在还可以..... 现在用户更新现有 item_id 的数量,新数组是

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

如果购物车中发生任何变化,我如何检查数据库中现有的数据条目并更新特定 item_id 的数量。提前谢谢您

【问题讨论】:

    标签: php mysql arrays session


    【解决方案1】:

    在插入之前只是通过item id判断具体数据是否存在。然后数据不存在于表中并执行插入,否则执行更新操作。以下是我的简短代码供您参考。

    <?php
    
    $cart_array = $_SESSION['cart_array'];;
    $item_ids = array();
    if ($cart_array) {
      foreach ($cart_array as $k => $v) {
        $item_ids[$v['item_id']] = 1;
       }
     }
    
    //before insert do a check
    $qry = sprintf("SELECT * FROM product_added WHERE id in (%s)",          implode(',', $item_ids));
    $result = mysql_query($qry);
    $existing_item_ids = array();
    while ($row = mysql_fetch_array($result)) {
         $existing_item_ids[$row['item_id']] = 1;
    }
    $insrtion_arr = array();
    $updated_arr = array();
    
    if (!$existing_item_ids) { // if empty , the all cart data will insert into database
        $insrtion_arr = $item_ids;
      } else {
        foreach ($item_ids as $v) {
               if (isset($existing_item_ids[$v])) {
                   $updated_arr[$v] = 1;
               } else {
                  $insrtion_arr[$v] = 1;
             }
        }
      }
     //loop cart data and do database operation
     foreach ($cart_array as $k => $v) {
           if (isset($insrtion_arr[$v['item_id']])) {
                // do insert
            }   
           if (isset($updated_arr[$v['item_id']])) {
              // do update                                                                
            }   
        }
    

    希望对你有帮助!

    【讨论】:

    • 感谢您的宝贵回复,但现在它显示错误-: isset 中的非法偏移类型或第 46 行为空,我的第 46 行是:if (isset($insrtion_arr[$v])) ,, ,第二个错误 isset 中的非法偏移类型或第 50 行为空,我的第 50 行是:if (isset($updated_arr[$v]))..请解决这个问题
    • 对不起,你说的有错误,我重新编辑代码,请再试一次。 @priyabratasen
    • 建议我错在哪里我的代码是.....->$id = $_SESSION['cart_array'];$ta= count($id);for($i=0 ;$i
    • 我的 mysql 代码将仅将该 item_id 的数量更新到数据库中
    • 我在上面的 for 循环中的错误是什么,为什么它没有在数据库中更新
    猜你喜欢
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多