I'm attempting to create a shopping cart using PHP and MySQL. I have gone through and done most of the work and it works fine, except for one thing: It's not adding my unit price to the database or totaling up my item prices.
I have two database tables, one for the items and one for tracking users:
Table 1 (products) has the following fields:
id, part_number, title, description, color, price
Table 2 (user_track) has the following fields:
user_id, sel_item, sel_item_title, sel_item_desc, sel_item_color, sel_item_personalization, sel_item_qty, sel_item_price, sel_item_totalprice, date_added
Here is the code for my addtocart.php page:
<?
// File Name: shop_addtocart.php
// check for required fields
if ((!($sel_item)) || (!($sel_item_title)) || (!($sel_item_desc)) || (!($sel_item_color)) || (!($sel_item_qty)) || (!($sel_item_price))) {
header("Location: http://www.customchronicles.com/show_menu.php");
exit;
}
// set cookie if not already set
if (!isset($user_id)) {
$token = md5(uniqid(rand()));
setcookie("user_id",$token,time()+86400,"/",".customchronicles.com");
}
// create connection
$connection = mysql_connect("localhost","xxxxxx","xxxxxx") or die ("Couldn't connect to the database.");
// select database
$db = mysql_select_db("fwdesign", $connection) or die ("Couldn't select database.");
// do some math
$sel_item_totalprice = $sel_item_qty * $sel_item_price;
// format a datestamp
$date_added = date("y-m-d");
// SQL statement to add record to track
$sql = "INSERT INTO user_track VALUES(\"$user_id\", \"$sel_item\", \"$sel_item_title\", \"$sel_item_desc\", \"$sel_item_color\", \"$sel_item_personalization\", \"$sel_item_qty\", \"$sel_item_price\", \"$sel_item_totalprice\", \"$date_added\")";
// execute SQL query and exit if failure
$sql_result = mysql_query($sql,$connection) or die ("Couldn't insert record!");
// count cart items
$item_count = "SELECT SUM(sel_item_qty) FROM user_track WHERE user_id = \"$user_id\"";
$item_result = mysql_query($item_count);
$item_count_total = mysql_result($item_result,0,"SUM(sel_item_qty)");
?>
<html>
<head>
<title>Custom Chronicles Shopping Menu: Product Added To Cart</title>
<link rel="stylesheet" href="chroniclestyle.css" type="text/css" />
</head>
<body>
<h1>Custom Chronicles Shopping Menu: Product Added To Cart</h1>
<p><strong>You have added the following item to your shopping cart:</strong></p>
<p><strong>Item:</strong> <?php echo "$sel_item"; ?> <?php echo "$sel_item_title"; ?>: <?php echo "$sel_item_desc"; ?> -- <?php echo "$sel_item_color"; ?><p>
<strong>Personalization:</strong> <?php echo "$sel_item_personalization"; ?><p>
<strong>Quantity:</strong> <?php echo "$sel_item_qty"; ?><br>
<strong>Single Unit Price:</strong> <?php echo "$sel_item_price"; ?><br>
<strong>Total Price:</strong> <?php echo "$sel_item_totalprice"; ?></p>
<p>Your cart contains
<?php
// display cart count
if ($item_count_total == "1") {
echo "1 item.";
} else {
echo "$item_count_total items.";
}
?>
<br>
You can <a href="shop_checkout.php">checkout</a> at any time.</p>
<p><a href="shop_viewprod.php">Continue Shopping</a></p>
</body>
</html>
I echoed the SQL statement and it shows something like the following:
INSERT INTO user_track VALUES("dbb46921bad181939804534434efd6fd", "ACIP", "Anniversary Chronicle", "Anniversary gift for someone special. Printed on Ivory Parchment paper. Can be personalized for a couple\'s wedding anniversary, for an employee\'s hiring anniversary, or any other type of anniversary.", "Ivory Parchment", "Some Name, 10/2/69", "1", "$7.95", "0", "02-09-06")
As you can see, it's giving me the unit price of $7.95, but it gives me 0 for the totalprice. Also, when I check my database records, the unit price field is blank. The $7.95 does not show up in my records; however, everything else is inserted. And when I go to checkout, my prices are $0.
Any advice here?
Thanks in advance.
M.