0

我有购物车,当我将商品添加到购物车中时,当我想更改数量或删除任何商品时,它给了我空的购物车。这是购物车的代码,开头:

编辑:

<?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';
        }
    }
}

 ?>
4

4 回答 4

0
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

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.

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.

于 2013-02-11T18:12:19.767 回答
0

尝试这个

 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';
 }
 }
 }

在表单按钮应该是这样的

 <a href="javascript:del(<?php echo $id?>)">
 <input type="button" class="button5" value="Remove" /></a>
于 2013-04-06T23:00:36.803 回答
0

您没有isset()正确使用该功能。这将返回一个布尔值 true 或 false,而不是值的字符串。您可能打算做的是:

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

...和...

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

如果没有这些更改,您将永远无法执行这些代码块,因为真/假永远不会等于“清除”或“更新”。

于 2013-02-11T17:56:42.227 回答
0

据我所知,服务器语言(php)不理解给定的目录,尝试有一个像 require 这样的有效目录,projectName/functions.php;希望它也会有所帮助。

于 2013-08-30T11:41:25.047 回答