Page 1 of 1

Simple Shopping cart using MySQL

Posted: Mon Oct 22, 2007 3:11 pm
by kwahodi
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I'm trying to create a simple shopping cart application which will connect to a MySql database.

I've created the database and I'm able to connect to it. I've also created the items table (books) and cart table.

When I click "Add to cart", the quantity on "view cart" increases by one but the new price does not compute and the book title does not show on the "View Cart" page. What could be my problem?

I've not inserted anything to the cart table as this is done by the script as the customers add and remove books from the cart. Is this correct?

Please find below my scripts. Sorry...I'm quite new to PHP programming and also to this comprehensive forum.

Thanks,
Steve

list_items.php

Code: Select all

//use db_conn.php code to connect database
<?php
include ("db_conn.php");
//get the info from cart
$result = mysql_query("select * from cart");
$num_row = mysql_num_rows($result);
$total_price=0;
//calculate the total price of all product
while ($row = mysql_fetch_array($result))
{
$total_price +=$row['bookPrice'];
} ?>



<table width="100%">
<tr><td width="100%" bgcolor="#0066FF">&nbsp;</td></tr>
<tr><td bgcolor="#00FF00" align="center"><? echo "You have <b style='color:#FF0000'>" . $num_row . " </b>item(s) in cart with total of <b style='color:#FF0000'>$ " . $total_price . "</b>" ?></td></tr>
</table>

</br></br></br><p style="font-size:38px; font-weight:bold; color:#00CCFF" align="center">Look @ our low price deals!</p></br></br></br></br>

<?php
$result = mysql_query("select * from books");
//display all the items
while($row = mysql_fetch_array($result))
{
?>

<table width="100%">
<tr bgcolor="#0099CC"><td><? echo "<p style='background-color:#0066CC'>[" . $row['bookId'] . "] - " . $row['bookTitle'] . "</p>"; ?></td></tr>
<tr bgcolor="#99CCFF"><td ><? echo "<p>Author " . $row['authoName'] . "</p>"; ?></td></tr>
<tr bgcolor="#9999CC"><td><? echo "<p>Our Price: " . $row['bookPrice'] . "</p>"; ?></td></tr>
</table>
<div align="right"><a href="http://mercury.it.swin.edu.au/hit3323/s5770165/test3/Assignment2/add_to_cart.php?item_id=<? echo $row['bookId']; ?>">Add to Cart</a></div>
<?
}

mysql_close($conn);
?>
<center><input type="button" value="Empty Cart" onClick=javascript:location.href="empty_cart.php" />
<input type="button" value="View Cart" onClick=javascript:location.href="view_cart.php" /></center>
</body>
</html>



add_to_cart.php

Code: Select all

<?php
$item_id = $_GET['bookId'];
//use the db_conn.php code to connect the database.
include ("db_conn.php");
//get both result from cart and items tables
$cart_result = mysql_query("select * from cart where bookId=$item_id");
$items_result = mysql_query("select * from books where bookId=$item_id");
//if row of result is equal to one.
if (mysql_num_rows($cart_result)==1)
{
$cart_row = mysql_fetch_array($cart_result);
$item_qty= $cart_row['bookQty']+1;

$items_row = mysql_fetch_array($items_result);

$item_price=$items_row['bookPrice'];

$total_price=$item_price * $item_qty;

$update = mysql_query("update cart set bookQty=$item_qty , bookPrice=$total_price where bookId=$item_id");
}
//if not
else
{
$items_row = mysql_fetch_array($items_result);
$item_name=$items_row['bookTitle'];
$item_price=$items_row['bookPrice'];
echo "Book Title:" . $item_name . $item_price;
$insert = mysql_query("insert into cart (bookId, bookTitle, bookQty, bookPrice) values ('$item_id', '$item_name', '1', '$item_price')");
}
//close database connection
mysql_close($conn);

?>

<html>
<head><title>Add to cart</title></head>
<body>
<script language="javascript">
location.href="list_items.php";
</script>
</body>


view_cart.php

Code: Select all

<?php
//use the db_conn code.
include ("db_conn.php");
//query all the info from cart table.
$result = mysql_query("select * from cart order by bookId");
$num_rows = mysql_num_rows($result);
$count=0;
//get each row from the cart and display to web pages
while ($row = mysql_fetch_array($result))
{
$total_price =$row['bookPrice'];
$item_qty = $row['bookQty'];
$item_name = $row['bookTitle'];
$total+=$total_price;
//use a form to send to update_cart.php
?>
<form action="update_cart.php" method="post">
<tr bgcolor="#999999"><td><input type="text" value= <? echo $item_qty; ?> name=<? echo "qty" . $count ?> /></td>
<td><? echo $item_name; ?> </td>
<td><? echo $total_price/$item_qty; ?></td>
<td><? echo $total_price; ?></td></tr>


<?
$count++;
}
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]