Jump to content

[SOLVED] PHP SHOPPING CART giving unexpected $end error ???!!


Recommended Posts

Hi all,

 

I copied this code from a Site's Shopping Cart Tutorial and it doens't work! Yes, I copied it carefully and followed all the advice! But, I just get this error -

Parse error: syntax error, unexpected $end in C:\Program Files\ZSOFT\XAMPP\xampp\htdocs\cart.php on line 159

 

 

Any ideas?

 

CART.PHP

 

 


<?php


include("menu.php"); 
include("SQL2.php"); 

?>

<script language="JavaScript">

function UpdateQty(item)
{
productid = product.name;
newQty = product.options[product.selectedIndex].text;

document.location.href = 'cart.php?action=update_item&id='+productid+'&qty='+newQty;
}

</script>

<?php
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 productid = $productid");

	$row = mysql_fetch_row($result);
	$numRows = $row[0];

	if($numRows == 0)
	{
	// This item doesn't exist in the users cart,
	// we will add it with an insert query

	@mysql_query("INSERT INTO cart(cookieid, productid, qty) values('" . GetCartId() . "', $productid, $qty)");
	}
	else
	{
	// This item already exists in the users cart,
	// we will update it instead

	UpdateItem($productid, $qty);
}

function UpdateItem($productid, $qty)
{
mysql_query("UPDATE cart set qty = $qty where cookieid = '" . GetCartId() . "' and productid = $productid");	
}

function RemoveItem($productid)
{
mysql_query("DELETE from cart where cookieId = '" . GetCartId() . "' and productid = $productid");
}

function ShowCart()
{
$result = mysql_query("select * from cart inner join product on cart.productid = product.productid where cart.cookieId = '" . GetCartId() . "' order by product.name asc");



while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["price"]);
?>
<html>
<head></head>
<body>
<table>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["productid"]; ?>" onChange="UpdateQty(this)">

<?php

for($i = 1; $i <= 5; $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["name"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["price"], 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["productid"]; ?>">Remove</a>
</font>
</td>
</tr>

<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="home.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: <?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>
</table>
</body>
</html>


 

SQL2.PHP

 


<html>
<head>

<title>Connector</title>
</head>

<body>

  <?php
  	mysql_connect ("localhost", "root" , "zforce") or die (mysql_error());
mysql_select_db("express") or die (mysql_error());

function GetCartId()
{
if (isset($_COOKIE["cartid"]))
{
	return $_COOKIE["cartid"];
}
else
{
	session_start();
	setcookie("cartid",session_id(),time()+((3600*24)*2) );
	return session_id();
}
}

  ?>





</body>
</html>

Thanks Intelly and esukf. Two { 's solved the problem :)

 

Now, I got another problem.

 

I get this error now:

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\ZSOFT\XAMPP\xampp\htdocs\menu.php: in C:\Program Files\ZSOFT\XAMPP\xampp\htdocs\SQL2.php on line 21

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\ZSOFT\XAMPP\xampp\htdocs\menu.php: in C:\Program Files\ZSOFT\XAMPP\xampp\htdocs\SQL2.php on line 22

 

Something to do with the Session starting in SQL2.php.

 

Could it be that it's clashing with the other Session I started when I logged in from login.php ?

 

Here are the session parts

 

LOGIN.PHP

 

session_register("usern");
session_register("passw");
header("location:acc.php");	 

 

ACC.PHP ( Each Member's Individual MY ACCOUNT area )

 

<?php
###### IF session is not registered , redirect to Homepage of site #####
session_start();
if(!session_is_registered(usern))
{
header("location:home.php");
}

 

 

And this might also be useful, here's the code for a Product - where I click ADD and it's added to the Shopping Cart.

 

<?php


include("menu.php"); 
include("SQL2.php"); 

$sql = mysql_query("SELECT * FROM product WHERE productid = '1'")
or die(mysql_error());
$rows=mysql_fetch_array($sql);

?>

<html>

<head>
<title>Core 2 Duo</title>
<link rel="stylesheet" href="styler.css">
</head>

<body>
<br><br><br>

<table border="0" width="87%" cellspacing="1" id="table1">
<tr>
	<td rowspan="6">
	<img src="http://www.webuildsolutions.com/images/products/intel-core2duo-lga775-box.jpg"></td>
	<td width="398" colspan="2">Intel Core 2 Duo E6300</td>
</tr>
<tr>
	<td width="398" colspan="2"> </td>
</tr>
<tr>
	<td width="118">Clock Speed</td>
	<td width="277"> 3.72 GHz  ( 2 X 1.86 GHz)</td>
</tr>
<tr>
	<td width="118">Bus Speed</td>
	<td width="277">1066 MHz</td>
</tr>
<tr>
	<td width="118">L2 Cache</td>
	<td width="277">2 MB </td>
</tr>
<tr>
	<td width="118">Features</td>
	<td width="277">Enhanced Speedstep Technology<br>Intel Extended Memory 
	64 Technology<br>Intel Virtualization Technology<br>Intel ViiV 
	Technology</td>
</tr>
<tr>
	<td>
	<p align="center">         
	<i><p align="center"><? echo $rows['stock']; ?> in Stock   </i>      
	<p align="center">£ <? echo $rows['price']; ?> inc. VAT   
	<a href ="cart.php?action=add_item@id=<?php echo $rows["productid"]; ?> &qty=1" > <img src = "add.jpg"> </a>
	</td>
	<td width="118">Warranty</td>
	<td width="277">3 Years</td>
</tr>
</table>
<br><br><br>	
<?php include("base.php"); ?>
</body>

</html>

 

 

Help would be appreciated again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.