我的for循环更新数据库无法正常工作

时间:2015-04-05 12:39:58

标签: php session

我有一个会话数组,即:$_SESSION['cart_array'],其中包含以下项目

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

我的数据库插入项目是

item_id   unit_price   quantity
qwerty      500          4
skjbm       100          3

我想用当前会话变量更新其尊重的item_id的数量值,我的forloop是

$id = $_SESSION['cart_array'];//my current session array items
$ta= count($id);//no of items in session
for($i=0;$i < $ta;$i++){
 $item_id= $id[$i]['item_id'];///got index [] item_id value
 $quantity= $id[$i]['quantity'];///got index [] quantity value
 $qry = ("SELECT * FROM product_added WHERE item_id='$item_id'");  ///Selecting from your products table where the item_id to matche curent item_id

    $result = mysql_query($qry);
    $row = mysql_fetch_array($result);
         $qty = $row['quantity']; // Original Quantity value of current item_id
        if ($qty !== $quantity){
            $sql_update=("UPDATE product_added SET quantity='$quantity' WHERE item_id='$item_id' "); 
            echo "updated";

           // If the quantity is not the same as the previous quantity, do something.
         };


    }

我的输出是

updatedupdated

但问题是在数据库中它没有更新为什么...在哪一点我出错了建议我并提前重写代码...

4 个答案:

答案 0 :(得分:0)

您似乎忘记了mysql_query(),因此它永远不会执行更新。

$sql_update=mysql_query("UPDATE product_added SET quantity='$quantity' WHERE item_id='$item_id' ");

答案 1 :(得分:0)

使用此代码

    if ($qty !== $quantity){
        $sql_update=("UPDATE product_added SET quantity='$quantity' WHERE item_id='$item_id' "); 
        $result=mysql_query($sql_update);
        if(!$result){
          echo mysql_error();
         }
        else
        echo "updated";

       // If the quantity is not the same as the previous quantity, do something.
     };

答案 2 :(得分:0)

$sql_update=("UPDATE product_added SET quantity='$quantity' WHERE item_id='$item_id' "); 
            echo "updated";

这不是一个查询。您实际上必须使用mysql_query($ sql_update);

我还要补充说,不推荐使用mysql,而选择mysqli。

答案 3 :(得分:0)

重写您的代码如下:

$id = $_SESSION['cart_array'];//my current session array items
$ta= count($id);//no of items in session
for($i=0;$i < $ta;$i++){
    $item_id= $id[$i]['item_id'];///got index [] item_id value
    $quantity= $id[$i]['quantity'];///got index [] quantity value
    $qry = ("SELECT * FROM product_added WHERE item_id='$item_id'");  ///Selecting from your products table where the item_id to matche curent item_id

   $result = mysql_query($qry);
   $row = mysql_fetch_array($result);
   $qty = $row['quantity']; // Original Quantity value of current item_id
    if ($qty !== $quantity){
        $sql_update=("UPDATE product_added SET quantity='$quantity' WHERE item_id='$item_id' "); 

        $sql_query = mysql_query($sql_update);

        if(mysql_affected_rows() > 0){
           echo "updated"; //update operation succeeded
        }
        else {
          echo 'Updated Failed. Reason : '. mysql_error(); //update operation failed, print the error generated
        }

       // If the quantity is not the same as the previous quantity, do something.
     };
}