使用代码为foreach提供的参数无效

时间:2015-07-16 12:59:53

标签: php

下面的代码为我提供了一个为foreach提供的无效参数。请帮忙

if(isset($_POST['update_cart'])) {  
    $i = 0;

    foreach ($_SESSION["qty"] as $each_item) { 
        $item_id = $each_item['qty'];

        $update_qty = mysqli_query("update cart set qty='$qty' LIMIT 1");

        while ($row = mysql_fetch_array($update_qty)) {
            $_SESSION['qty']=$qty;
        }
        $total = $total * $each_item['qty'];
    }
}

1 个答案:

答案 0 :(得分:0)

关于此类错误的许多问题。首先是警告而不是错误,因此您的代码将继续正常工作。您可以使用error_reporting(E_ERROR)来抑制,这只会显示致命的错误。否则在您的代码中如果您不想看到此错误,则需要执行类似的操作。

//only try loop if there is an array
if(is_array($_SESSION["qty"])){ 
  foreach ($_SESSION["qty"] as $each_item) { 
    $item_id = $each_item['qty'];

    $update_qty = mysqli_query("update cart set qty='$qty' LIMIT 1");
    //change to mysqli_fetch_array!!
    while ($row = mysqli_fetch_array($update_qty)) {
        //you will only get one row in here like this
        $_SESSION['qty']=$qty; //maybe do $_SESSION['qty'][]=$qty;
    }
    $total = $total * $each_item['qty'];
  }
}else{
   echo 'No array to be found here';
}