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.
 
this code does not look complete, William. for example there are curly braces just hanging at the start of the code with no apparent purpose.

i think it is a bit odd that you are using id for a quantity too. it would help if you also provided the html for the form that you use for the cart interaction.

at a minimum your form needs to pass (or your code needs to know) the product id of the product you are wanting to change the quantity of, and the quantity itself, as you point out. i would use an embedded form which posted the information to the webserver. if you want to use a get method instead you would either use an embedded form or write a link. you can add variables to the query string by separateing the key=value pair with an "&". so
Code:
<a href="somepage.php?field1=value1&field2=value2&field3=value3"> query </a>

 
Ummm interesting I did not know you could do that! wow.

here is the HTML form that I am trying to interact with.

Code:
<HTML>
<HEAD>
<TITLE>Product Thumbnail</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<link href="css/css.css" rel="stylesheet" type="text/css">


<style type="text/css">
<!--
.style1 {color: #FFFFFF}
.style3 {color: #FFFFFF; font-weight: bold; }
-->
</style>
</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
<!-- ImageReady Slices (indexNewsubpages.psd) -->
<TABLE WIDTH=775 BORDER=0 align="center" CELLPADDING=0 CELLSPACING=0>
	<TR>
		<TD ROWSPAN=9>
			<IMG SRC="images/products_01.jpg" WIDTH=18 HEIGHT=775 ALT=""></TD>
		<TD COLSPAN=10>
			<IMG SRC="images/products_02.jpg" WIDTH=600 HEIGHT=73 ALT=""></TD>
		<TD>

			<IMG SRC="images/products_03.jpg" WIDTH=137 HEIGHT=73 ALT=""></TD>
		<TD ROWSPAN=9>
			<IMG SRC="images/products_04.jpg" WIDTH=20 HEIGHT=775 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=11>
			<IMG SRC="images/products_05.jpg" WIDTH=737 HEIGHT=176 ALT=""></TD>
	</TR>
	<TR>

		<TD>
			<IMG SRC="images/products_06.jpg" WIDTH=139 HEIGHT=64 ALT=""></TD>
		<TD COLSPAN=4>
			<IMG SRC="images/products_07.jpg" WIDTH=158 HEIGHT=64 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_08.jpg" WIDTH=138 HEIGHT=64 ALT=""></TD>
		<TD COLSPAN=3>
			<IMG SRC="images/products_09.jpg" WIDTH=165 HEIGHT=64 ALT=""></TD>
		<TD>

			<IMG SRC="images/products_10.jpg" WIDTH=137 HEIGHT=64 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=11>
			<IMG SRC="images/products_11.jpg" WIDTH=737 HEIGHT=39 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=2 valign="top">
		<IMG SRC="images/products_12.jpg" WIDTH=214 HEIGHT=33 ALT="">

<IMG SRC="images/products_15.jpg" WIDTH=214 HEIGHT=27 ALT="">
<IMG SRC="images/products_16.jpg" WIDTH=214 HEIGHT=24 ALT="">
<IMG SRC="images/products_17.jpg" WIDTH=214 HEIGHT=24 ALT="">
<IMG SRC="images/products_18.jpg" WIDTH=214 HEIGHT=23 ALT="">
<IMG SRC="images/products_19.jpg" WIDTH=214 HEIGHT=24 ALT="">
		
		
		&nbsp;																		</TD>
		<TD ROWSPAN=3>
			<IMG SRC="images/products_13.jpg" WIDTH=19 HEIGHT=345 ALT=""></TD>
		<TD COLSPAN=8 ROWSPAN=3 valign="top">
		  
			  <style>
.odd_row {background:url(products/mysqlbg1.jpg);}
.even_row {background:url(products/mysqlbg2.jpg);}
/*.odd_row {background:url(../images/mysql_bg2.jpg);} */
/*.odd_row {background-color:#00CCFF;} */
/*.even_row {background-color:#CCCCCC;} */
</style>	
			  <table width="450" border="0" align="center" cellpadding="0" cellspacing="0">

          <tr valign="middle" class="SubHeadings">
                <td><h3 align="left"><span class="style3">ITEM</span></h3></td>
                <td><h3 align="left"><span class="style3">NAME</span></h3></td>
                <td><h3 align="left"><span class="style3">QUANTITY</span></h3></td>
                <td><h3 align="left"><span class="style3">PRICE</span></h3></td>
				<td><h3 align="left"><span class="style3">ACTION</span></h3></td>

          </tr>
		  
		    	  
			  <style>
.even_row {background:url(products/mysqlbg2.jpg);}
.odd_row {background:url(products/mysqlbg1.jpg);}
</style>
<center><h2>YOUR SHOPPING BASKET</h2></center>	  
	     <tr class= odd_row><td width="15%" class="LoopRowsQueryCart">
<img src="products/KATI BROWN.jpg" alt="Product in your Basket" width="30" height="30"></a>
<td><br/>KATI BROWN</td><td><br/>1</td><td><br/>£34.99</td><td>
<a  class="LoopRowsQueryCart" href="remove.php?id=L003">remove</a>	
	</td>
<td>

<form action="modifycart.php" method="post" name="form1" class="text8">

<input name="textfield" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>


</td>	
	
	<br /><br /></td></tr><tr class= even_row><td width="15%" class="LoopRowsQueryCart">
<img src="products/MARY-ANN.jpg" alt="Product in your Basket" width="30" height="30"></a>
<td><br/>MARY-ANN</td><td><br/>1</td><td><br/>£39.99</td><td>
<a  class="LoopRowsQueryCart" href="remove.php?id=L005">remove</a>	
	</td>

<td>

<form action="modifycart.php" method="post" name="form1" class="text8">

<input name="textfield" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>


</td>	
	
	<br /><br /></td></tr><tr class= odd_row><td width="15%" class="LoopRowsQueryCart">
<img src="products/R-SUZY-SHOULDER-BAG.jpg" alt="Product in your Basket" width="30" height="30"></a>
<td><br/>R-SUZY-SHOULDER-BAG</td><td><br/>1</td><td><br/>£39.99</td><td>
<a  class="LoopRowsQueryCart" href="remove.php?id=L006">remove</a>	
	</td>

<td>

<form action="modifycart.php" method="post" name="form1" class="text8">

<input name="textfield" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>


</td>	
	
	<br /><br /></td></tr><tr class= even_row><td width="15%" class="LoopRowsQueryCart">
<img src="products/SUZY-SHOULDER-BAG.jpg" alt="Product in your Basket" width="30" height="30"></a>
<td><br/>SUZY-SHOULDER-BAG</td><td><br/>1</td><td><br/>£39.99</td><td>
<a  class="LoopRowsQueryCart" href="remove.php?id=L008">remove</a>	
	</td>

<td>

<form action="modifycart.php" method="post" name="form1" class="text8">

<input name="textfield" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>


</td>	
	
	<br /><br /></td></tr><tr class= odd_row><td width="15%" class="LoopRowsQueryCart">
<img src="products/SUZY-SHOULDER-BAG.jpg" alt="Product in your Basket" width="30" height="30"></a>
<td><br/>SUZY-SHOULDER-BAG</td><td><br/>1</td><td><br/>£39.99</td><td>
<a  class="LoopRowsQueryCart" href="remove.php?id=L008">remove</a>	
	</td>

<td>

<form action="modifycart.php" method="post" name="form1" class="text8">

<input name="textfield" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>


</td>	
	
	<br /><br /></td></tr>  


		  
     <tr>
	 <td colspan="5"><p><hr></td>
	 
	 </tr>
	 
	 
	 <tr>

                <td class="style1">&nbsp;</td>
                <td class="style1">&nbsp;</td>
                <td class="tablePrices">Total</td>
                <td class="tablePrices">£194.95</strong></td>
        </tr>		  
		
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>

            <td>&nbsp;</td>
            <td colspan="2" align="right">
			<form target="paypal" action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1">
<!--setup for paypal integration -->
<input type="hidden" name="business" value="yewandel@hotmail.com">    
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="receiver_email" value="yewandel@hotmail.com">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="lc" value="GB">

     

    <!-- ITEM 1 DATA -->

    <input type="hidden" name="item_number_1" value="L003"/>
    <input type="hidden" name="item_name_1" value="KATI BROWN"/>
    <input type="hidden" name="quantity_1" value="1"/>
    <input type="hidden" name="amount_1" value="34.99"/>
    <!-- END ITEM 1 DATA -->    <!-- ITEM 2 DATA -->
    <input type="hidden" name="item_number_2" value="L005"/>
    <input type="hidden" name="item_name_2" value="MARY-ANN"/>
    <input type="hidden" name="quantity_2" value="1"/>

    <input type="hidden" name="amount_2" value="39.99"/>
    <!-- END ITEM 2 DATA -->    <!-- ITEM 3 DATA -->
    <input type="hidden" name="item_number_3" value="L006"/>
    <input type="hidden" name="item_name_3" value="R-SUZY-SHOULDER-BAG"/>
    <input type="hidden" name="quantity_3" value="1"/>
    <input type="hidden" name="amount_3" value="39.99"/>
    <!-- END ITEM 3 DATA -->    <!-- ITEM 4 DATA -->
    <input type="hidden" name="item_number_4" value="L008"/>

    <input type="hidden" name="item_name_4" value="SUZY-SHOULDER-BAG"/>
    <input type="hidden" name="quantity_4" value="1"/>
    <input type="hidden" name="amount_4" value="39.99"/>
    <!-- END ITEM 4 DATA -->    <!-- ITEM 5 DATA -->
    <input type="hidden" name="item_number_5" value="L008"/>
    <input type="hidden" name="item_name_5" value="SUZY-SHOULDER-BAG"/>
    <input type="hidden" name="quantity_5" value="1"/>
    <input type="hidden" name="amount_5" value="39.99"/>

    <!-- END ITEM 5 DATA --><input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" src="[URL unfurl="true"]https://www.paypal.com/en_US/i/btn/x-click-but23.gif"[/URL] border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">

</form>
		
			
			
			
			
			
			
			
			
			
			
			
			
			</td>
            </tr>
		  <tr> 
		  <td colspan="4"> <a class="LoopRowsQueryCart" href="javascript:history.go(-1)">
						<<< Go Back<p>

</a></td>
<td colspan="5"> <a class="LoopRowsQueryCart" href="[URL unfurl="true"]http://www.ylabel.co.uk">[/URL]
						HOME >>><p>
</a></td>


</tr>
        </table>
		
		 
	  		
		
		
		
		
		
		
		
		
	  </TD>
	</TR>
	<TR>

		<TD COLSPAN=2>
			<IMG SRC="images/products_20.jpg" WIDTH=214 HEIGHT=109 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=2>
			<IMG SRC="images/products_21.jpg" WIDTH=214 HEIGHT=81 ALT=""></TD>
	</TR>
	<TR>
		<TD COLSPAN=4 ROWSPAN=2>

			<IMG SRC="images/products_22.jpg" WIDTH=282 HEIGHT=78 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_23.jpg" WIDTH=88 HEIGHT=37 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_24.jpg" WIDTH=126 HEIGHT=37 ALT=""></TD>
		<TD>
			<IMG SRC="images/products_25.jpg" WIDTH=72 HEIGHT=37 ALT=""></TD>
		<TD COLSPAN=2>
			<IMG SRC="images/products_26.jpg" WIDTH=169 HEIGHT=37 ALT=""></TD>

	</TR>
	<TR>
		<TD COLSPAN=7>
			<IMG SRC="images/products_27.jpg" WIDTH=455 HEIGHT=41 ALT=""></TD>
	</TR>
	<TR>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=18 HEIGHT=1 ALT=""></TD>
		<TD>

			<IMG SRC="images/spacer.gif" WIDTH=139 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=75 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=19 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=49 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=15 HEIGHT=1 ALT=""></TD>

		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=73 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=65 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=61 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=72 HEIGHT=1 ALT=""></TD>
		<TD>

			<IMG SRC="images/spacer.gif" WIDTH=32 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=137 HEIGHT=1 ALT=""></TD>
		<TD>
			<IMG SRC="images/spacer.gif" WIDTH=20 HEIGHT=1 ALT=""></TD>
	</TR>
</TABLE>
<!-- End ImageReady Slices -->
</BODY>
</HTML>
 
in each of these forms
Code:
<form action="modifycart.php" method="post" name="form1" class="text8">

<input name="textfield" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>

make sure that you are including a hidden field with the productID of the product in question. i would also advise changing the name of the input holding the quantity from textfield.

the incoming data will be in the $_POST superglobal.
 
Ok am a little lost now.

Maybe my codes are two twisted up. I'll try to explain it.

I have a shopping cart, which basically shows the item in the basket. I now put a form text field on beside each of the items on the cart to enable users to change the number of items they want. so I created this code below:

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


<?php
$quantity = $HTTP_COOKIE_VARS[quantity];
$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='$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\">");
 
    ?>

this is of the top of my head to an extent, am not really a php genius as you can tell. The above code basically just shows blank and dosnt appear to do anything. what can I do regarding this?

Thanks again
 
i'm getting muddled in the files. when you post code can you post like this

Code:
code goes here

that way i can see which file is which.

anyway, i am still not getting the code properly as it does not display properly on my servers. can you point me to your website and i'll look at the code as generated by your live environment?
 
yup. i agree with myself! in the script that outputs the shopping cart to the screen make sure that the form that currently looks like this

Code:
<form action="modifycart.php" method="post" name="form1" class="text8">

<input name="textfield" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>

actually looks like this

Code:
<form action="modifycart.php" method="post" name="form1" class="text8">
[red]<input type="hidden" name="productID" value="WHATEVER THE PRODUCTID IS" />[/red]
<input name="[red]quantity[/red]" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>

then in your receiving script
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());
		header('Location: cart_view.php');
	}
} else {
	header('Location: cart_view.php');
}
?>
 
Hi Justin,

Thank you for the above it looks very right however, when I tried it, it simply run through the receiving page and returns to the cart_view.php page without having to update the page.

Cart_view page form code now looks like this:

Code:
<form action="modifycart.php" method="post" name="form1" class="text8">
<input type="hidden" name="productID" value="<? echo "" . $row['product_id'];?>" />
<input name="quantity" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="Submit" value="update">
</form>

And the receiving page as you wrote above. it dosnt give error but at the same time dosnt update.

Thanks for your efforts so far.
 
remove the two double quotes and dot after the <? echo
 
echo out the queries rather than running them. then test the queries in phpmyadmin.
 
Thanks for that. Two things, is there an alternative easier way of achieving the same result? And how should I echo them out? I tried echo $query which I dont think is right neither.
 
the code is fine (i think) just change this line as shown
Code:
if(isset($_POST['[red]S[/red]ubmit'])){
 
Still the same. Might just be something small somewhere I am searching through but cant see anything so far.
 
just echo the queries. comment out the header lines and add an echo $query
 
It just shows a blank page and when I tried to view the blank page html output source as well, its just blank.
 
please post the code of modifycart.php

also the output from phpinfo() on display_errors and error_reporting.
 

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

Idont know were to get the "output from phpinfo() on display_errors and error_reporting." From.

 
Just in case you need the cart_view html form code as well, here it is.

Code:
<form action="modifycart.php" method="post" name="form1" class="text8">
<input type="hidden" name="productID" value="<? echo $row['product_id'];?>" />
<input name="quantity" type="text" value="1" size="2" maxlength="2">
<input type="submit" name="submit" value="update">
</form>

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top