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.'; echo ' | ' . $prodname . ' | '; echo '' . '$' . $price . ' | '; echo '
?>
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<?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);
?>
|