SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Coding problem,plz help

    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:

    PHP 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($totalCost2); ?></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

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print out the query with print($query) to find out what is wrong with it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •