SharingDOODLES
06-18-2007, 01:28 PM
Hello,
I am pretty new to php but i am attempting to create a simple product page and cart page to see how it all works,
I am retriving the price, name, and qty in stock from MySQL database,
The problem i have got is when the products are added to the cart it is adding the wrong infomation,
it dosent matter what product u select the infomation put into the cart will be the first one listed in the database, it just keeps extracting the next one in db every time,
product page snippet
<td>
<form method="post" action="cart.php?action=add&id=ProductItem1">
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="ProductItem1" />
</form>
</td>
PHP snippet
<?php session_start(); ?>
<?PHP include 'data.php'; ?>
<?PHP
//function to check if a product exists
function productExists($productName) {
// use sprintf to make sure that $productName is inserted into
// the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM products WHERE productName = %b", $productName);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
//the product id from the URL
$productName = $_GET[id];
//the action from the URL
$action = $_GET[action];
//if that productName doesn't exist display an error message
if($productName && !productExists($productName)) {
die("Error. Product Doesn't Exist");
}
//decide what to do
switch($action) {
case "add":
//add one to the qty of the product with id $productName
$_SESSION['cart'][$productName]++;
break;
case "remove":
//remove one from the qty of the product with id $productName
$_SESSION['cart'][$productName]--;
//if the qty is zero, remove it using the 'unset' functio) -
//otherwise it will show zero, then -1, -2 etc
if($_SESSION['cart'][$productName] == 0) unset($_SESSION['cart'][$productName]);
break;
case "empty":
//empty the cart.
unset($_SESSION['cart']);
break;
}
?>
<?php
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table
//iterate through the cart, the $productName is the key and $qty is the value
foreach($_SESSION['cart'] as $productName => $qty) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $productName is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM products WHERE productName = %b;", $productName);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
list($name, $stock, $price) = mysql_fetch_row($result);
$cost = $price/100 * $qty; //work out the line cost
$total = $total + $cost; //add to the total cost
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\">$name</td>";
//along with a 'remove' link next to the qty - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\">$qty <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$productName\">X</a>
<a href=\"$_SERVER[PHP_SELF]?action=add&id=$productName\">add</a></td>";
echo "<td align=\"center\">$cost</td>";
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
echo "</tr>";
echo "</table>";
}else{
// else tell the user there cart is empty
echo "Your shopping cart is currently empyt.";
}
?>
I am not looking for someone to tottaly rewrite my script for me, I just need pointing in the right direction,
If you need anymore info please ask!
I am pretty new to php but i am attempting to create a simple product page and cart page to see how it all works,
I am retriving the price, name, and qty in stock from MySQL database,
The problem i have got is when the products are added to the cart it is adding the wrong infomation,
it dosent matter what product u select the infomation put into the cart will be the first one listed in the database, it just keeps extracting the next one in db every time,
product page snippet
<td>
<form method="post" action="cart.php?action=add&id=ProductItem1">
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="ProductItem1" />
</form>
</td>
PHP snippet
<?php session_start(); ?>
<?PHP include 'data.php'; ?>
<?PHP
//function to check if a product exists
function productExists($productName) {
// use sprintf to make sure that $productName is inserted into
// the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM products WHERE productName = %b", $productName);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
//the product id from the URL
$productName = $_GET[id];
//the action from the URL
$action = $_GET[action];
//if that productName doesn't exist display an error message
if($productName && !productExists($productName)) {
die("Error. Product Doesn't Exist");
}
//decide what to do
switch($action) {
case "add":
//add one to the qty of the product with id $productName
$_SESSION['cart'][$productName]++;
break;
case "remove":
//remove one from the qty of the product with id $productName
$_SESSION['cart'][$productName]--;
//if the qty is zero, remove it using the 'unset' functio) -
//otherwise it will show zero, then -1, -2 etc
if($_SESSION['cart'][$productName] == 0) unset($_SESSION['cart'][$productName]);
break;
case "empty":
//empty the cart.
unset($_SESSION['cart']);
break;
}
?>
<?php
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table
//iterate through the cart, the $productName is the key and $qty is the value
foreach($_SESSION['cart'] as $productName => $qty) {
//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $productName is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM products WHERE productName = %b;", $productName);
//Only display the row if there is a product (though there should always be as we have already checked)
if(mysql_num_rows($result) > 0) {
list($name, $stock, $price) = mysql_fetch_row($result);
$cost = $price/100 * $qty; //work out the line cost
$total = $total + $cost; //add to the total cost
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\">$name</td>";
//along with a 'remove' link next to the qty - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\">$qty <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$productName\">X</a>
<a href=\"$_SERVER[PHP_SELF]?action=add&id=$productName\">add</a></td>";
echo "<td align=\"center\">$cost</td>";
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";
//show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
echo "<tr>";
echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
echo "</tr>";
echo "</table>";
}else{
// else tell the user there cart is empty
echo "Your shopping cart is currently empyt.";
}
?>
I am not looking for someone to tottaly rewrite my script for me, I just need pointing in the right direction,
If you need anymore info please ask!