Undefined Index & Cannot send session header errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Undefined Index & Cannot send session header errors

Post by dardsemail »

Hi,

I'm having some problems with the following code - I get a couple of errors:
Notice: Undefined index: action in cart.php on line 26
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at cart.php:26) in cart.php on line 16
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at cart.php:26) in cart.php 16
Warning: Cannot modify header information - headers already sent by (output started at cart.php:26) in cart.php on line 17
The 'action' is set when the user adds an item to the cart from a collections page. I'm not sure whether or not this can be set as a default on the page somewhere.

Regarding setting the cookies - would this be better to be in an include file somewhere? Is it getting called repeatedly thus the error?

Thanks!

Here is the code (the WHOLE page!):

Code: Select all

<?php
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) * 30));
			return session_id();
		}
	}

	require_once('Connections/conn_mydb_db.php');
	
	error_reporting(E_ALL);
		
	switch($_GET["action"])
	{
		case "add_item":
		{
			AddItem($_GET["piecenum"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "update_item":
		{
			UpdateItem($_GET["piecenum"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "remove_item":
		{
			RemoveItem($_GET["piecenum"]);
			ShowCart();
			break;
		}
		default:
		{
			ShowCart();
		}
	}

	function AddItem($piecenum, $qty)
	{
		// Will check whether or not this item
		// already exists in the cart table.
		// If it does, the UpdateItem function
		// will be called instead
		
		// Check if this item already exists in the users cart table
		$result = mysql_query("SELECT count(*) FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum"); 
		$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, piecenum, qty) VALUES('" . GetCartId() . "', $piecenum, $qty)");
		}
		else
		{
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($piecenum, $qty);
		}
	}
	
	function UpdateItem($piecenum, $qty)
	{
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead


		// Get a connection to the database
		
		if($qty == 0)
		{
			// Remove the item from the users cart
			RemoveItem($piecenum);
		}
		else
		{
			mysql_query("UPDATE cart SET qty =  $qty WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
		}
	}
	
	function RemoveItem($piecenum)
	{
		// Uses an SQL delete statement to remove an item from
		// the users cart
		
		mysql_query("DELETE FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
	}
	
	function ShowCart()
	{
		// Gets each item from the cart table and display them in
		// a tabulated format, as well as a final total for the cart
		
		$totalCost = 0;
		$result = mysql_query("SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'");
		?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Shopping Cart</title>
<script language="JavaScript">
		
			function UpdateQty(item)
			{
				itemId = item.name;
				newQty = item.options[item.selectedIndex].text;
				
				document.location.href = 'cart.php?action=update_item&piecenum='+itemId+'&qty='+newQty;
			}
		
		</script>
<!-- End Preload Script -->
<style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
</style>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body onload="preloadImages();">
<table style="height:100%" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
  <td style="vertical-align:top;"><table style="height:76px;" width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td style="background-color: rgb(102,153,163); width:125px; height:80px;"><a href="index.php"><img src="images/Logo.gif" alt=" " width="125" height="75" border="0" align="middle" /></a></td>
        <td><table style="height:100%;" width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td style="background-color: rgb(126, 127, 117); height:40px" align="center"><!-- ImageReady Slices (TopNav.psd) -->
                <img
 src="images/TopNav.gif" alt=""
 name="TopNav_01" width="500" height="38"
 border="0" align="middle" usemap="#TopNav_01_Map" id="TopNav_01" />
                <map
 name="TopNav_01_Map">
                  <area shape="rect" alt="" coords="403,6,487,31"
 href="contactus.php"
 onmouseover="changeImages('TopNav_01', 'images/TopNa-06.gif'); return true;"
 onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
                  <area shape="rect" alt="" coords="293,5,376,34"
 href="aboutus.php"
 onmouseover="changeImages('TopNav_01', 'images/TopNa-05.gif'); return true;"
 onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
                  <area shape="rect" alt="" coords="207,8,258,33"
 href="orders.php"
 onmouseover="changeImages('TopNav_01', 'images/TopNa-04.gif'); return true;"
 onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
                  <area shape="rect" alt="" coords="86,7,169,30"
 href="collections.php?categorydesc=Sets"
 onmouseover="changeImages('TopNav_01', 'images/TopNa-03.gif'); return true;"
 onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
                  <area shape="rect" alt="" coords="10,8,62,31"
 href="index.php"
 onmouseover="changeImages('TopNav_01', 'images/TopNa-02.gif'); return true;"
 onmouseout="changeImages('TopNav_01', 'images/TopNav_0.gif'); return true;" />
                </map>
                <!-- End ImageReady Slices --></td>
            </tr>
            <tr>
              <td style="background-color: rgb(221, 222, 194); height:40px;" align="center"></td>
            </tr>
          </table></td>
      </tr>
    </table></td>
</tr>
<tr>
  <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td width="25%" align="right" valign="top"><a href="login.php"><img src="images/login.gif" border="0" /></a><img src="images/hyphen.gif" /><a href="registration.php"><img src="images/register.gif" border="0" /></a>&nbsp;&nbsp;&nbsp;</td>
      </tr>
    </table>
<tr>
  <td align="center"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td></td>
      </tr>
      <tr>
        <td align"center" width="15%" height="25"><h1>Your Shopping Cart</h1>
          <form name="frmCart" method="get">
            <table align="center" width="80%" cellspacing="0" cellpadding="0" border="0">
              <tr>
                <td width="15%" height="25"><h4>Qty</h4></td>
                <td width="55%" height="25"><h4>Product</h4></td>
                <td width="20%" height="25"><h4>Price Each</h4></td>
                <td width="10%" height="25"><h4>Remove?</h4></td>
              </tr>
              <?php
			
			while($row = mysql_fetch_array($result))
			{
				// Increment the total cost of all items
				$totalCost += ($row["qty"] * $row["price"]);
				?>
              <tr>
                <td width="15%" height="25"><select name="<?php echo $row["piecenum"]; ?>" onChange="UpdateQty(this)">
                    <?php
								
									for($i = 1; $i <= 20; $i++)
									{
										echo "<option ";
										if($row["qty"] == $i)
										{
											echo " SELECTED ";
										}
										echo ">" . $i . "</option>";
									}
								?>
                  </select>
                </td>
                <td width="55%" height="25"><h3> <?php echo $row["description"]; ?> </h3></td>
                <td width="20%" height="25"><h3> $<?php echo number_format($row["price"], 2, ".", ","); ?> </h3></td>
                <td width="10%" height="25"><a href="cart.php?action=remove_item&piecenum=<?php echo $row["piecenum"]; ?>">Remove</a> </td>
              </tr>
              <?php
			}
			
			// Display the total
			?>
              <tr>
                <td width="100%" colspan="4"></td>
              </tr>
              <tr>
                <td width="70%" colspan="2"><br />
                  <a href="collections.php">Keep Shopping</a> </td>
                <td width="30%" colspan="2"><br />
                  <h4>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></h4></td>
				  <?php
	}
?>
              </tr>
            </table>
          </form></td>
      </tr>
      <tr>
        <td style="height:30px;" align="center"></td>
      </tr>
      <tr>
        <td style="background-color: rgb(126, 127, 117); height:20px;">&nbsp;</td>
      </tr>
      <tr>
        <td style="background-color: rgb(126, 127, 117); height:20px;">&nbsp;</td>
      </tr>
      <tr>
        <td style="text-align:center"><br />
           <br />
          <h3>Copyright © 2004 . All rights reserved. All graphics, pictures, contents, and jewelry designs are copyrighted and protected by US and international law.†Use, reproduction, or modification in any manner without written permission is prohibited.</h3></td>
      </tr>
    </table>
</body>
</html>
PAW Projects
Forum Commoner
Posts: 30
Joined: Tue Jun 15, 2004 7:43 am
Contact:

Post by PAW Projects »

Is that a newline at the very beginning of your file?
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

It looks like a blank line - but its not. The first line is the <?php and the second line is the function GetCartId line.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

What happens if you took the session_start() function out of the GetCartId() function and placed at the top of the script so the top would look like this

Code: Select all

<?php 
session_start();
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  
         setcookie("cartID", session_id(), time() + ((3600 * 24) * 30)); 
         return session_id(); 
      } 
   } 
//..........rest of script
Mark
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I still end up with the following error message:
Warning: Cannot modify header information - headers already sent by (output started at cart.php:26) in cart.php on line 17
Of course, I still end up with the Undefined Index and mysql_fetch_array errors... but that's to be expected.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what is on line 26 of cart.php?

Mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

change

Code: Select all

switch($_GET&#1111;"action"])
to

Code: Select all

switch(@$_GET["action"])
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

Bech100 - line 26 is the

Code: Select all

switch($_GET&#1111;"action"])
line.

Weirdan - I changed it and am now down to just two error messages: the undefined index: action message and the mysql_fetch_assoc message.

Getting there!
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

undefined index: action is a notice, not an error. If $_GET["action"] is not declared previously (e.g. in the URL), you will get that notice - but it has no implications on your code.

I would change your

error_reporting(E_ALL);

to

error_reporting (E_ALL ^ E_NOTICE);

as it seems that that notice is causing the problem, while it's not an error.
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I've changed that - thanks so much. That's better.

I'm still getting the mysql_fetch_array message which I believe is because I am not calling the correct function in the body of the page. Is this correct?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

change your function ShowCart() so that it is one coherent function, not a horrendous mixture of PHP broken up by HTML. Put the HTML in a file and use include().

Btw.: please use

Code: Select all

-tags instead of

Code: Select all

around PHP-code. I've changed it in your post - it makes reading much easier.
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

Sorry about the Code/PHP tags. My mistake. I did do some modification to the code and its come out bug free - however, its not showing the data. I guess my biggest thing is that I don't understand how to call a function from within the body of the page (thus the horrendous mixture of stuff!). I will move it to an include page if I can get it working. I've listed below my updated page which is running through a debugger clean, but not displaying any data.

Code: Select all

<?php
session_start();
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
			setcookie("cartID", session_id(), time() + ((3600 * 24) * 30));
			return session_id();
		}
	}

	require_once('Connections/conn_my_db.php');
	
	switch(@$_GET["action"]) 

	{
		case "add_item":
		{
			AddItem($_GET["piecenum"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "update_item":
		{
			UpdateItem($_GET["piecenum"], $_GET["qty"]);
			ShowCart();
			break;
		}
		case "remove_item":
		{
			RemoveItem($_GET["piecenum"]);
			ShowCart();
			break;
		}
		default:
		{
		ShowCart();
		}
	}

	function AddItem($piecenum, $qty)
	{
		// Will check whether or not this item
		// already exists in the cart table.
		// If it does, the UpdateItem function
		// will be called instead
		
		// Check if this item already exists in the users cart table
		$result = mysql_query("SELECT count(*) FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum"); 
		$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, piecenum, qty) VALUES('" . GetCartId() . "', $piecenum, $qty)");
		}
		else
		{
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($piecenum, $qty);
		}
	}
	
	function UpdateItem($piecenum, $qty)
	{
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead


		// Get a connection to the database
		
		if($qty == 0)
		{
			// Remove the item from the users cart
			RemoveItem($piecenum);
		}
		else
		{
			mysql_query("UPDATE cart SET qty =  $qty WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
		}
	}
	
	function RemoveItem($piecenum)
	{
		// Uses an SQL delete statement to remove an item from
		// the users cart
		
		mysql_query("DELETE FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
	}

function ShowCart()
{
$maxRows_ShowCart = 10;
$pageNum_ShowCart = 0;
if (isset($_GET['pageNum_ShowCart'])) {
  $pageNum_ShowCart = $_GET['pageNum_ShowCart'];
}
$startRow_ShowCart = $pageNum_ShowCart * $maxRows_ShowCart;

$colname_ShowCart = "1";
if (isset($_COOKIE['cookieID'])) {
  $colname_ShowCart = (get_magic_quotes_gpc()) ? $_COOKIE['cookieID'] : addslashes($_COOKIE['cookieID']);
}
$query_ShowCart = sprintf("SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'");
$query_limit_ShowCart = sprintf("%s LIMIT %d, %d", $query_ShowCart, $startRow_ShowCart, $maxRows_ShowCart);
$ShowCart = mysql_query($query_limit_ShowCart) or die(mysql_error());
$row_ShowCart = mysql_fetch_assoc($ShowCart);

if (isset($_GET['totalRows_ShowCart'])) {
  $totalRows_ShowCart = $_GET['totalRows_ShowCart'];
} else {
  $all_ShowCart = mysql_query($query_ShowCart);
  $totalRows_ShowCart = mysql_num_rows($all_ShowCart);
}
$totalPages_ShowCart = ceil($totalRows_ShowCart/$maxRows_ShowCart)-1;
}
 error_reporting (E_ALL ^ E_NOTICE);
	?>
Here is the code within the body of the page where I'm trying to call the function:

Code: Select all

&lt;table width="100%"  border="0" cellspacing="0" cellpadding="0"&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td align"center" width="15%" height="25"&gt;&lt;h1&gt;Your Shopping Cart&lt;/h1&gt;
          &lt;form name="frmCart" method="get"&gt;
            &lt;table align="center" width="80%" cellspacing="0" cellpadding="0" border="0"&gt;
              &lt;tr&gt;
                &lt;td width="15%" height="25"&gt;&lt;h4&gt;Qty&lt;/h4&gt;&lt;/td&gt;
                &lt;td width="55%" height="25"&gt;&lt;h4&gt;Product&lt;/h4&gt;&lt;/td&gt;
                &lt;td width="20%" height="25"&gt;&lt;h4&gt;Price Each&lt;/h4&gt;&lt;/td&gt;
                &lt;td width="10%" height="25"&gt;&lt;h4&gt;Remove?&lt;/h4&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;?php
              ShowCart();
			  do &#123; ?&gt;
              &lt;tr&gt;
                &lt;td width="15%" height="25"&gt;&lt;select name="&lt;?php echo $row&#1111;"piecenum"]; ?&gt;" onChange="UpdateQty(this)"&gt;                    &lt;?php
								
									for($i = 1; $i &lt;= 20; $i++)
									&#123;
										echo "&lt;option ";
										if($row&#1111;"qty"] == $i)
										&#123;
											echo " SELECTED ";
										&#125;
										echo "&gt;" . $i . "&lt;/option&gt;";
									&#125;
								?&gt;
                  &lt;/select&gt;&lt;/td&gt;
                &lt;td width="55%" height="25"&gt;&lt;?php echo $row_ShowCart&#1111;'description']; ?&gt;&lt;/td&gt;
                &lt;td width="20%" height="25"&gt;&lt;?php echo $row_ShowCart&#1111;'price']; ?&gt;&lt;/td&gt;
                &lt;td width="10%" height="25"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;?php &#125; while ($row_ShowCart = mysql_fetch_assoc($ShowCart)); ?&gt;
              &lt;tr&gt;
               &lt;td width="100%" colspan="4"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td width="70%" colspan="2"&gt;&lt;br /&gt;
                  &lt;a href="collections.php"&gt;&lt;&lt; Keep Shopping&lt;/a&gt; &lt;/td&gt;
                &lt;td width="30%" colspan="2"&gt;&lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;
I feel like I'm close, but so far away! I'm just learning and it can be very frustrating!!! I keep playing around with the ShowCart function to get it to work, but can't seem to get the syntax right such that:

1) I have the function clearly defined in the header or an include file (I can't seem to get a connection to the database when I do this).

2) I am not sure how to call the function from within the page (down where I have the repeat code defined to pull the data from the database).

Any help would be greatly appreciated!!!
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I've been playing with my code and managed to get rid of the 'cannot send header...' error. I really appreciate the awesome tutorial on that. I placed the ob_start() at the beginning of the document and it worked beautifully.

Now I'm just down to one error -
mysql_fetch_array(): supplied argument is not a valid MySQL result resource in cart.php on line 213
Line 213 is the following line from the body of the page code below (not the PHP section, but the next section):

Code: Select all

while($row = mysql_fetch_array($result));
Here is the php code:

Code: Select all

<?php
ob_start();
include("include.php");
	
require_once('Connections/conn_mydb_db.php');
	
	switch(@$_GET["action"]) 

	{
		case "add_item":
		{
			AddItem($_GET["piecenum"], $_GET["qty"]);
			break;
		}
		case "update_item":
		{
			UpdateItem($_GET["piecenum"], $_GET["qty"]);
			break;
		}
		case "remove_item":
		{
			RemoveItem($_GET["piecenum"]);
			break;
		}
	}

	function AddItem($piecenum, $qty)
	{
		// Will check whether or not this item
		// already exists in the cart table.
		// If it does, the UpdateItem function
		// will be called instead
		
		// Check if this item already exists in the users cart table
		$result = mysql_query("SELECT count(*) FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum"); 
		$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, piecenum, qty) VALUES('" . GetCartId() . "', $piecenum, $qty)");
		}
		else
		{
			// This item already exists in the users cart,
			// we will update it instead
			
			UpdateItem($piecenum, $qty);
		}
	}
	
	function UpdateItem($piecenum, $qty)
	{
		// Updates the quantity of an item in the users cart.
		// If the qutnaity is zero, then RemoveItem will be
		// called instead


		// Get a connection to the database
		
		if($qty == 0)
		{
			// Remove the item from the users cart
			RemoveItem($piecenum);
		}
		else
		{
			mysql_query("UPDATE cart SET qty =  $qty WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
		}
	}
	
	function RemoveItem($piecenum)
	{
		// Uses an SQL delete statement to remove an item from
		// the users cart
		
		mysql_query("DELETE FROM cart WHERE cookieID = '" . GetCartId() . "' AND piecenum = $piecenum");
	}

	function ShowCart()
	{
		$totalCost = 0;
		$result = mysql_query("SELECT * FROM cart INNER JOIN pieces ON cart.piecenum=pieces.piecenum WHERE cookieID='" . GetCartId() . "'");
		echo ($result);
	}
		error_reporting (E_ALL ^ E_NOTICE);
?>
Further down the page, I'm trying to call the 'ShowCart' function but I've never done this before and it doesn't seem to be working properly.

Here's the body of the page code:

Code: Select all

&lt;table width="100%"  border="0" cellspacing="0" cellpadding="0"&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td align"center" width="15%" height="25"&gt;&lt;h1&gt;Your Shopping Cart&lt;/h1&gt;
          &lt;form name="frmCart" method="get"&gt;
            &lt;table align="center" width="80%" cellspacing="0" cellpadding="0" border="0"&gt;
              &lt;tr&gt;
                &lt;td width="15%" height="25"&gt;&lt;h4&gt;Qty&lt;/h4&gt;&lt;/td&gt;
                &lt;td width="55%" height="25"&gt;&lt;h4&gt;Product&lt;/h4&gt;&lt;/td&gt;
                &lt;td width="20%" height="25"&gt;&lt;h4&gt;Price Each&lt;/h4&gt;&lt;/td&gt;
                &lt;td width="10%" height="25"&gt;&lt;h4&gt;Remove?&lt;/h4&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;?php
              ShowCart();
              while($row = mysql_fetch_array($result));
              &#123;?&gt;
              &lt;tr&gt;
                &lt;td width="15%" height="25"&gt;&lt;/td&gt;
                &lt;td width="55%" height="25"&gt;&lt;?php echo $row&#1111;'description']; ?&gt;&lt;/td&gt;
                &lt;td width="20%" height="25"&gt;&lt;?php echo $row&#1111;'price']; ?&gt;&lt;/td&gt;
                &lt;td width="10%" height="25"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;?php &#125;?&gt;
              &lt;tr&gt;
               &lt;td width="100%" colspan="4"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td width="70%" colspan="2"&gt;&lt;br /&gt;
                  &lt;a href="collections.php"&gt;&lt;&lt; Keep Shopping&lt;/a&gt; &lt;/td&gt;
                &lt;td width="30%" colspan="2"&gt;&lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;
Incidentally, here is the include.php code just in case:

Code: Select all

<?php
session_start();
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 
setcookie("cartID", session_id(), time() + ((3600 * 24) * 30)); 
return session_id(); 

} 
}
?>
I think I'm on the right track, but just can't seem to get the damn data to appear on the page!!!

Help!!!! Please!!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dardsemail wrote:
mysql_fetch_array(): supplied argument is not a valid MySQL result resource in cart.php on line 213
Line 213 is the following line from the body of the page code below (not the PHP section, but the next section):

Code: Select all

while($row = mysql_fetch_array($result));
$result is probably not set or invalid by this point. Where are your returning/settings/creating the query being used here?
dardsemail
Forum Contributor
Posts: 136
Joined: Thu Jun 03, 2004 9:02 pm

Post by dardsemail »

I'm not sure I'm following...

I guess I haven't set them anywhere - how do I do that?
Post Reply