PHP saying error in mysql syntax, but written my mysql query browser!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Flic

    PHP saying error in mysql syntax, but written my mysql query browser!

    Hi,

    I have a basic db that I access with MySQL query browser. Everything
    seems fine to me but I am using this db as part of a php shopping
    basket and when I try to add an item I get:

    Notice: Query 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 '>function.extr act]: First argument should be an
    array in functions.inc.p hp on line 31
    Notice: Undefined variable: price in functions.inc.p hp on line 36
    Notice: Undefined variable: price in functions.inc.p hp on line 39
    Notice: Undefined variable: total in unctions.inc.ph p on line 39

    I'm assuming the last three are caused by this problem as price should
    be passed to the cart, and total is worked out using it. However
    although I know mySQL code it was the MySQL query browser that
    actually generated the code and I cannot see a way to view or debug
    the code.

    The db has one table in it which is made up of id, name, subname,
    desc, and price.

    The code in the php file that is being referred to is:

    <?php
    function writeShoppingCa rt() {
    $cart = $_SESSION['cart'];
    if (!$cart) {
    return '<p>You have no items in your shopping cart</p>';
    } else {
    // Parse the cart session variable
    $items = explode(',',$ca rt);
    $s = (count($items) 1) ? 's':'';
    return '<p>You have <a href="cart.php" >'.count($items ).' item'.$s.'
    in your shopping cart</a></p>';
    }
    }

    function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    $output[] = "<form action='cart.ph p?action=update ' method='post'
    id='cart'>";
    $output[] = "<table><tr><td >ID</td>";
    $output[] = "<td>Name</td>";
    $output[] = "<td>Price</td>";
    $output[] = "<td>Quan</td>";
    $output[] = "<td>Subtot al</td></tr>";
    if ($cart) {
    $items = explode(',',$ca rt);
    $contents = array();
    foreach ($items as $item) {$contents[$item] =
    (isset($content s[$item])) ? $contents[$item] + 1 : 1;}
    foreach ($contents as $id=>$qty) {
    $sql = 'SELECT * FROM acc WHERE id = '.$id;
    $result = $db->query($sql);
    $row = $result->fetch();
    extract($row);
    $output[] = "<tr><td>".$ite m['id']."</td>";
    $output[] = "<td>".$ite m['info']."</td>";
    $output[] = "<td>".number_f ormat($item['price'],2)."</td>";
    $output[] = "<td>".$ite m['qty']."</td>";
    $output[] = "<td>&pound;".( $price * $qty)."</td>";
    $output[] = "<td><form method=post><in put
    type='hidden' name='id' value='".$item['id']."'/>";
    $output[] = "<input type='submit' name='remove' value='X'/></form></
    td></tr>";
    $total += $price * $qty;
    }
    $output[] = "<tr><td colspan=4>Sub total:</td><td>£".$tota l."</td></
    tr>";
    $output[] = "<tr><td colspan=4>VAT:</td><td>£".($tot al * 0.175)."</
    td></tr>";
    $output[] = "<tr><td colspan=4>Grand total:</td><td>£".($tot al *
    1.175)."</td></tr>";
    $output[] = "<div><butt on type='submit'>U pdate cart</button></div>";
    $output[] = "</table></form>";
    } else {
    $output[] = "<tr><td colspan=5>- No items found in cart -</td></tr></
    table>";
    }
    return join('',$output );
    }
    ?>

    So I can't see any problems that would be causing this other than the
    db problem but can't find out what that is exactly!

    Any help appreciated
    Thanks, Flic

  • Jerry Stuckle

    #2
    Re: PHP saying error in mysql syntax, but written my mysql querybrowser!

    Flic wrote:
    Hi,
    >
    I have a basic db that I access with MySQL query browser. Everything
    seems fine to me but I am using this db as part of a php shopping
    basket and when I try to add an item I get:
    >
    Notice: Query 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 '>function.extr act]: First argument should be an
    array in functions.inc.p hp on line 31
    Notice: Undefined variable: price in functions.inc.p hp on line 36
    Notice: Undefined variable: price in functions.inc.p hp on line 39
    Notice: Undefined variable: total in unctions.inc.ph p on line 39
    >
    I'm assuming the last three are caused by this problem as price should
    be passed to the cart, and total is worked out using it. However
    although I know mySQL code it was the MySQL query browser that
    actually generated the code and I cannot see a way to view or debug
    the code.
    >
    The db has one table in it which is made up of id, name, subname,
    desc, and price.
    >
    The code in the php file that is being referred to is:
    >
    <?php
    function writeShoppingCa rt() {
    $cart = $_SESSION['cart'];
    if (!$cart) {
    return '<p>You have no items in your shopping cart</p>';
    } else {
    // Parse the cart session variable
    $items = explode(',',$ca rt);
    $s = (count($items) 1) ? 's':'';
    return '<p>You have <a href="cart.php" >'.count($items ).' item'.$s.'
    in your shopping cart</a></p>';
    }
    }
    >
    function showCart() {
    global $db;
    $cart = $_SESSION['cart'];
    $output[] = "<form action='cart.ph p?action=update ' method='post'
    id='cart'>";
    $output[] = "<table><tr><td >ID</td>";
    $output[] = "<td>Name</td>";
    $output[] = "<td>Price</td>";
    $output[] = "<td>Quan</td>";
    $output[] = "<td>Subtot al</td></tr>";
    if ($cart) {
    $items = explode(',',$ca rt);
    $contents = array();
    foreach ($items as $item) {$contents[$item] =
    (isset($content s[$item])) ? $contents[$item] + 1 : 1;}
    foreach ($contents as $id=>$qty) {
    $sql = 'SELECT * FROM acc WHERE id = '.$id;
    $result = $db->query($sql);
    $row = $result->fetch();
    extract($row);
    $output[] = "<tr><td>".$ite m['id']."</td>";
    $output[] = "<td>".$ite m['info']."</td>";
    $output[] = "<td>".number_f ormat($item['price'],2)."</td>";
    $output[] = "<td>".$ite m['qty']."</td>";
    $output[] = "<td>&pound;".( $price * $qty)."</td>";
    $output[] = "<td><form method=post><in put
    type='hidden' name='id' value='".$item['id']."'/>";
    $output[] = "<input type='submit' name='remove' value='X'/></form></
    td></tr>";
    $total += $price * $qty;
    }
    $output[] = "<tr><td colspan=4>Sub total:</td><td>£".$tota l."</td></
    tr>";
    $output[] = "<tr><td colspan=4>VAT:</td><td>£".($tot al * 0.175)."</
    td></tr>";
    $output[] = "<tr><td colspan=4>Grand total:</td><td>£".($tot al *
    1.175)."</td></tr>";
    $output[] = "<div><butt on type='submit'>U pdate cart</button></div>";
    $output[] = "</table></form>";
    } else {
    $output[] = "<tr><td colspan=5>- No items found in cart -</td></tr></
    table>";
    }
    return join('',$output );
    }
    ?>
    >
    So I can't see any problems that would be causing this other than the
    db problem but can't find out what that is exactly!
    >
    Any help appreciated
    Thanks, Flic
    >
    Did your query succeed? You assume it did and never check for errors on
    the query. And even if it did succeed, did it return any rows? You're
    assuming it id.

    Neither are valid assumption.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Toby A Inkster

      #3
      Re: PHP saying error in mysql syntax, but written my mysql querybrowser!

      Flic wrote:
      $sql = 'SELECT * FROM acc WHERE id = '.$id;
      Try adding this:

      print "<!--{$sql}-->\n";

      It should help you to debug.

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact
      Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

      * = I'm getting there!

      Comment

      Working...