Jump to content

Cart Problem. Urgent Help needed.


OilSheikh

Recommended Posts

I have this code of a Shopping cart based on an article at www.devarticles.com

 

http://www.devarticles.com/c/a/MySQL/Building-A-Persistent-Shopping-Cart-With-PHP-and-MySQL/

 

However, I just can't get this to work. I would appreciate if someone helped me.

 

This is the error message that I get :

 

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

 

Now, the shopping cart has 3 files - CART.PHP , SQL2.PHP ( connects to database) and a E6300.PHP ( Core 2 Duo E6300 Item that is to be added to the cart )

 

Here are the codes for each :

 

E6300.PHP

 


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

<?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>

<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>

 

 

 

SQL2.PHP


<?php
ob_start();

  	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
{


	setcookie("cartid",session_id(),time()+((3600*24)*2) );
	return session_id();
}
}
ob_flush();
?>


 

 

CART.PHP

<html>
<head><title>Your Shopping Cart's contents</title></head>

<?php

include("SQL2.php");
include("menu.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"]);
?>

<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>
<?php
}
}
?>	


 

Help is urgently needed and of course, more than appreciated.

Link to comment
https://forums.phpfreaks.com/topic/53161-cart-problem-urgent-help-needed/
Share on other sites

All headers including session coookie must be sent before something is printed. You print

<head>

<title>Core 2 Duo E6300</title>

<link rel="stylesheet" href="styler.css">

</head>

before calling session_start in SQL2.PHP because all that is not in a <?php ?> tag is printed.

You need to include SQL2.PHP before <head>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.