Okay I have a couple of scripts for a shopping system. I have the product list and the index working fine, but my cart script is going all crazy on me.
Here is the db.php connection script (giving me the "Fatal error: Cannot redeclare connecttodb() (previously declared in /myhost/db.php:11) in /myhost/db.php on line 11" error)
<?php
// This page contains the connection routine for the
// database as well as getting the ID of the cart, etc
$dbServer = "server";
$dbUser = "user";
$dbPass = "pass";
$dbName = "dbname";
function ConnectToDb($server, $user, $pass, $database)
{
// Connect to the database and return
// true/false depending on whether or
// not a connection could be made.
$s = mysql_connect($server, $user, $pass);
$d = mysql_select_db($database, $s);
if(!$s || !$d)
return false;
else
return true;
}
function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID
session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 7));
return session_id();
}
}
?>
and my cart script (may or may not have a load of errors in it, I am kind of out of it at work today):
<?php
include("db.php");
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId, $qty)
{
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add itwith an insert query
@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
function UpdateItem($itemId, $qty)
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
function RemoveItem($itemId)
{
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
function ShowCart()
{
$result = mysql_query("select * from cart inner join items on cart.itemId = " . $_SESSION["table"] . ".itemId where cart.cookieId = '" . GetCartId() . "' order by " . $_SESSION["table"] . ".itemName asc");
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
<?php
}
}
}?>
I just can't figure it out >_<
Thanks!