I'm setting up a shopping cart what I,m trying to do is...
when a user add a item the script should check to see if the item is already added to the cart if it's not add it and display the cart. if item exist them add 1 to the qty field. and display the cart.
two problems i have one If the item exist it add one but dosen't display the cart
second it only increments from 1 - 2 after that it stay on two.
heres the script I'm using if anybody can help or show me a better way to do this would be great
<? // Check to see if require field pass correctly
if ((!($sel_product)) || (!($sel_price)) || (!($sel_qty))) {
Header("location: site/error1.php3");
exit;
}
?>
<? if (!isset($user_id)) {
$token = MD5(uniqid(rand()));
setcookie("user_id",$token,time()+86400,"/","site.com");
}
?>
<?
<!-- start cart functions -->
<? // start by checking if product has been added to cart already
// if so then update the $sel_qty field by one (1)
$result = mysql_query("SELECT * FROM user_track WHERE sel_product = '$sel_product' and user_id = '$user_id'");
//if rows are returned, then product
//exists in shopping cart already
if (mysql_num_rows($result)) {
// set qty plus 1
$sel_qty = $sel_qty + 1;
$update = mysql_query("UPDATE user_track SET
sel_qty='$sel_qty' WHERE sel_product = '$sel_product'");
} else {
$insert = "INSERT INTO user_track VALUES (
\"$user_id\",
\"$sel_product\",
\"$sel_price\",
\"$sel_qty\",
\"$sel_product_id\",
\"$sel_price_total\",
\"$pdate\")";
// excute the sql statement or exit of error
$sql_result = mysql_query($insert,$conn) or die ("<BR><BR><BR><B>Error addind to cart");
// print error
print mysql_error();
?>
<div align="center">
<center>
<table border="0" cellpadding="2" cellspacing="1" width="95%" height="21">
<tr>
<td width="15%" height="17" bordercolor="#000000" bgcolor="#000000">
<p align="center"><font size="1" color="#FFFFFF">Product ID</font></td>
<td width="42%" height="17" bordercolor="#000000" bgcolor="#000000">
<p align="center"><font size="1" color="#FFFFFF">Product Name</font></td>
<td width="13%" height="17" bordercolor="#000000" bgcolor="#000000">
<p align="center"><font size="1" color="#FFFFFF">Price</font></td>
<td width="5%" height="17" bordercolor="#000000" bgcolor="#000000">
<p align="center"><font size="1" color="#FFFFFF">Qty</font></td>
<td width="12%" height="17" bordercolor="#000000" bgcolor="#000000">
<p align="center"><font size="1" color="#FFFFFF">Total</font></td>
<td width="13%" height="17" bordercolor="#000000" bgcolor="#000000">
<p align="center"><font size="1" color="#FFFFFF">Delete Item</font></td>
</tr>
</table>
</center>
</div>
<? // loop cart item according to user_id
$result = mysql_query("SELECT * FROM user_track WHERE user_track.user_id = '$user_id'");
while ($myrow = mysql_fetch_array($result)) {
print mysql_error();
print "<div align=\"center\">";
print "<center>";
print "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"95%\" height=\"21\">";
print "<tr>";
print "<td width=\"15%\" height=\"17\" bordercolor=\"#000000\" bgcolor=\"#FFFFFF\">";
print "<p align=\"center\"><font size=\"1\" color=\"#000000\">$myrow[sel_product_id]</font></td>";
print "<td width=\"42%\" height=\"17\" bordercolor=\"#000000\" bgcolor=\"#FFFFFF\">";
print "<p align=\"center\"><font size=\"1\" color=\"#000000\">$myrow[sel_product]</font></td>";
print " <td width=\"13%\" height=\"17\" bordercolor=\"#000000\" bgcolor=\"#FFFFFF\">";
print "<p align=\"center\"><font size=\"1\" color=\"#000000\">$$myrow[sel_price]</font></td>";
print "<td width=\"5%\" height=\"17\" bordercolor=\"#000000\" bgcolor=\"#FFFFFF\">";
print "<CENTER><FONT SIZE=1 FACE=\"Arial\">$myrow[sel_qty]</FONT></CENTER></TD>";
print "<td width=\"12%\" height=\"17\" bordercolor=\"#000000\" bgcolor=\"#FFFFFF\">";
print "<center><FONT SIZE=1 FACE=\"Arial\">$myrow[sel_price_total]</FONT>";
print "<td width=\"13%\" height=\"17\" bordercolor=\"#000000\" bgcolor=\"#FFFFFF\">";
print "<center><FONT SIZE=1 FACE=\"Arial\">Delete Item</FONT>
</TD></form>";
print "</tr>";
print "</table></center></div>";
}
?>
<? } ?>
<? end_body(); ?>
<?
footer_notes();
?>
TIA
Richie TM