|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
Can you tell us the output of GetCartId() ?
deedee |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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!! |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
Can you paste the entire script? (without real login info of course)
|
|
#11
|
|||
|
|||
|
Here is the first half, too big to post as one
PHP Code:
|
|
#12
|
|||
|
|||
|
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)">
</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"> </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>
|
|
#13
|
|||
|
|||
|
Here is the last one, sorry!!!
PHP Code:
|