Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Shopping Cart Quantity 1

Status
Not open for further replies.

WilliamMute

Programmer
Jan 4, 2006
117
Hi Again,

not sure the way forward. I am certain this code for updating the quantity on my self made shopping cart is a time bomb which will probably crash my computer when executed. Basically, I have an update field on my cart where the user simply types in desired number of the products they want.

Code:
<?php

$quantity = $_GET[id];
{
        mysql_select_db($database_Connection, $Connection);
		
		
		  if($quantity <= 0)  $query = 
                "DELETE FROM ylabelcart WHERE ylabelcart.`product_id`='$var' AND ylabelcart.`session`='$cart_id'";
				  mysql_query ($query)
				  die("<META http-equiv=\"Refresh\" content=\"0;url=cart_view.php\">");
			else	  
			 $query = "UPDATE ".$this->ylabelcart.
         " SET quantity='$quantity' WHERE session='".$this->cart_id."' ";
   $query .= "AND product_id='$product' ";
   mysql_query ($query)   
            or die("There was a failure putting the required Product into your shopping basket, Please try again ".mysql_error());
			die("<META http-equiv=\"Refresh\" content=\"0;url=cart_view.php\">");
    }
		
    ?>

I know that the above code is not right because how can I get my product id from the cart page in other to use it on my query? I have already use the $_GET[id]; function to receive the initial quantity.

Thanks for your help again.
 
you have not changed the isset line. it won't work unless you do.

 
I did changed it but no difference, so I changed the HTML form to a lower case as well and did the same with the isset as you can see from the code above but to no avail
 
pls put this code up for debugging

Code:
<?php
require_once('Connections/Connection.php');

mysql_select_db($database_Connection, $Connection);
if(isset($_POST['submit'])){
    if (session_name() == '') {session_start();} //force the session to start if it has not already done so
    $quantity = (int) trim($_POST['quantity']);
    $productID = trim($_POST['productID']);
    $cart_id = $_SESSION['cart_ID'];
    
    //should put in some verification code to ensure the quantity has not been spoofed
    
    if ($quantity <=0){
        $query = "    DELETE
                    FROM ylabelcart
                    WHERE
                        ylabelcart.`product_id`='".mysql_escape_string($productID)."'
                        AND
                        ylabelcart.`session`='$cart_id'";
        mysql_query($query) or die (mysql_error()); //change for production code
        header('Location: cart_view.php');
    } else {
        $query = "UPDATE
                    ylabelcart
                SET
                    quantity='".mysql_escape_string($quantity)."'
                WHERE
                    ylabelcart.`product_id`='".mysql_escape_string($productID)."'
                    AND
                    ylabelcart.`session`='$cart_id'";
        mysql_query ($query)   
            or die("There was a failure putting the required Product into your shopping basket, Please try again ".mysql_error());
    echo $query;
      //  header('Location: cart_view.php');
    }
} else {
 echo $query'
 //  header('Location: cart_view.php');
}
?>
 
well the code that i am looking at does not seem to be behaving in the same way that modifycart should behave. and the form still has the capital S in submit.

is the above version of modifycart now uploaded?
 
pls fix the second to last line by changing the apostrophe for a semicolon.
 
ok. still some strange things going on.

please upload this version of modifycart.php
Code:
<?php
require_once('Connections/Connection.php');

mysql_select_db($database_Connection, $Connection);
if(isset($_POST['submit'])){
    if (session_name() == '') {session_start();} //force the session to start if it has not already done so
    $quantity = (int) trim($_POST['quantity']);
    $productID = trim($_POST['productID']);
    $cart_id = $_SESSION['cart_ID'];
    
    //should put in some verification code to ensure the quantity has not been spoofed
    
    if ($quantity <=0){
        $query = "    DELETE
                    FROM ylabelcart
                    WHERE
                        ylabelcart.`product_id`='".mysql_escape_string($productID)."'
                        AND
                        ylabelcart.`session`='$cart_id'";
		echo $query;
        mysql_query($query) or die (mysql_error()); //change for production code
        //header('Location: cart_view.php');
    } else {
        $query = "UPDATE
                    ylabelcart
                SET
                    quantity='".mysql_escape_string($quantity)."'
                WHERE
                    ylabelcart.`product_id`='".mysql_escape_string($productID)."'
                    AND
                    ylabelcart.`session`='$cart_id'";
		echo $query;
        mysql_query ($query)   
            or die("There was a failure putting the required Product into your shopping basket, Please try again ".mysql_error());

      //  header('Location: cart_view.php');
    }
} else {
 echo $query;
 //  header('Location: cart_view.php');
}
echo '<hr />For debugging purposes<br/><pre>'.print_r($_POST, true) . print_r($_SESSION, true). '</pre>';
echo "<hr />";
echo htmlentities(file_get_contents('modifycart.php'));
?>
 
you have not harmonised the capitalisation of the select button. leave the viewcart script as is and change the modifycart script to

Code:
if(isset($_POST['[red]S[/red]ubmit'])){
 
Hi Justin,

Itsworking now!!! thanks a million. The problem was the session value wasnt passing through. I changed the session from
Code:
if (session_name() == '') {session_start();}

to this version

Code:
 if(!isset($HTTP_COOKIE_VARS['cart_id'])) {
     $cart_id = md5(uniqid(rand()));
     setcookie("cart_id", $cart_id, time() + 14400);
 } else {
     $cart_id = $HTTP_COOKIE_VARS['cart_id'];
 }

And that was the problem. Thank you for your extraordinary help you've been wonderful. Much Appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top