quick question about shopping cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SJanis
Forum Newbie
Posts: 5
Joined: Tue Jul 27, 2004 10:06 am

quick question about shopping cart

Post by SJanis »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


sorry...but i have a quick question...my shopping cart that i built of the macromedia persistent cart example... works perfectly fine when i'm adding items numbers to it such as 1000354, 3500035, or 3053535, but as soon as i add a letter to the end of one of those part numbers...lets say the number is....1004400F, (http://127.0.0.1/testshopping/cart.php? ... &qty=1) i get a Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource error....if anyone could help i would greatly appreciate it...thank you very much....

this is the cart.php..if this helps..

Code: Select all

<?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;
}
case "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
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
$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;
$result = mysql_query("select * from cart inner join data_det on cart.itemId = data_det.itemId where cart.cookieId = '" . GetCartId() . "' order by data_det.itemManu asc");
?>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's likely the part number is stored as a number, or the query returned zero records that matched.. you have a comma in your query string, could this be screwing it up?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

why would you want to add a letter to it?
SJanis
Forum Newbie
Posts: 5
Joined: Tue Jul 27, 2004 10:06 am

Post by SJanis »

the part number calls for a letter in it....such as maybe 104440F or HT 2013....i'm still getting the same mysql_fetch_row(): supplied argument is not a valid MySQL result resource even after changing the type of itemId in my cart table from an int...to a varchar.....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have an error in you sql syntax now that you placed a letter in there.. add quotes around $itemId in the queries.
SJanis
Forum Newbie
Posts: 5
Joined: Tue Jul 27, 2004 10:06 am

Post by SJanis »

thank you feyd...i did that too all of them and it worked perfect...i really appreciate you help...thanks again
Post Reply