Forum Moderators: coopster

Message Too Old, No Replies

Building a php/mysql shopping cart

Following a tutorial but could use some assistance)

         

Birdman

1:16 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello everybody at WW.

I'm trying to learn how to build my own cart. I've scrubbed the web all day and the best tutorial I have found(IMO) is here [devarticles.com].

It's going okay so far(I guess), except I'm stuck on the cart.php file.

I'm getting an error on this line:

$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

Could anyone enlighten me on this error? I'm pretty sure it has to do with this portion: '" . GetCartId() . "'

Also, if anyone knows of another good resource I would appreciate a link.

Thanks alot! :)
Birdman

lorax

1:48 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month


Hey Birdman,
Well, it's hard to tell without knowing what the function GetCartId() looks like. You may want to change the itemID to '".$itemID."'

As for other tutorials: http://www.hotscripts.com/PHP/Tips_and_Tutorials/E-Commerce/

Birdman

1:53 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is that function:
function GetCartId()
{
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}

?>

Why can't anything be easy? ;)

lorax

2:02 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So I assume that the var $_cookie["cartID"] contains the last (or current) sessionID. If so then I don't see anything wrong with the function.

That being said, calling a function within a SELECT statement might be a no no. I've never tried it so I'm not sure. Try setting a var first and using that in the SELECT statement.

And no - nothing's ever easy - especially if you're in a hurry. Isn't there a Murphy's Law about this? :)

jatar_k

2:27 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I usually build the query in a var and then use the var in mysql_query.

$cartid = GetCartId();
$rq = "select count(*) from cart where cookieId='" . $cartid . "' and itemId=" . $itemId;
$result = mysql_query($rq) or die (mysql_errno().": ".mysql_error()."<BR>");

the or die bit is good for checking the error that mysql is sending as opposed to the php error.

helps keep everything seperated logically and can make things easier to work with.

andreasfriedrich

2:29 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would you mind sharing the error message?

Please post the code surrounding the offending line.

$rq = sprintf( 
'select count(*) from cart where cookieId=\'%s\' and itemId=%s',
GetCartId(), $itemId);
$result = mysql_query($rq)
or die (sprintf('%s: %s<br>', mysql_errno(), mysql_error()));

That is the way I would do it. Of course this still does not solve your error.

Andreas

lorax

2:41 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey Andreas,
I'm going to give you a complex because I'm always pestering you.;)

What good, other than just to make the error look official, is the errno()? Does it server any purpose beyond what you can get from mysql_error()?

andreasfriedrich

3:08 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That´s ok lorax. I like to answer questions ;)

If you have the error number you can then grep errmsg.h and mysqld_error.h for that number. Or you could look up the corresponding error message in docs/mysqld_error.txt. ;)

Other than those purposes I have no idea what it is good for.

It wasn´t my idea to include it into the error message. I just copied that from Adam. I guess you need to ask him.

I know I wrote in my last post that that would be the way I´d do it. I admit it: That was a plain lie, a clear deviation from the truth. ;)

I only wanted to demonstrate the sprintf() usage. I use a wrapper function for mysql_query myself which handles errors:

function my_db_query($sql) { 
global $local, $config, $db_prefix, $db_prefix_on_tables;
if (!$local) {
$_db_prefix_on_tables = $db_prefix_on_tables
. 'comments_board¦comments_category¦comments_post¦'
. 'comments_session¦comments_thread¦comments_user¦'
. 'words¦relevancy¦pages¦release';
$sql = preg_replace("/(from¦update¦replace¦into)([\r\n\s]+)($_db_prefix_on_tables)([\r\n\s]*)/smi", "$1$2$db_prefix$3$4", $sql);
}
$query = @mysql_query($sql);
if ($query) {
if (substr(trim($sql), 0, 6) == 'INSERT') {
return mysql_insert_id();
} else {
return $query;
}
} else {
if ($local) {
die('<pre>' . mysql_error() . '<br/>' . $sql . '</pre>');
} else {
mail($config['board_admin'],
$config['board_name'] . " - Fehler",
mysql_error()."\n$sql\n".time(),
"From: $config[board_admin]");
}
}
}

As you see there is no mysql_errno() there.

Adam, what´s the purpose of having mysql_errno() in your error message? Do you really look the error message up in mysql´s source code? ;)

Andreas

[edited by: jatar_k at 3:33 am (utc) on Nov. 13, 2002]
[edit reason] stopped side scroll [/edit]

Birdman

3:16 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, I really appreciate your kind assistance. I try not to post large blocks of code(from a thread I read), but here are the first 36 lines. The link in my original post has all the 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)
$crtid="'GetCartId()'";

$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemID");

$row = mysql_fetch_row($result);

I just printed a good portion of the php manual, so I'm going to see what I can soak in from that. I am a php newbie, but it's great!

andreasfriedrich

3:22 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


Just as I suspected from the lack of braces in the tutorial you mentioned.

[pre]function AddItem($itemId, $qty)
$crtid="'GetCartId()'";[/pre]

should be

[pre]function AddItem($itemId, $qty) [b]{[/b]
$crtid="'GetCartId()'";
# your code
[b]}[/b][/pre]

A function´s code needs to be enclosed in braces.

http://www.php.net/manual/en/functions.php

Andreas

jatar_k

3:35 am on Nov 13, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it is a format someone used when I started learning php and I picked it up. Yes, I have used it to look up errors for the rare one that I have never heard of. ;)

lorax

2:26 pm on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Andreas. You'll be hearing from me. ;)

Birdman, re: curly braces - by default I always include them when I'm first writing code. Once I've finished the code then I'll go back in and see if I can remove any - like on single line if statements

if (!isset($somevar))
$another var = $yetanother * $onemore;

If I don't do it this way I'll invariably add another line of code to the 'if' statement and forget to wrap it in the curly braces. Of course the server barks at me and then I go back in and add them.