MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 18th, 2003, 04:12 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Building A Persistent Shopping Cart With PHP and MySQL

I am getting an error with a shopping cart script.

The line that is causing the error is:

$query = "SELECT * FROM cart INNER JOIN products ON cart.itemId = products.id WHERE cart.cookieId = '" . GetCartId() . "' ORDER BY products.title ASC";

and the error I am getting is:

Warning: Supplied argument is not a valid MySQL result resource in /home/guitarc/public_html/cart.php on line 265

If anyone can offer any assistance that would be awesome!!!

Thanks in advance.

Reply With Quote
  #2  
Old April 18th, 2003, 06:20 AM
deedee deedee is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 11 deedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to deedee
Can you tell us the output of GetCartId() ?

deedee

Reply With Quote
  #3  
Old April 18th, 2003, 07:11 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
GetCartId()

This is the function that GetCartId() performs:

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();
}

Let me know if you need more info!!
Thanks.

Reply With Quote
  #4  
Old April 18th, 2003, 07:16 AM
deedee deedee is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 11 deedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to deedee
Well maybe we can clarify if you make an echo $query in order to see the output.

The function looks fine (will work on PHP ver 4.2.x or higher)

deedee

Reply With Quote
  #5  
Old April 18th, 2003, 07:28 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
query result

This is the result I got when I made an echo $query:

SELECT * FROM cart INNER JOIN products ON cart.itemId = products.id WHERE cart.cookieId = 'c8df510bf89a65218316e8ec33187d48' ORDER BY products.title ASC

Thanks!!

Reply With Quote
  #6  
Old April 18th, 2003, 07:44 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
New Info

I took a look at my database and it looks like this:

SELECT * FROM cart INNER JOIN products ON cart.itemId = products.id WHERE cart.cookieId = '" . GetCartId() . "' ORDER BY products.title ASC

is being done. So maybe my problem is with the code that is below that. This is what I have below that:

$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{

Could the problem be here?

Any help you can offer is great!!!

Thanks in advance.

Reply With Quote
  #7  
Old April 18th, 2003, 07:47 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
More additional info...

The error that I am getting on the browser is:

Warning: Supplied argument is not a valid MySQL result resource in /home/guitarc/public_html/cart.php on line 266


And on line 266 I have this:

while($row = mysql_fetch_array($result))

Do you think this is the actual problem and not my original query?

Thank again.

Reply With Quote
  #8  
Old April 18th, 2003, 08:08 AM
deedee deedee is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 11 deedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to deedee
This should work:
/* REPLACE YOUR SERVER SETTINGS HERE */
mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("mydb");

$query="SELECT * FROM cart INNER JOIN products ON cart.itemId = products.id WHERE cart.cookieId = \"" . GetCartId() . "\" ORDER BY products.title ASC";

$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
// RESULTS HERE....

}

Also, you may check the database fields. I mean you must check if the JOIN in correct.

Reply With Quote
  #9  
Old April 18th, 2003, 08:16 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Still having problems

I went ahead and made the changes you suggested, and I am still having the same problem. I checked my database and the INNER JOIN is happening correctly. So I know that the query is occuring and it is occurring correctly. I think the problem is with the way I am trying to obtain the results from the query.

There may be something wrong with the while loop.

What do you think?

Thanks again.

Reply With Quote
  #10  
Old April 18th, 2003, 08:32 AM
deedee deedee is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 11 deedee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to deedee
Can you paste the entire script? (without real login info of course)

Reply With Quote
  #11  
Old April 18th, 2003, 08:36 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here is the first half, too big to post as one

PHP Code:
<?php 

include("db.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

// Will check whether or not this item 
// already exists in the cart table. 
// If it does, the UpdateItem function 
// will be called instead 

global $dbServer$dbUser$dbPass$dbName

// Get a connection to the database 
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 

// Check if this item already exists in the users cart table
$query "select count(*) from cart where cookieId = '" GetCartId() . "' and itemId = $itemId";
$result mysql_query($query) or die("Couldn't execute query");
$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, 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

// Updates the quantity of an item in the users cart. 
// If the qutnaity is zero, then RemoveItem will be 
// called instead 

global $dbServer$dbUser$dbPass$dbName

// Get a connection to the database 
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 

if(
$qty == 0

// Remove the item from the users cart 
RemoveItem($itemId); 

else 

mysql_query("update cart set qty = $qty where cookieId = '" GetCartId() . "' and itemId = $itemId"); 



function 
RemoveItem($itemId

// Uses an SQL delete statement to remove an item from 
// the users cart 

global $dbServer$dbUser$dbPass$dbName

// Get a connection to the database 
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 

mysql_query("delete from cart where cookieId = '" GetCartId() . "' and itemId = $itemId"); 


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 

global $dbServer$dbUser$dbPass$dbName

// Get a connection to the database 
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 

$totalCost 0
$query "SELECT * FROM cart INNER JOIN products ON cart.itemId = products.id WHERE cart.cookieId = '" GetCartId() . "' ORDER BY products.title ASC";
$result mysql_query($query);
print(
$query);
?>

Reply With Quote
  #12  
Old April 18th, 2003, 08:37 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here is the second half, there is still one more to go.

Code:
<html>
<head>
<title>Guitar City</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript"> 

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

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

</script>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}
//-->
</script>
<link href="styles.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
a {
	color: #4F4F4F;
}
-->
</style>
<style type="text/css">
<!--
-->
</style>
</head>

<body bgcolor="#000000" leftmargin="0" topmargin="0" rightmargin="0" marginwidth="0" marginheight="0" onLoad="MM_preloadImages('images/home/products_on.gif','images/home/order_on.gif','images/home/contact_on.gif','images/home/links_on.gif','images/home/celebrity_on.gif','images/home/home_on.gif')">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td background="images/home/toptile.gif"><img src="images/home/toptile_left.gif" width="7" height="30"></td>
  </tr>
  <tr> 
    <td background="images/home/logo_tile.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td><img src="images/home/logo.gif" width="215" height="68"></td>
          <td align="right" valign="top"> <form name="form1" method="post" action="">
              <img src="images/home/choose_product.gif" width="125" height="22"> 
              <select name="menu" onChange="MM_jumpMenu('parent',this,0)">
                <option>Choose One</option>
                <option value="products.php?cat=electric">Electric Guitars</option>
                <option value="products.php?cat=acoustic">Acoustic Guitars</option>
                <option value="products.php?cat=bass">Bass Guitars</option>
                <option value="products.php?cat=classical">Classical Guitars</option>
                <option value="products.php?cat=vintage">Vintage Guitars</option>
                <option value="products.php?cat=other_stringed">Other Stringed 
                Instruments</option>
                <option value="products.php?cat=amplifiers">Amplifiers</option>
                <option value="products.php?cat=cases">Cases</option>
                <option value="products.php?cat=accessories">Strings and Accessories</option>
                <option value="products.php?cat=lefthanded">Left-Handed Guitars</option>
              </select>
              <input type="button" name="Button1" value="Go" onClick="MM_jumpMenuGo('menu2','parent',0)">
              &nbsp;&nbsp; </form></td>
        </tr>
      </table></td>
  </tr>
  <tr> 
    <td background="images/home/button_tile.gif"><table border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td width="78"><a href="categories.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image4','','images/home/products_on.gif',1)"><img src="images/home/products_off.gif" name="Image4" width="78" height="20" border="0"></a></td>
          <td width="88"><a href="https://earth.he.net/~guitarc/confirm_order.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image5','','images/home/order_on.gif',1)"><img src="images/home/order_off.gif" name="Image5" width="88" height="20" border="0"></a></td>
          <td width="88"><a href="contact.htm" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','images/home/contact_on.gif',1)"><img src="images/home/contact_off.gif" name="Image6" width="88" height="20" border="0"></a></td>
          <td width="58"><a href="links.htm" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image7','','images/home/links_on.gif',1)"><img src="images/home/links_off.gif" name="Image7" width="58" height="20" border="0"></a></td>
          <td width="115"><a href="celebrity.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image8','','images/home/celebrity_on.gif',1)"><img src="images/home/celebrity_off.gif" name="Image8" width="115" height="20" border="0"></a></td>
          <td width="59"><a href="index.htm" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image16','','images/home/home_on.gif',1)"><img src="images/home/home_off.gif" name="Image16" width="59" height="20" border="0"></a></td>
        </tr>
      </table></td>
  </tr>
  <tr> 
    <td align="right" background="images/nav/guitar_bar_tile.gif"><img src="images/nav/guitar_bar.gif" width="780" height="50"></td>
  </tr>
  <tr> 
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td height="53" background="images/nav/title_tile.gif"><img src="images/nav/lt_toptable.gif" width="497" height="53"></td>
          <td align="right" background="images/nav/title_tile.gif"><img src="images/nav/checkout_ttl.gif" width="283" height="53"></td>
        </tr>
      </table></td>
  </tr>
  <tr> 
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td width="45" valign="top" background="images/nav/lt_midtabletile.gif"><img src="images/nav/lt_midtabletop.gif" width="45" height="242"></td>
          <td rowspan="2" valign="top" bgcolor="#FFFFFF"><table width="100%" height="527" border="0" cellpadding="20" cellspacing="0">
              <tr> 
                <td valign="top" class="bgTile"><img src="images/nav/checkout_ttl_small.gif" width="70" height="10"> 
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr> 
                      <td bgcolor="#878787"><img src="images/home/spacer.gif" width="1" height="1"></td>
                    </tr>
                  </table>
                  <p><strong><span class="standardFont">The following items are 
                    in your shopping cart</span></strong>:<br>
                  </p>
                  <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" bgcolor="#000000">
                    <TR> 
                      <TD WIDTH="100%"> <table width="100%" border="0" cellspacing="1" cellpadding="0">
                          <tr> 
                            <td bgcolor="#FFFFFF"><TABLE BORDER="0" CELLPADDING="4" CELLSPACING="0" WIDTH="100%">
                                <form name="frmCart" method="get">
                                  <TR>
                                    <TD background="images/products/table_bg.gif">&nbsp;</TD>
                                    <TD height="17" background="images/products/table_bg.gif"> 
                                      <img src="images/products/item.gif" width="26" height="9"></TD>
                                    <TD ALIGN="center" background="images/products/table_bg.gif"> 
                                      <img src="images/products/shipping.gif" width="51" height="9"></TD>
                                    <TD ALIGN="center" background="images/products/table_bg.gif"> 
                                      <img src="images/products/quantity.gif" width="57" height="9"></TD>
                                    <TD ALIGN="center" background="images/products/table_bg.gif"> 
                                      <img src="images/products/price.gif" width="33" height="9"></TD>
                                    <TD ALIGN="center" background="images/products/table_bg.gif"> 
                                      <img src="images/products/total.gif" width="37" height="9"></TD>
                                  </TR>

Reply With Quote
  #13  
Old April 18th, 2003, 08:38 AM
digital102 digital102 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 digital102 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here is the last one, sorry!!!

PHP Code:
<?php 
while($row mysql_fetch_array($result))
{
// Increment the total cost of all items 
$totalCost += ($row["qty"] * $row["cost"]); 
$subtotal=(($row["cost"] + $row["shipping"]) * ($row["qty"]));?>
                                  &