Variable cookie values not being passed correctly to next web page

I have been working with, and studying to learn PHP for about two months. I am creating my online store, and have run into a problem I need some help with. I currently have two products in my products table (will add more later), and the cookies I have set are often passing the wrong values to my next page, which is set up to enable the customer to see the details, i.e. picture, product code, description, and price on the next page. The initial, or home page is entitled shop.php, and the code is the first program shown below. The second program or page is entitled view_product.php, and is the second program shown below. The way this is intended to and needs to work is that when the customer clicks on a product on the home page, the corresponding details of the product clicked on should immediately come up on the next page. The main problem is that when I position the setcookie lines of code before the ‘extract($row);’ line of code, only the first product is displayed on the next page regardless of which product I clicked on. If I place the setcookie lines after the ‘extract($row);’ line of code, only the last product is displayed on the next page regardless of which product I clicked on. I have been studying and researching, and trying hard to find a solution to this for several days, and although I have 3 PHP books, have tried to find this information in the PHP manual, and have done many searches on the Internet, I have found nothing on this. I hope someone can help me with a good workable solution to this problem. The first item below is the table layout for the products table.

±---------±-------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±---------±-------------±-----±----±--------±------+
| prodcode | char(5) | NO | PRI | NULL | |
| prodname | varchar(100) | NO | | NULL | |
| descrip | mediumtext | YES | | NULL | |
| price | decimal(6,2) | NO | | NULL | |
±---------±-------------±-----±----±--------±------+

Code for shop.php:

Heading Information Basic HTML body text, which is irrelevant code, and has been deleted to make this request for help short as possible. <?php require 'db.inc.php'; $expire = time() + (60 * 60 * 24); $db = mysql_connect('localhost', 'superman', 'loislane') or die ('Unable to connect. Check your connection parameters.'); mysql_select_db('DonsStore', $db) or die(mysql_error($db)); $query = 'SELECT prodcode, prodname, descrip, price FROM DonsStore.products ORDER By prodname ASC'; $result = mysql_query($query, $db)or die(mysql_error($db)); $odd = true; while ($row = mysql_fetch_array($result)) { echo ($odd == true) ? '' : ''; $odd = !$odd; extract($row); setcookie('prodcode', $prodcode, time() + $expire); setcookie('prodname', $prodname, time() + $expire); setcookie('descrip', $descrip, time() + $expire); setcookie('price', $price, time() + $expire); echo ''; echo ''; echo ''; echo ''; } ?>
' . $prodname .
        '' . $prodname . '' . '$' . $price . '
<?php

?>

Code for view_product.php

<?php session_start(); $prodcode = $_COOKIE['prodcode']; $prodname = $_COOKIE['prodname']; $descrip = $_COOKIE['descrip']; $price = $_COOKIE['price']; require 'db.inc.php'; $db = mysql_connect('localhost', 'superman', 'loislane') or die ('Unable to connect. Check your connection parameters.'); mysql_select_db('DonsStore', $db) or die(mysql_error($db)); ?> Heading Information </table

<< Back to home page

<?php echo $prodname; ?> <?php echo $descrip; ?>
Product Code: <?php echo $prodcode; ?>
Price: $<?php echo $price; ?>
Quantity: <?php echo ''; $session = session_id(); $query = 'SELECT qty FROM DonsStore.cart'; $result = mysql_query($query, $db) or die(mysql_error($db)); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); extract($row); } else { $qty = 0; } mysql_free_result($result); echo ''; if ($qty > 0) { echo ''; } else { echo ''; } //setcookie('qty', $qty, time() + 60*60*24); ?>
  </div>
 </form>
</td>

Hi,

Would it not be easier to send the id of the product via $_GET to the next page then pull that data from the database instead of using cookies?
(thats how my store works)

in my browse items script i have links like this:
[php]<a href=“view_item.php?pid={$row[‘item_id’]}”[/php] which send the product id with the url…

then in my view item script i have:
[php]$pid = $_GET[‘pid’]; //<-- gets the id from the url…[/php]
and my query is:
[php]$query = mysql_query(“SELECT * FROM shop WHERE item_id = $pid”); // get the individual item. [/php]

if cookies are blocked on the users system then your script will cause you problems (or rather the would be buyer), this way will work regardless if cookies are on or off.

If ya need more help, gimmie a holla

Red :wink:

PS: i’m quite busy so i may not reply straight away but just send me a pm and i’ll look in asap :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service