Php 错误消息出现在实时服务器上,而不是本地主机上:为foreach()提供的参数无效

Php 错误消息出现在实时服务器上,而不是本地主机上:为foreach()提供的参数无效,php,class,cookies,Php,Class,Cookies,我有一些代码在localhost上正确运行,但在服务器上显示错误消息: 警告:为foreach()提供的参数无效 如果我单击Addtobasket,我的代码首先检查它是否在cookie中,它是否没有将id添加到数组中如果它不在cookie中,则产品的id添加到数组的末尾,并设置cookie 这是我在课堂上的代码: <?php class Cart { //Add a product order to our COOKIE/JSON based cart. public

我有一些代码在localhost上正确运行,但在服务器上显示错误消息:

警告:为foreach()提供的参数无效

如果我单击Addtobasket,我的代码首先检查它是否在cookie中,它是否没有将id添加到数组中如果它不在cookie中,则产品的id添加到数组的末尾,并设置cookie

这是我在课堂上的代码:

<?php
class Cart
{

    //Add a product order to our COOKIE/JSON based cart.

    public function AddProduct($productid, $quantity)
    {
        $x=0;
        //Checks to see if cart already exists.

        if (isset($_COOKIE['Cart']))
        {
            //JSON content is turned back into array.
            $cart = json_decode($_COOKIE['Cart']);
            foreach ($cart as $order => $product)
            {
                //Check to see if $products key ($order) matches the $products key we wish to remove.
                if($product[0] == $productid)
                {
                    //If result is false add product order to new array.
                    $record=$order;
                    $x=1;
                }
            }
            if($x==1){

                }else{
                    $cart[] = array($productid,$quantity);

                    //Encode cart array back into JSON.

                    $createcart = json_encode($cart);

                    setcookie('Cart',$createcart,time()+3600);
                }
        }
        else
        {
            $cart = array();
            $cart[] = array($productid,$quantity);
            //Encode cart array back into JSON.
            $createcart = json_encode($cart);
            setcookie('Cart',$createcart,time()+3600);
        }

        //Add product id and quantity desired to cart.
    }

    //Displays our Cart to the page.

    public function DisplayCart()
    {

        if (isset($_COOKIE['Cart']))
        {
            $cart = $_COOKIE['Cart'];

            //JSON content is turned back into array.

            $products = json_decode($cart);

            //Loop through Cart array to display relevant Cart info.
            //print_r($products);
            foreach($products as $order => $product)
            {
                $myarr[]=$product[0];

                //echo '<p>';
                //echo $order;
                //echo $product[0];
                //echo $product[1];
                //echo '</p>';
            }
            return $myarr;
        }
        else
        {
            echo 'No Products In Cart';
        }

    }

    //Remove product order from Cart.



    public function RemoveProduct($orderid)
        {

            $cart = $_COOKIE['Cart'];

            //JSON content is turned back into array.

            $products = json_decode($cart);

            $newcart = array();

            foreach ($products as $order => $product)
            {

                //Check to see if $products key ($order) matches the $products key we wish to remove.
                if($product[0] != $orderid)
                {
                    //If result is false add product order to new array.
                    $newcart[] = array($product[0], $product[1]);

                }
            }

            //Encode new Cart array into JSON.

            $createcart = json_encode($newcart);
            setcookie('Cart',$createcart,time()+3600);
        }

        public function CountCart()
        {
            if (isset($_COOKIE['Cart']))
            {

                $cart = $_COOKIE['Cart'];

                //JSON content is turned back into array.

                $products = json_decode($cart);

                //count array.
                $count=count($products);
                return $count;

            }
            else
            {
                return '0';
            }

        }
    }


    ?>



     <?php
        if(isset($_POST['addtobasket']) && !empty($_POST['addtobasket'])){
                $id=clearxss($_POST['id']);
                require_once('basket/product.cl.php');
                $cart=new Cart();
                $cart-> AddProduct($id,'1');
    //echo "yes";

                //header("Location: http://www.srgiran.com/index.php?srg=basket");
        }

?>


var\u dump($\u COOKIE['Cart'])的结果是什么?如果设置了,则将其放入执行的if中。在发出警告之前调用了哪些函数?是
CountCart()
RemoveProduct()
DisplayCart()
,还是
AddProduct()
?@Chris我在本地主机上没有任何问题,但在live server上,我今天早上上传了站点,我看到了这个问题,是的,我用print\r()回显了阵列;但是里面没有任何东西。@Keeleon一开始它的显示为null,下一部分有id值,但如果我单击另一个产品,它的id值将被替换。你可以在这个网站www.srgiran.com上查看这是var dump:string(16)[[[\'40\',\'1\']]这是打印:[[\'40\',\'1\']这是解码后的var dump:null