Forum Moderators: coopster
set_time_limit(0);
session_start();
$SID=session_id();
//i designed a shopping cart
//page named is shop_cart.php
//the code on this page is:
$act=$_GET["act"]; //the action like card added or removed
$im=$_GET["image"]; //the image of card paased
$pr=$_GET["val"]; //the amount of image as it is in dollar like $5.00
$action=$_GET["action"];
$pr=substr($pr,1); // trim the $ sign $pr=5.00
$SID=session_id();
//open databse in MySQL
$db_host="localhost";
$username="myusername";
$password="mypassword";
$db_name="netphone_db";
$connection=mysql_connect($db_host,$username,$password) or die(mysql_error());
$database=mysql_select_db($db_name,$connection) or die(mysql_error());
if($act == "add")
{
//see if the user has bought the item already or not
$result=mysql_query("SELECT * FROM tmp_shop WHERE image='$im' and price='$pr' and SID='$SID'");
$row=mysql_fetch_array($result,MYSQL_ASSOC);
$num=mysql_numrows($result);
//if item not bought already, insert into tmp_shop
if($num == 0)
{
$query="INSERT INTO tmp_shop (image,quantity,price,total,SID,Date) values('$im','$q','$pr','$tot','$SID','$Date')";
mysql_query($query);
}
//if bought already, simply update the quantity depending upon the item and its rate
else if($num!= 0)
{
$q=$row["quantity"];
$q=(int)$q + 1;
$tot=(int)$q*(float)$pr;
$update= "UPDATE tmp_shop SET quantity='$q',total='$tot' WHERE image='$im' and price='$pr' and SID='$SID'";
mysql_query($update);
}
}
?>
<html><body>
// display the shoping cart
//again open database and select record from tmp_shop for that SID
<?
$result=mysql_query("SELECT * FROM tmp_shop where SID='$SID'");
$num1=mysql_num_rows($result);
// if shoping cart is not empty
if($num1!= "0")
{
?>
<table>
<?php
//fetch the data from tmp_shop and display it on the cart
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
echo('<td>><img src="'.$row["image"].'" width="100" height="80"></td>
<td > '.$row["quantity"].'</td> ');
echo('<td><A href="shop_cart.php?image='.$row["image"].'&val=$'.$row["price"].'&act=add">add
one more card</A></td>');
echo('<td> '.$row["price"].'</td>
</tr>
</table>');
}
}
else?>
shoping cart is empty;
<? mysql_close()?>
</body></html>
this is the code
problem with the code is that
I have a page cardlist from where i boubght a card, at this page there is link "BUY now" when somebody click on this link
item is added on the shoping cart and now shop_cart page is displayed. to add one more card i click on the add one more card link on the shop_cart page.
this increase the quantity bought. but when i click on browser's back button it does not go to the previous page say "cardlist"
if i double click it goes back. also if i refresh the shop_cart page after adding one more item by clicking on add one more item link it add one more quantity.
for example if i bought an item from cardlist page. it add the quantity "1" on shopping cart and display the shop_cart. now i add one more item by clicking on link add one more card. now quantity become "2" now if refresh the the page the quantity become "3"
[edited by: coopster at 1:17 pm (utc) on July 24, 2004]
[edit reason] generalized username and password [/edit]
As for the back button, if I understand right, you are doing the following
1. looking at list of cards on PAGE 'card listing'
2. clicking on link to add card to cart which takes you to PAGE 'shopping cart'
3. clicking on link to add one more card. Still on PAGE 'shopping cart'
4. hitting the back button. Still on PAGE 'shopping cart'
5. hitting the back button again. Now on PAGE 'card listing'
This is the correct behavior. At step 3, hitting the back once should take you back to step 2, which is the shopping cart. "double clicking" on the back button gets you to card list.
You need a link in your page to "continue shopping". You save the URL or search criteria for the listing page and create a link that recreates the page.
By the way, I did not read *any* of your code. If you want people to read your code, you need to work out which parts are important and which parts aren't. I answered based on your statement of the problem at the end.
Tom