会话PHP的未定义偏移量

时间:2015-10-08 15:18:59

标签: php

我试图从购物车中保存多个订单,但似乎我使用的代码不太正确。我从会话中获取值。我还在购物车上添加了其他食物。

继承我的错误

  

注意:未定义的偏移量:0英寸   第53行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移量:0英寸   第54行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移量:0英寸   第55行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移量:0英寸   第56行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移:1英寸   第53行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移:1英寸   第54行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移:1英寸   第55行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移:1英寸   第56行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移量:2英寸   第53行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移量:2英寸   第54行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移量:2英寸   第55行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php

     

注意:未定义的偏移量:2英寸   第56行的C:\ xampp \ htdocs \ project \ mrpl123 \ chart.php谢谢!您的   已下订单!

这是我的代码

    <?php
    session_start();
    //mysql_connect("localhost", "root", "") or die(mysql_error());
////select database
//mysql_select_db("rapal") or die(mysql_error());
    require_once("dbcon.php");
    $db_handle = new DBController();
    if(!isset($_SESSION['user']))
    {
     header("Location: login.php");
    }
    $user = $_SESSION['user'];
    $res=mysql_query("SELECT * FROM members_account WHERE uid='$user'") or die(mysql_error());
    $userRow=mysql_fetch_array($res);


    if(!empty($_GET["action"])) {
    switch($_GET["action"]) {
        case "add":
            if(!empty($_POST["quantity"])) {
                $productByCode = $db_handle->runQuery("SELECT * FROM menus WHERE code='" . $_GET["code"] . "'");
                $itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["food_name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["food_price"]));

                if(!empty($_SESSION["cart_item"])) {
                    if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
                        foreach($_SESSION["cart_item"] as $k => $v) {
                                if($productByCode[0]["code"] == $k)
                                    $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                        }
                    } else {
                        $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                    }
                } else {
                    $_SESSION["cart_item"] = $itemArray;
                }
            }
        break;
        case "remove":
            if(!empty($_SESSION["cart_item"])) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($_GET["code"] == $k)
                            unset($_SESSION["cart_item"][$k]);              
                        if(empty($_SESSION["cart_item"]))
                            unset($_SESSION["cart_item"]);
                }
            }
        break;
        case "checkout":
        $member=$_POST['namex'];

            $max=count($_SESSION['cart_item']);
            for($i=0;$i<$max;$i++){
                $pname=$_SESSION['cart_item'][$i]['name'];
                $pid=$_SESSION['cart_item'][$i]['code'];
                $q=$_SESSION['cart_item'][$i]['quantity'];
                $price=$_SESSION['cart_item'][$i]['price'];
                mysql_query("insert into checkout_records values ('',$member,$pname,$pid,$q,$price,'In process')");
            }
            echo 'Thank You! your order has been placed!'; 
    //    $code5 = $_POST['code'];
    //    $food_qty = $_POST['qty'];
    //    $price = $_POST['price'];
    //    $data = $_SESSION['cart_item'];
    //    $result = mysql_query ("INSERT INTO checkout_records(member,food_order,code,food_qty,food_price,status) VALUES ('$member','".$data['name']."','".$data['code']."','".$data['quantity']."','".$data['price']."','In process')");
    //    
    //  if($result && mysql_num_rows($result)>0) {
    //// all ok
    //      echo "inserted";
    //    }
    //    else {
    //        echo "not inserted";
    //    }
    //    $query1 = "INSERT INTO checkout_records (member,food_order,code,food_qty,food_price,status)VALUES('$member','$food_order','$code5','$food_qty','$price','In process')";
    //
    ////if (mysql_query($query1)) {
    ////        //dislay a message box that the saving is successfully save
    ////        $uid = mysql_insert_id(); // last inserted id
    ////        $result = mysql_query("SELECT * FROM checkout_records WHERE id = '$uid'");
    ////        echo "<script type=\"text/javascript\">
    ////                    alert(\"Thank You!\");
    ////                    window.location = \"chart.php?action=empty\"
    ////                    </script>";
    ////        } else {
    ////            echo "Failed: " . mysql_error();
    ////        }

        break;
        case "empty":
            unset($_SESSION["cart_item"]);
        break;  
    }
    }
    ?>

更新会话阵列输出

Array ( [user] => 55f0497487b335.17059914 [cart_item] => Array ( [main1] => Array ( [name] => adobo [code] => main1 [quantity] => 200 [price] => 1 ) [main3] => Array ( [name] => asado [code] => main3 [quantity] => 1 [price] => 1 ) [main2] => Array ( [name] => afritada [code] => main2 [quantity] => 123 [price] => 1 ) ) [cart] => Array ( [0] => Array ( [productid] => 1 [qty] => 1 ) [1] => Array ( [productid] => 2 [qty] => 1 ) [2] => Array ( [productid] => 3 [qty] => 1 ) ) )

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

我的错误是我使用for loop代替foreach感谢Anurag Verma :)现在正在使用