简体   繁体   English

无法删除和更新购物车中的商品,会话有问题

[英]Cannot delete and update items in my shopping cart something wrong with sessions

I have shopping cart, and when I add items into the cart and when I want to change the quantity or to delete any of the items it gives me empty cart. 我有购物车,当我向购物车中添加物品时,当我想更改数量或删除任何物品时,这给了我空的购物车。 Here is the code for the shopping cart, at the beginning: 这是开头的购物车代码:

Edited: 编辑:

<?php 
session_start();
require "C:/xampp/...../DButils.php";
require "C:/xampp/..../functions.php";
$msg = '';
if(isset($_REQUEST['command']) && isset($_REQUEST['pid'])>0)
 {
    remove_product($_REQUEST['pid']);
}
else if(isset($_REQUEST['command']) && $_REQUEST['command'] == 'clear'){
    unset($_SESSION['cart']);
}
else if(isset($_REQUEST['command']) && $_REQUEST['command'] == 'update'){
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++){
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=intval($_REQUEST['product'.$pid]);
        if($q>0 && $q<=999){
            $_SESSION['cart'][$i]['qty']=$q;
        }
        else{
            $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
        }
    }
}

 ?>

You are not using the isset() function correctly. 您没有正确使用isset()函数。 That will return a boolean value true or false, not a string of the value. 这将返回布尔值true或false,而不是该值的字符串。 What you probably intend to do is: 您可能打算做的是:

else if(isset($_REQUEST['command']) && $_REQUEST['command'] == 'clear'){

...and... ...和...

else if(isset($_REQUEST['command']) && $_REQUEST['command'] == 'update'){

Without those changes, you will never be able to execute those code blocks because true/false will NEVER equal "clear" or "update". 没有这些更改,您将永远无法执行那些代码块,因为true / false永远不会等于“ clear”或“ update”。

if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0)
{
    remove_product($_REQUEST['pid']);
}

It looks like you're trying to remove the product when you want to be adding one. 您似乎想在要添加产品时删除该产品。

Also, as @cillosis mentioned 另外,正如@cillosis所提到的

else if(isset($_REQUEST['command'])=='update'){

will ALWAYS evaluate to false. 将始终评估为假。 This is because isset() only ever returns a BOOLEAN value (true, false, 1, 0) and you're evaluation the condition: "If 'true/false' equals 'update'" and since true and false are both not equal to the string 'update' the test will fail every time. 这是因为isset()仅返回BOOLEAN值(true,false,1、0),并且您正在评估条件:“如果'true / false'等于'update'”,并且由于true和false都不相等到字符串“更新”时,测试每次都会失败。

It may make it easier to give you answer to your question if you update the code in your question to remove the errors as they're pointed out. 如果您更新问题中的代码以删除所指出的错误,则可能更容易为您提供问题的答案。 As it's currently written it is fairly hard to decipher. 正如目前所写的那样,很难解密。

try this 尝试这个

 if($_REQUEST['command']=='delete' && $_REQUEST['id']>0){
 remove_product($_REQUEST['id']);
 }
 else if($_REQUEST['command']=='clear'){
 unset($_SESSION['cart']);
 }
 else if($_REQUEST['command']=='update'){
 $max=count($_SESSION['cart']);
 for($i=0;$i<$max;$i++){
 $id=$_SESSION['cart'][$i]['id'];
 q=intval($_REQUEST['qty'.$id]);
 if($q>0 && $q<=999){
 $_SESSION['cart'][$i]['qty']=$q;
 }
 else{
 $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
 }
 }
 }

In Form Button Should Be Like This 表单按钮应该是这样的

 <a href="javascript:del(<?php echo $id?>)">
 <input type="button" class="button5" value="Remove" /></a>

from what i know that server language(php) do not understand that directory given, Try to have a valid directory like require projectName/functions.php; 据我所知,服务器语言(php)无法理解给定的目录,请尝试使用有效的目录,例如require projectName/functions.php; hope it will help too. 希望它也会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM