General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

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 November 5th, 2003, 04:50 PM
duckman duckman is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 4 duckman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Another ShoppingCartTutorial question. heeeeelp!!

I'm trying to get the shoppingcart from the tutorial on this site to work.
I've modified it a bit, and now I'm stuck. Been running around in circles to try and find out the error. I've searched and read all the posts about this particular tutorial on the forum, but only found one other mention of the same error, and no solution.

Here's the walkthrough:

The page products.php includes a db.inc with databaseinfo, and a function:

(the database was already in place, with norwegian titles, therefore I've changed them in the scripts (ie: cartId = handlekurvID)

[From dp.inc (also called dp.php in the tutorial. got an error when i called it that) ]
PHP Code:
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["handlekurvID"])) 

return 
$_COOKIE["handlekurvID"]; 

else 

// There is no cookie set. We will set the cookie 
// and return the value of the users session ID 

session_start(); 
setcookie("handlekurvID"session_id(), time() + ((3600 24) * 30)); 
return 
session_id(); 




heres the products.php:
PHP Code:
<? 

include ("db.inc"); 

$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 
$result mysql_query("select * from produkt order by produktnavn asc"); 

?> 

<html>
<body>

<table border="0"> 
<?php 
while($row mysql_fetch_array($result)) 

?> 
<tr> 
<td width="10%" height="25"> 

<font face="Arial" size="2" color="black"><?php echo $row["produktnavn"]; ?> 
</font> 
</td> 

<td width="10%" height="25"> 
<font face="arial" size="2" color="black"> 
<? echo $row["pris"]; ?> 
</font> 
</td> 

<td width="20%" height="25"> 
<font face="arial" size="2" color="black"><?php echo $row["beskrivelse"]; ?> 
</font> 
</td> 

<td width="10%" height="25"> 

<font face="verdana" size="2" color="black"><a href ="cart.php?action=add_item&id=<?php echo $row["produktID"];?>&antall=1">Add Item 
</a></font> 
</td> 
<td></td> 
</tr> 


<tr> 
<td width="100%" colspan="4"> 
<hr size="1" color="red" NOSHADE> 
</td> 
</tr> 
<? ?> 
<tr> 
<td width="100%" colspan="4"> 
<font face="arial" size="1" color="black"><a href="cart.php">Your shopping cart >> 
</a></font> 
</td> 
</tr> 

</table> 
</BODY>



Here's the cart.php:
PHP Code:
<?php 

include("db.inc"); 
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 

Switch(
$_GET["action"]) 

case 
"add_item"

AddItem($_GET["produktID"], $_GET["antall"]); 
//ShowCart(); 
include("showcart.php"); 
break; 


case 
"update_item"

UpdateItem($_GET["produktID"], $_GET["antall"]); 
//ShowCart(); 
include("showcart.php"); 
break; 


case 
"remove_item"

RemoveItem($_GET["produktID"]); 
//ShowCart(); 
include("showcart.php"); 
break; 


default: 

//ShowCart(); 
include("showcart.php"); 




function 
AddItem($produktID$antall
{
global 
$dbserver$dbuser$dbpass$dbname$cxn;
$cxn = @ConnectToDb($dbserver$dbuser$dbpass$dbname); 
 

//!!!!!!!!!!!!!!!!!!!!!!!! Here's my error, it seems!!!!!!!!!!!!!!!


$result mysql_query("Select count(*) from handlekurv where cookieID = '".GetCartId()."' and produktID = $produktID")or die("Query failed: $query<br><br>" mysql_error()); 

$row mysql_fetch_row($result); 
$numRows $row[0]; 

if(
$numRows == 0

mysql_query("insert into handlekurv(cookieID, produktID, antall) values('".GetCartId()."', $produktID, $antall)") or die("query failed".mysql_error()); 


else 



UpdateItem($produktID$antall); 





function 
UpdateItem($produktID$antall


global 
$dbserver$dbuser$dbpass$dbname$cxn;
$cxn = @ConnectToDb($dbserver$dbuser$dbpass$dbname); 
mysql_query("update handlekurv set antall = $antall where cookieID = '".GetCartId()."' and produktID = $produktID"); 




function 
RemoveItem($produktID
{
global 
$dbserver$dbuser$dbpass$dbname$cxn;
$cxn = @ConnectToDb($dbserver$dbuser$dbpass$dbname);  

mysql_query("delete from handlekurv where cookieID = '" .GetCartId()."' and produktID = $produktID"); 


?>


Finally - showcart.php
PHP Code:
<? 
function ShowCart() 

?> 
<script language="JavaScript"> 
function UpdateQty(produkt) 

produktID = item.name; 
newQty = item.options[item.selectedIndex].text; 

document.location.href = 'cart.php?update_item&id='+produktID+'&antall='+newQty; 


</script> 

<? 


$result 
mysql_query("select* from handlekurv inner join produkt on handlekurv.produktID = produkt.produktID where handlekurvID.cookieID = '".GetCartID()."' order by produkt.produktnavn asc"); 

while(
$row mysql_fetch_array($result)) 

$totalCost += ($row["antall"] * $row["pris"]); 
?> 

<tr> 
<td width = "15%" height="25"> 
<font face="arial" size="2" color="black"> 
<select name="<? echo $row["produktID"]; ?>" onChange="UpdateQty(this)"> 

<? 
for ($i 1$i <=20$i++) 

echo 
"<option"
if(
$row["antall"] == $i

echo 
"Selected"


echo 
">".$i."</option>"


?> 
</select> 
</font> 
</td> 

<td width="55%" height="25"> 
<font face="arial" size="2" color="black"> 
<? echo $row["produktnavn"]; ?> 
</font> 
</td> 

<td width="20%" height="25"> 
<font face="arial" size="2" color="black"> 
RM<? echo number_format($row["pris"], 2,".""."); ?> 
</font> 
</td> 

<td width="10%" height="25"> 
<font face="arial" size="2" color="black"> 
<a href="cart.php?action=remove_item&id=<? echo $row["produktID"]; ?>">Remove</a> 
</font> 
</td> 

<? 
// close for while loop 

//$totalCost += ($row["antall"] * $$row["pris"]); 
//$totalCost = $totalCost + ($row["antall"] * $row["pris"]); // same by using += 
?> 


<tr> 
<td width="100%" colspan="4"> 
<hr size="1" color="red" NOSHADE> 
</td> 
</tr> 

<tr> 
<td width="70%" colspan="2"> 
<font face="arial" size="2" color="black"> 
<a href="products.php"><< Keep Shopping</a> 
</font> 
</td> 

<td width="30%" colspan="2"> 
<font face="arial" size="3" color="black"> 
<b>Total: <? echo number_format($totalCost2".","."); ?> </b> 
</font> 
</td> 
</tr> 
<? //close function?>



------------------------

THere you have the whole thing. Hope someone's got their kind suit on today, and is willing to help a php-newbie.
This has gotten me on the verge of mental meltdown the past two days.

Here's what happens.

The products.php shows my products without problem
When I press "add item", this show up:

Query failed:

You have an error in your SQL syntax near '' at line 1


Thanks for reading this, deeply grateful for any help...

Reply With Quote
  #2  
Old November 5th, 2003, 05:59 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Duckman,

What you should try is first storing your SQL query in a variable, and then echo out the variable value to see exactly what MySQL is receiving... It may be something in your query string.

I'm not completely sure, but I have a feeling it may have something to do with this part of your query:

PHP Code:
 $result mysql_query("Select count(*) from handlekurv where cookieID = [b]'[/b]".GetCartId()."[b]'[/b] and produktID = $produktID")or die("Query failed: $query<br><br>" mysql_error()); 


You're using single quotes... Is the value of "GetCardId()" a string value or an integer? If it's not a string, that would cause an error.

PS: When you post code, please try to use the [ php ] and [/ php ] BB codes (no spaces between the contents and the square braces). It makes for much easier reading of your code!
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old November 6th, 2003, 04:16 AM
duckman duckman is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 4 duckman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by FrankieShakes
Duckman,


You're using single quotes... Is the value of "GetCardId()" a string value or an integer? If it's not a string, that would cause an error.


Hi FrankieShakes, thanks for giving this a shot

the sql-snippet you mention is where I also belive the error lies.

PHP Code:
 $result mysql_query("Select count(*) from handlekurv where cookieID = ' ".GetCartId()." ' and produktID = $produktID")or die("Query failed: $query<br><br>" mysql_error()); 



That calls the function "GetCartId" from db.inc.
That function - GetCartId is this one:
It returns a string.

PHP Code:
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["handlekurvID"])) 

return 
$_COOKIE["handlekurvID"]; 

else 

// There is no cookie set. We will set the cookie 
// and return the value of the users session ID 

session_start(); 
setcookie("handlekurvID"session_id(), time() + ((3600 24) * 30)); 
return 
session_id(); 




Sorry for leaving the [ php] [/ php] out, but I see someone already has edited my post while I slept.
I'll behave from now on

Reply With Quote
  #4  
Old November 6th, 2003, 04:50 AM
duckman duckman is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 4 duckman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
FrankieShakes, tried what you said - about putting the query in a string to see what happened.


Put it on the line before the one I suspect makes the error:

PHP Code:
function AddItem($produktID$antall
{
global 
$dbserver$dbuser$dbpass$dbname$cxn;
$cxn = @ConnectToDb($dbserver$dbuser$dbpass$dbname); 

$sqlUtrykket "Select count(*) from handlekurv where cookieID = '".GetCartId()."' and produktID = $produktID";

echo 
$sqlUtrykket;
 
$result mysql_query("Select count(*) from handlekurv where cookieID = '".GetCartId()."' and produktID = $produktID")or die("Query failed: $query<br><br>" mysql_error()); 

$row mysql_fetch_row($result); 
$numRows $row[0]; 

if(
$numRows == 0

mysql_query("insert into handlekurv(cookieID, produktID, antall) values('".GetCartId()."', $produktID, $antall)") or die("query failed".mysql_error()); 


else 



UpdateItem($produktID$antall); 







Here's the result :

Select count(*) from handlekurv where cookieID = '382c5c2d7275d80a89d5a07c1de6a4d7' and produktID = Query failed:

You have an error in your SQL syntax near '' at line 1

Reply With Quote
  #5  
Old November 6th, 2003, 05:52 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Okay!

I think I got it...

PHP Code:
 $result mysql_query("Select count(*) from handlekurv where cookieID = ' ".GetCartId()." ' and produktID = $produktID")or die("Query failed: $query<br><br>" mysql_error()); 


Should be:

PHP Code:
 $result mysql_query("Select count(*) from handlekurv where cookieID = ' ".GetCartId()." ' and produktID = $_GET['produktID']")or die("Query failed: $query<br><br>" mysql_error()); 


You weren't using the superglobal array ($_GET) to retrieve the value of produktID. As you can see by the echoed query, "... and produktID = ". There was no value for produktID.

HTH!

Reply With Quote
  #6  
Old November 7th, 2003, 09:43 AM
duckman duckman is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 4 duckman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by FrankieShakes
Okay!

I think I got it...

You weren't using the superglobal array ($_GET) to retrieve the value of produktID. As you can see by the echoed query, "... and produktID = ". There was no value for produktID.

HTH!



And it WORKS
Thanks a lot, FrankieShakes!
I can sleep at night now.

The only thing to find out, is why my cart.php/showcart.php does NOT show any products
But that's been asked a million times here, I noticed when I searched for answers to this question.
So I'll try those solutions before I pester anyone again!

Reply With Quote
  #7  
Old November 14th, 2003, 05:54 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
No problem, Duckman!

Any updated on the no showing of products? Any luck on finding a fix in the forums?

Reply With Quote
  #8  
Old November 24th, 2003, 09:04 AM
BrokenDreams BrokenDreams is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 5 BrokenDreams User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How about a checkout script?

How about a checkout script? For the same tutorial?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Another ShoppingCartTutorial question. heeeeelp!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump