简体   繁体   中英

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. That will return a boolean value true or false, not a string of the value. 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".

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.

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; hope it will help too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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