Jump to content

Coding bugs,plz help


cty

Recommended Posts

Error shown:

Query (line 50) failed: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 '' at line 1

-----------------------------------------
My first problem:

How am i going to eidt this query to a correct form?


$query="delete from cart where bookid =$bookid";


----------------------------------------
My second problem(main problem):


Why the book price and the total price always shown 0?(*When i click on "buy).

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

Below are my code:

//product.php


<?php


include("db.php");

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed(line 7):'.mysqli_connect_error());
}


$db->select_db('test')
or die ('Select_db failed(line 14):'.$db->error);


$query="select * from book order by title asc";
$result = $db->query($query)
or die('Query (line 18) failed:'.$db->error);

?>

<?php
while($row =$result->fetch_assoc())
{
?>

<table border=1 width=80% bgcolor="pink">
<tr>
<td width=20%>
<font face="verdana" size="2" color="black" >
ISBN :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["isbn"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
TITLE:
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["title"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Author :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["author"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Description :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["description"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Condition :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["condition"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Price(RM) :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["price"]; ?>
</font>
</td>
</tr>





<td >
<font face="verdana" size="2" color="black">
<a href=cart.php?action=add_item&bookid=<?php echo $row["bookid"];?>>Buy</a>
</font>
</td>
</tr>
<br />
</table>

<?php }?>

<tr>
<td >
<hr size="2" color="red" NOSHADE>
</td>
</tr>



<tr>
<td >
<font face="verdana" size="5" color="black">
<a href="cart.php"> View Your Shopping Cart &gt;&gt;</a>
</font>
</td>
</tr>
</table>
</body>
</html>


-----------------------------------------------------
//kelly.php

<?php


function AddItem($bookid){

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}
$db->select_db('test')
or die ('Select_db failed:'.$db->error);

$query="select count(*) from cart where bookid = '$bookid'";

$result=$db->query($query)
or die('Query line 17 failed:'.$db->error);

$row =$result->fetch_row();
$numRows = $row[0];
if($numRows == 0)
{


$query="insert into cart(cookieId, bookid) values('" . GetCartId() . "', '$bookid')";
$result=$db->query($query)
or die('Query line 29 failed:'.$db->error);
}

else
{
echo "The book already in your shopping cart.";
echo "<br />";

}

}


function RemoveItem($bookid){

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die ('Select_db failed(line 53):'.$db->error);

$query="delete from cart where bookid =$bookid";

$result=$db->query($query)
or die('Query (line 50) failed:'.$db->error);
}





function ShowCart( ){

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die ('Select_db failed:'.$db->error);


$query="select * from cart inner join book where cart.bookid = book.bookid and cart.cookieId = '" . GetCartId() . "'order by book.title asc";
$result=$db->query($query)
or die('Query line 78 failed:'.$db->error);

$totalCost=0;
while($result->fetch_assoc())
{

$totalCost +=$row["price"];


?>

<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["title"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["price"], 2); ?>

</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href=cart.php?action=remove_item&bookid=<?php echo $row["bookid"];?>>Remove</a>
</font>
</td>
</tr>
<br />

<?php } ?>

<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php">&lt;&lt; Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2); ?></b>
</font>
</td>
</tr>
<?php } ?>

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

//db.php

<?php

function GetCartId()
{

if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{


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

?>
------------------------------------------------------
//cart.php

<?php
include("kelly.php");
include("db.php");
?>


<?php

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["bookid"]);
ShowCart();
break;
}


case "remove_item":
{
RemoveItem($_GET["bookid"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}

?>


---------------------------------------------//end


Link to comment
https://forums.phpfreaks.com/topic/31317-coding-bugsplz-help/
Share on other sites

1. $query="DELETE FROM cart WHERE bookid='$bookid'";  Capitalize your query commands. Easier to read.
2. In between <?php ?> tags you shouldn't be using double quotes " for everything. For example, the $_GET statements typically are $_GET['bookid'];  You can use double quotes inside of single quotes.

http://dev.mysql.com/doc/refman/4.1/en/string-syntax.html

3. You should paste your code into the posts using the # code view so we can view it in the proper color scheme for PHP/HTML :)
Link to comment
https://forums.phpfreaks.com/topic/31317-coding-bugsplz-help/#findComment-144924
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.