I am learning to create myself a small shopping cart because its something I have always wanted to do.
What I really want to know is if the code below is acceptable.
The code below works, what it does is if the cookie id and id already exists in the cart table then 1 is added to the existing quantity and then displayed on the cart. If the cookieid and id does not exist then a new entry is created in the cart.
I know the code is very amateurish.
// include database connection information
include "conn.php";
$dbcnx = @mysql_connect("$servername","$username", "$password");
if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" );
exit(); }
if (! @mysql_select_db("s// Connect to databaseb") ) {
echo( "<P>Unable to locate the sb " . "database at this time. Please contact the admininstator</p>" );
exit(); }
$result = mysql_query("SELECT * FROM cart WHERE cookieid ='$cookieid' AND id='$id'");
$num = mysql_numrows($result);
if ($num == 0) {
mysql_connect($servername,$username,$password);
@mysql_select_db($database) or die( "Unable to select database sb");
$query = "INSERT INTO cart VALUES ('$cookieid','$id','$product_name','$product_description','$qty','$price')";
mysql_query($query);
mysql_close();
require "showcart.php";
}
while ($row = mysql_fetch_array($result) ) {
$ud_cookieid = $cookieid;
$ud_id = $id;
$ud_product_name = $product_name;
$ud_product_description = $product_description;
$ud_qty = $row['qty'] + 1;
$ud_price = $price;
mysql_connect($servername,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "UPDATE cart SET cookieid='$ud_cookieid', product_name='$ud_product_name', product_description='$ud_product_description', qty='$ud_qty', price='$ud_price' WHERE id='$ud_id'";
mysql_query($query);
mysql_close();
require "showcart.php";
}