Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

shopping cart help

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

shopping cart help

Postby shahogjalal » Sat Jun 08, 2013 1:56 am

I have two files for simple shopping cart, its working but showing some error.Please check and tell me the mistake.Thanx.

file with cart.php contains this code.

<?php session_start(); ?>
<!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">
<head>
<meta name="description" content="PHP Shopping Cart Using Sessions" />
<meta name="keywords" content="shopping cart tutorial, shopping cart, php, sessions" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" media="all" href="/style/style.css" type="text/css" />
<title>Cart</title>


<?php
//connect to your database here
$conn = mysql_connect("localhost", "root", "");
$db = mysql_select_db("cart_carting", $conn);
?>


</head>
<body>


<?php

$product_id = $_GET['id']; //the product id from the URL
$action = $_GET['action']; //the action from the URL

//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}

switch($action) { //decide what to do

case "add":
$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
break;

case "remove":
$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;

case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the 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 $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $product_id => $quantity) {

//get the name, description and price from the database - this will depend on your database implementation.
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT name, description, price FROM art_carting WHERE id = %d;",
$product_id);

$result = mysql_query($sql);

//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, $description, $price) = mysql_fetch_row($result);

$line_cost = $price * $quantity; //work out the line cost
$total = $line_cost; //add to the total cost
$Atotal = $total + $line_cost;
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\">$name</td>";
//along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>";
echo "<td align=\"center\">$line_cost</td>";

echo "</tr>";

}

}

//show the total
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "<td colspan=\"2\" align=\"right\">Aggregate Total</td>";
echo "<td align=\"right\">$Atotal</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{
//otherwise tell the user they have no items in their cart
echo "You have no items in your shopping cart.";

}

//function to check if a product exists
function productExists($product_id) {
//use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
$sql = sprintf("SELECT * FROM art_carting WHERE id = %d;",
$product_id);

return mysql_num_rows(mysql_query($sql)) > 0;
}
?>

<a href="products.php">Continue Shopping</a>


<?php

/*

products table:
CREATE TABLE `products` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`description` TEXT,
`price` DOUBLE DEFAULT '0.00' NOT NULL ,
PRIMARY KEY ( `id` )
);

*/

?>



</body>
</html>



and file with product.php contain this code.

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="PHP Shopping Cart Using Sessions" />
<meta name="keywords" content="shopping cart tutorial, shopping cart, php, sessions" />
<link rel="stylesheet" media="all" href="/style/style.css" type="text/css" />
<title>Products</title>

<?php
//connect to your database here
$conn = mysql_connect("localhost", "root", "");
$db = mysql_select_db("cart_carting", $conn);
?>

</head>

<body>


<table border="1">

<?php

$sql = "SELECT id, name, description, price FROM art_carting;";

$result = mysql_query($sql);

while(list($id, $name, $description, $price) = mysql_fetch_row($result)) {

echo "<tr>";

echo "<td>$name</td>";
echo "<td>$description</td>";
echo "<td>$price</td>";
echo "<td><a href=\"cart.php?action=add&id=$id\">Add To Cart</a></td>";

echo "</tr>";
}

?>
</table>


<a href="cart.php">View Cart</a>

</body>
</html>

and i have a data base with this

http://tinypic.com/view.php?pic=i5dmo8&s=5
shahogjalal
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sat Jun 08, 2013 1:44 am

Re: shopping cart help

Postby developerahusain » Sat Jun 08, 2013 4:38 am

firstly this line has the problem

$conn = mysql_connect("localhost", "root", "");
There is no password in it. If there is no password then it cannot connect the db.Correct this and if problem persists,please do take the screensheet of the error you are viewing and send it to me. I will resolve your problem

developerahusain
developerahusain
New php-forum User
New php-forum User
 
Posts: 36
Joined: Tue May 28, 2013 10:04 pm
Location: India,Lucknow


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.