adding function

A

Anonymous

Guest
i would like to ask if i wanna add a showcart function after removeitem function, will it works (the code is included below??

i am not able to check the function due to a mysql error (error:You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(*) from cart where cookieId = 'b4559701f1b56b6b451e1e272d82585) may i know what actually this mysql error means ??? did i inserted the code wrongly ??

p.s: is my showcart function written correctly ??

please advise...

my current code:
Code:
<?

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)
{
$result = mysql_query("Select count (*) from cart where cookieId = '".GetCartId(). "' and itemId = $itemId")or die("Query failed: $query<br><br>" . mysql_error());

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

if($numRows == 0)
{
@mysql_query("insert into cart(cookieId, itemId, qty) values('".GetCartId()."', $itemId, $qty)");
}

else

{

UpdateItem($itemId, $qty);

}
}


function UpdateItem($itemId, $qty)
{

mysql_query("update cart set qty = $qty where cookieId = '".GetCartId()."' and itemId = $itemId");

}


function RemoveItem($itemId)
{

mysql_query("delete from cart where cookieId = '" .GetCartId()."' and itemId = $itemId");
}


// paste the showcart function here,,
?>





my showcart code:
Code:
function ShowCart()
<? { ?> 
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;

document.location.href = 'cart.php?update_item&id='+itemId+'&qty='+newQty;
}

</script>
<?
$result = mysql_query("select * from cart inner join items on cart,itemId = items.itemId where cart.cookieId = '".GetCartId()."' order by items.itemName asc");

while($row = mysql_fetch_array($result))
{
$totalCost += ($row["$qty"] * $row["itemPrice"]);
?>

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

for ($i = 1; $i <=20; $i++)
{
echo "<option";
if($row["qty"] == $i)
{
echo "Selected";
}

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

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

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

<td width="20%" height="25">
<font face="arial" size="2" color="black">
RM<? echo number_format($row["itemPrice"], 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["itemId"]; ?>">Remove</a>
</font>
</td>

<?
}

$totalCost += ($row["qty"] * $$row["itemPrice"]);
//$totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); // 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: RM<? echo number_format($totalCost, 2, ".","."); ?> </b>
</font>
</td>
</tr>
<? } ?>
 
why don't you make a php script [file] that contains all the functions and include that file...
2nd better run your sql on the phpmyadmin or mysql konsole :!:
 
cookieId = 'b4559701f1b56b6b451e1e272d82585)

you are missing a ' at the end of the string.
 
how can i add another ' at the end of the cookie ... i dun even see this value stored in my table
 
i have removed spacing and the error is fixed... but now, it says no database selected... what is the prob ??? i do use the same include file in the previous file and it works...
 
Alicia said:
i have removed spacing and the error is fixed... but now, it says no database selected... what is the prob ??? i do use the same include file in the previous file and it works...

check your connection to host and selection for database
 
Back
Top