|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
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:
|
|
#14
|
|||
|
|||
|
Let's try this:
<?php include("db.php"); global $dbServer, $dbUser, $dbPass, $dbName; @mysql_connect($dbServer, $dbUser, $dbPass) or die("Can't connect"); @mysql_select_db($dbName) or die("Invalid database"); ?> |
|
#15
|
|||
|
|||
|
Error found
I ran a print(mysql_error()); and I got the error below:
You have an error in your SQL syntax near 'on cart.itemId = products.id WHERE cart.cookieId = 'c8df510bf89a65218316e8ec3318' at line 1 The line of code it is referring to is: $query = "SELECT * FROM cart INNER JOIN products on cart.itemId = products.id WHERE cart.cookieId = '" . GetCartId() . "' ORDER BY products.title ASC"; I don't see an error in syntax, do you? Thanks again for your help!!! |
|
#16
|
|||
|
|||
|
What's the output of this man?
<?php include("db.php"); global $dbServer, $dbUser, $dbPass, $dbName; @mysql_connect($dbServer, $dbUser, $dbPass) or die("Can't connect"); @mysql_select_db($dbName) or die("Invalid database"); ?> |
|
#17
|
|||
|
|||
|
Output
I added what you suggested. But there was no output. I'll still only getting the same error from before.
Thanks again!! |
|
#18
|
|||
|
|||
|
Try this:
$query = "SELECT * FROM cart,products WHERE cart.itemId=products.id and cart.cookieId = \"" . GetCartId() . "\" ORDER BY products.title ASC"; |
|
#19
|
|||
|
|||
|
It finally worked
This is what I did and it finally worked:
$query = "SELECT * FROM cart,products WHERE (cart.itemId=products.id) and cart.cookieId = \"" . GetCartId() . "\" ORDER BY products.title ASC"; Thanks a lot for your help!!!! It is greatly appreciated!!! |
|
#20
|
|||
|
|||
|
Ok, no problem
![]() Have a nice weekend! Greets, deedee |
|
#21
|
|||
|
|||
|
Warning: session_start()
Hi Guys!
I have a small problem - I get the following Warnings: Warning: session_start(): open(/tmp\sess_9fb4f116aff355956dbd2e23ef7cbf09, O_RDWR) failed: No such file or directory (2) in C:\Programme\Apache Group\Apache\htdocs\herbalife\script\db.php on line 30 Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Programme\Apache Group\Apache\htdocs\herbalife\html\TMP4b3yeti4xr.p hp:6) in C:\Programme\Apache Group\Apache\htdocs\herbalife\script\db.php on line 30 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\Programme\Apache Group\Apache\htdocs\herbalife\html\TMP4b3yeti4xr.p hp:6) in C:\Programme\Apache Group\Apache\htdocs\herbalife\script\db.php on line 30 Warning: Cannot modify header information - headers already sent by (output started at C:\Programme\Apache Group\Apache\htdocs\herbalife\html\TMP4b3yeti4xr.p hp:6) in C:\Programme\Apache Group\Apache\htdocs\herbalife\script\db.php on line 31 The Code looks fine to me: <?php $dbServer = "localhost"; $dbUser = "apache"; $dbPass = "xxxxxxxxxx"; $dbName = "php_db"; function ConnectToDb($server, $user, $pass, $database) { $s = @mysql_connect($server, $user, $pass); $d = @mysql_select_db($database, $s); if(!$s || !$d) return false; else return true; } function GetCartId() { if(isset($_COOKIE["cartId"])) { return $_COOKIE["cartId"]; } else { session_start(); setcookie("cartId", session_id(), time() + ((3600 * 24) * 30)); return session_id(); } } ?> What am I doing wrong? Thanks! |
|
#22
|
|||
|
|||
|
Need help :(
Can someone help me with cart.php script? I am confused since the script is broken into pieces here
I'd appreciate any kind of help.Thanks |
|
#23
|
|||
|
|||
|
Solved
Sorry....duplicated my earlier post.
|
|
#24
|
||||
|
||||
|
Suggest you place a new thread for your problem.
__________________
Work to live, don't live to work |
|
#25
|
|||
|
|||
|
Thanks for your suggestion, I'm gonna start a new thread
![]() Regards, AJ |
|
#26
|
|||
|
|||
|
If anyone else is looking for the files that go with this article you can find them on the Macromedia site - URL
I've no idea why they are not on the devarticles site - the way the tutorial is written requires the files (IMHO). Anyway. good luck. Topper |
|
#27
|
|||
|
|||
|
I'm having this problem:
Notice: Undefined variable: sql_result in C:\Programas\Apache Group\Apache2\htdocs\page\cart.php on line 118 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Programas\Apache Group\Apache2\htdocs\page\cart.php on line 118 function UpdateQty(item) { itemId = item.name; newQty = item.options[item.selectedIndex].text; document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty; } << Keep Shopping Total: € Notice: Undefined variable: totalCost in C:\Programas\Apache Group\Apache2\htdocs\page\cart.php on line 197 0.00 I think this two erros are the same. I made some changes in products.php, like substituting $results by $sql_results and worked fine. So I made the same to cart.php. I've alredy tried to rename it back and i've got the same error. Can anyone send me an example of this working or tell me what's wrong with this? Sorry any bad english but I'm portuguese. Thanks! |
|
#28
|
|||
|
|||
|
mysql_fetch_array() error
hi, can anybody tell me why i get the following error when using the tutorial scripts verbatim?
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /webroot/cart/products.php on line 42 i'm using php 4.3.3 and mysql 4.0.15a thanks. |
|
#29
|
|||
|
|||
|
erm, sorry i'll put this in a new thread actually...
|
|
#30
|
|||
|
|||
|
I am getting the following error: "You have an error in your SQL syntax near '' at line 1" " When
I try to add an item into the shopping cart! Does any one know why? Thanks, |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > Building A Persistent Shopping Cart With PHP and MySQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|