This little project involves two scripts
- shop.php
- entry.php
entry gets data from the user and a database
then shop takes the data and manipulates it and adds it to the table carts in the db shop.
everything works fine save three things (that i know of)
1. If no cookie exists and the submit button is pressed an error is given about the $added string. (a problem i can fix)
2. As the first item is entered and displayed in the cart everything shows fine. When a second item is entered only the cell with the new item is displayed properly. The "old" items all show fine except for the ($qty * $price) cell, which shows grey with no text (this is my most frustrating problem)
3. Finally ... When i pull from a cookie that is not there i have the script to make a cookie and put the $session value into it. How do I stop the error message telling me there is no cookie from showing "Undefined index: sessionid in d:\shopp..."
ive been working on these errors for a few days
maybe somebody can figure this out and make me feel stupid.
thanks for all your help php gurus.
shop.php (accessed second)
<?
session_start();
ob_start();
$total = 0;
$ipaddress = getenv('REMOTE_ADDR');
echo $ipaddress."<br>";
if ($sessionver == 2)
{
$session = md5(uniqid(rand()));
setcookie("sessionid", $session, time() + 300);
}
if ($sessionver == 3)
{
$session = $HTTP_COOKIE_VARS["sessionid"];
}
//*********************************************************************************
?>
<form action="shop.php" method=post>
<table bgcolor=#C0C0C0 border=2 width="612" height="1" style="border-collapse: collapse" bordercolor="#FFFFFF" cellpadding="0" cellspacing="0">
<tr>
<td align=center width="84" height="26" bgcolor="#000000" style="color: #00FF00">
Quantity</td>
<td align=center width="1968" height="26" bgcolor="#000000" style="color: #00FF00">
Product Description</td>
<td align=center width="322" height="26" bgcolor="#000000" style="color: #00FF00">
Each</td>
<td align=center width="305" height="26" bgcolor="#000000" style="color: #00FF00">
Total</td>
</tr>
<?
//*****************************************************************
@ $db = mysql_pconnect("localhost","user","pass");
if (!$db) { echo "Error: Could not connect to database."; exit; }
mysql_select_db("shop");
//*****************************************************************
$query = "select * from products
where pdesc = '".$product."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$prodprice = stripslashes($row["pprice"]);
//**add to cart***
if ($quantity > 0)
{
$session = addslashes($session);
$product = addslashes($product);
$quantity = addslashes($quantity);
$query = "insert into carts values
('".$product."', ".$quantity.", '".$session."', NULL, ".$prodprice.")";
mysql_query($query);
}
//**show cart**
$query = "select * from carts where session = '".$session."'";
$result = mysql_query($query);
if($result)
{
$num_results = mysql_num_rows($result);
}
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result); ?>
<tr>
<td align=center width="84" height="26" bgcolor="#C0C0C0">
<? $added = stripslashes($row["qty"]) stripslashes($row["price"]);
echo htmlspecialchars (stripslashes($row["qty"]));?></td> <?/ FIX THE OVERCOLORED GREY CELLS ON ADDING ITEMS /?>
<td align=left width="1968" height="26" bgcolor="#C0C0C0"> <?/ FIX THE $added ERRORS WHEN CART IS EMPTY */?>
<?echo htmlspecialchars (stripslashes($row["product"])); ?>
</td>
<td align=center width="322" height="26" bgcolor="#C0C0C0">
<?
$total = $total + $added;
echo " $"; echo htmlspecialchars (stripslashes($row["price"]));}?> </td>
<td align=center width="305" height="26" bgcolor="#C0C0C0"><?echo "$".$added;?>
</td>
</tr>
<tr>
<td align=center width="84" height="26" bgcolor="#C0C0C0">
<p align="left">
</td>
<td align=right width="1968" height="26" bgcolor="#C0C0C0">
</td>
<td align=center width="322" height="26" bgcolor="#C0C0C0">
<b>Sub Total:</b></td>
<td align=center width="305" height="26" bgcolor="#C0C0C0"><?echo "$".$total;?>
</td>
</tr>
<tr>
<td align=center width="84" height="26" bgcolor="#C0C0C0">
</td>
<td align=right width="1968" height="26" bgcolor="#C0C0C0">
</td>
<td align=center width="322" height="26" bgcolor="#C0C0C0">
<b>Tax:</b></td>
<td align=center width="305" height="26" bgcolor="#C0C0C0"> </td>
</tr>
<tr>
<td align=center width="84" height="26" bgcolor="#C0C0C0">
</td>
<td align=right width="1968" height="26" bgcolor="#C0C0C0">
</td>
<td align=center width="322" height="26" bgcolor="#C0C0C0">
<b>Total:</b></td>
<td align=center width="305" height="26" bgcolor="#C0C0C0"> </td>
</tr>
</table>
</form>
entry.php (accessed first)
<?
session_start();
session_register("sessionver","prodprice");
if(!$HTTP_COOKIE_VARS["sessionid"])
{
$sessionver = 2;
}
else
{
$sessionver = 3;
}
?>
<body bgcolor=#FFFFFF>
<form action="shop.php" method=post>
<table bgcolor=#778899 border=0 width="383" height="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<tr>
<td align=center width="84" height="26" bgcolor="#000000" style="color: #00FF00">
Quantity</td>
<td align=center width="1001" height="26" bgcolor="#000000" style="color: #00FF00">
Product Description</td>
</tr>
<tr>
<td align=center width="84" height="26" bgcolor="#C0C0C0">
<!--webbot bot="Validation" s-data-type="Integer" s-number-separators="," b-value-required="TRUE" i-maximum-length="4" s-validation-constraint="Greater than or equal to" s-validation-value="0" --><input type="text" name="quantity" size="6" value="0" maxlength="4" style="text-align: right"></td>
<td align=left width="1001" height="26" bgcolor="#C0C0C0">
<select size="1" name="product">
<?
@ $db = mysql_pconnect("localhost","user","pass");
if (!$db) { echo "Error: Could not connect to database."; exit; }
mysql_select_db("shop");
$query = "select * from products";
$result = mysql_query($query);
if($result)
{
$num_results = mysql_num_rows($result);
}
for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
?>
<option value="<?echo htmlspecialchars (stripslashes($row["pdesc"]));?>"><?echo htmlspecialchars (stripslashes($row["pdesc"])); echo " - $"; echo htmlspecialchars (stripslashes($row["pprice"]));}?></option>
</select></td>
</tr>
<tr>
<td align=center width="84" height="26" bgcolor="#C0C0C0">
<p align="left">
</td>
<td align=right width="1001" height="26" bgcolor="#C0C0C0">
<input type=submit value="Add to Cart"></td>
</tr>
</table>
</form>
<p> </p>
</body>