+ Reply to Thread
Results 1 to 9 of 9

Thread: MySQL Error Message.

  1. #1
    zenlok is offline x10Hosting Member zenlok is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    13

    MySQL Error Message.

    Hi Guys,

    I have tried to use a script that creates a simple list and has a form that adds data to the list and everytime I try and add some data, I get the following error message;

    An Error Occurred: 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 'limit 1' at line 1

    Does anyone know whart this means or how to resolve it?

    Many thanks,

    ZeNLok..

  2. #2
    sarvar's Avatar
    sarvar is offline x10Hosting Member sarvar is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    82

    Re: MySQL Error Message.

    I could check it, but please include the sql query.
    CLICK HERE to Thank Me.
    (You must be logged in into forums.)

  3. #3
    zenlok is offline x10Hosting Member zenlok is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    13

    Re: MySQL Error Message.

    Hi Sarvar,

    Thank you for your reply

    The code is;

    Code:
    <?php
    $hostname     = '';
    $username     = '';
    $password     = '';
    $databasename = '';
    if (!$connection_result = mysql_connect($hostname, $username, $password)) {
     die('Error Connecting to MySQL Database: ' . mysql_error());
    }
    if (!$db_result = mysql_select_db($databasename, $connection_result)) {
     die('Error Selecting the MySQL Database: ' . mysql_error());
    }
    // if we made it here, we are one with the database
    switch($_GET['action']){
     case 'delete':
      $title = 'Delete an Item';
      $content = DeleteItem();
      break;
     case 'edit':
      $title = 'Edit an Item';
      $content = ManageItem();
      break;
     case 'new':
      $title = 'Create an Item';
      $content = ManageItem();
      break;
     case 'save':
      $title = 'Save an Item';
      $content = SaveItem();
      break;
     case 'print':
      PrintList();
      break;
     default:
      $title = 'The List';
      $content = ShowList();
      break;
    }
    
    function ShowList(){
     // we are showing the table, no initial actions are needed
     // so we'll jump straight into the table
     $output = '<table border="1" width="700" cellpadding="5" style="border-collapse: collapse">
        <tr>
         <th  width="50">ItemID</th>
         <th>Item Name</th>
         <th>Owner</th>
         <th  width="70">Quantity</th>
         <th  width="76">Reoccuring</th>
         <th  width="76">Purchased</th>
         <th width="76">Action</th>
        </tr>';
     // run the query, we're putting the things that aren't purchased at the top
     $query = mysql_query("select * from thelist order by purchased asc");
     // loop all the records
     while($row = mysql_fetch_assoc($query)){
      $output .= '<tr>';
      $output .= '<td>' . $row['itemid'] .'</td>';
      $output .= '<td>' . $row['name'] . '</td>';
      $output .= '<td>';
      // if this field is empty, its not for anyone specific
      if(empty($row['owner'])){
       $output .= "Everyone";
      }else{
       $output .= $row['owner'];
      }
      $output .= '</td>';
      $output .= '<td>' . $row['quantity'] .'</td>';
      $output .= '<td>';
      if($row['reoccuring'] == 1){
       $output .= "yes";
      }else{
       $output .= "no";
      }
      $output .= '</td>';
      $output .= '<td>';
      if($row['purchased'] == 1){
       $output .= "yes";
      }else{
       $output .= "no";
      }
      $output .= '</td>';
      $output .= '<td width="76"><a href="?action=edititem&itemid=' . $row['itemid'] . '">Edit</a> - ';
      // need to add slashes as we're dealing with javascript here
      $output .= '<a href="javascript:checkDelete(\'' . addslashes($row['name']) . '\',' . $row['itemid'] . 
    ');">Delete</a></td>';
      $output .= '</tr>';
     }
     $output .= '</table>';
     return $output;
    }
    function DeleteItem(){
     // in the query we convert it to an integer to prevent any injection
     if(mysql_query("delete from thelist where itemid='".(int)$_GET['itemid']."'")){
      $output = '<b>Item deleted successfully!</b><br/><br/>';
     }else{
      $output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
     }
     // show the list
     $output .= ShowList();
     return $output;
    }
    function ManageItem(){
     if(isset($_GET['itemid'])){
      // if we're editing we need to grab the stuff from the database
      // convert to integer (if its not a number it'll become zero
      $itemid= (int)$_GET['itemid'];
      $query = mysql_query("select * from thelist where itemid='" . $itemid . "' limit 1");
      $row = mysql_fetch_assoc($query);
     }else{
      // set up blank array
      $row['itemid'] = '';
      $row['name'] = '';
      $row['purchased'] = '';
      $row['owner'] = '';
      $row['reoccuring'] = '';
      $row['quantity'] = '';
     }
     // we have lots of HTML here, so we're breaking out of PHP, but we need to stop it outputting
     // so we'll use output buffering and capture the result
     ob_start();
     ?>
     <form method="POST" action="<?=$_SEVER['PHP_SELF']?>?action=save">
     <?php
     if(isset($_GET['itemid']) && $itemid > 0){
      echo '<input type="hidden" name="itemid" value="' . $itemid . '">';
     }
     ?>
     <table border="0" width="450">
      <tr>
       <td><font size="2">Item Name:</font></td>
       <td><input type="text" name="name" size="20" value="<?php echo htmlspecialchars($row['name']); 
    ?>"></td>
      </tr>
      <tr>
       <td><font size="2">For someone specific? </font><font size="1">(leave blank if not)</font></td>
       <td><input type="text" name="owner" size="20"  value="<?php echo htmlspecialchars($row['owner']); 
    ?>"></td>
      </tr>
      <tr>
       <td><font size="2">Quantity: </font></td>
       <td><input type="text" name="quantity" size="4"   value="<?php echo $row['quantity']; ?>"></td>
      </tr>
      <tr>
       <td><font size="2">Reoccuring?</font></td>
       <td><input type="checkbox" name="reoccuring" value="yes"   <?php if($row['reoccuring'] == 1){echo 
    "checked"; } ?>></td>
      </tr>
      <tr>
       <td height="23"><font size="2">Purchased already?</font></td>
       <td height="23">
       <input type="checkbox" name="purchased" value="yes" <?php if($row['purchased']== 1){echo "checked"; } 
    ?>></td>
      </tr>
      <tr>
       <td colspan="2">
       <p align="center">
       <input type="submit" value="Add/Edit Item" name="submit"></td>
      </tr>
     </table>
     </form>
     <?php
     // get output buffer and then clean it up
     $output = ob_get_contents();
     ob_end_clean();
     return $output;
    }
    function SaveItem(){
     if(isset($_POST['itemid'])){
      // we are updating
      // using our custom db escape function
      $query = 'update `thelist` set ';
      $query .= " `name`='".db_escape($_POST['name'])."', ";
      $query .= " `owner`='".db_escape($_POST['owner'])."', ";
      $query .= " `quantity`='".(int)$_POST['quantity']."', ";
      if($_POST['reoccuring'] == 'yes'){
       $query .= " `reoccuring`='1', ";
      }else{
       $query .= " `reoccuring`='0', ";
      }
      if($_POST['purchased'] == 'yes'){
       $query .= " `purchased`='1' ";
      }else{
       $query .= " `purchased`='0' ";
      }
      $query .= " where itemid='".(int)$_POST['itemid']."' limit 1";
      if(mysql_query($query)){
       $output = '<b>Item updated successfully!</b><br/><br/>';
      }else{
       $output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
      }
     }else{
      // we are adding
      // we are updating
      // using our custom db escape function
      $query = "insert into `thelist` (`name`,`owner`,`quantity`,`reoccuring`,`purchased`) values ";
      $query .= "('".db_escape($_POST['name'])."','".db_escape($_POST['owner'])."','".(int)$_POST['quantity']."'";
      if($_POST['reoccuring'] == 'yes'){
       $query .= "'1', ";
      }else{
       $query .= "'0', ";
      }
      if($_POST['purchased'] == 'yes'){
       $query .= "'1' ";
      }else{
       $query .= "'0' ";
      }
      $query .= " limit 1";
      if(mysql_query($query)){
       $output = '<b>Item added successfully!</b><br/><br/>';
      }else{
       $output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
      }
     }
     // show the list
     $output .= ShowList();
     return $output;
    }
    function PrintList(){
     // this is a printer version, so we cna just echo it straight out
     $q = mysql_query("select * from thelist where purchased=0 order by owner asc");
     echo "<table border=0>";
     while($row = mysql_fetch_assoc($q)){
      echo "<tr>";
      echo "<td>";
      echo $row['quantity'] . " x";
      echo "</td><td>";
      echo $row['name'];
      if(!empty($row['owner'])){
       echo " for ".$row['owner'];
      }
      echo "</td></tr>";
     }
     echo "</table>";
     die();
    }
    function db_escape($string){
     if(function_exists('mysql_real_escape_string')){
      return mysql_real_escape_string($string);
     }else{
      return mysql_escape_string($string);
     }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
     <head>
      <title> The Interspire Shopping List </title>
      <link rel="shortcut icon" href="cart.png" type="image/png">
     </head>
    <style>
    body,table, td, th, tr {
    font-family: georgia;
    font-size: 10pt;
    }
    </style>
    <script>
    function checkDelete(name,itemid){
    if(confirm('Are you sure you want to delete "'+ name +'" ?')){
     window.location = '?action=delete&itemid='+itemid;
    }else{
    }
    }
    </script>
    <body>
    <h1>The Shopping List - <?=$title?></h1>
    <a href="<?=$_SERVER['PHP_SELF']?>">List Home</a>  |  <a href="?action=new">Add Item</a>  | <a href="?action=print">Printer 
    Version</a><br><br>
    <?=$content?>
    </body>
    </html>
    The schema for the SQL table is;


    Code:
    CREATE TABLE `thelist` (
    `itemid` int(11) NOT NULL auto_increment,
    `name` varchar(255) NOT NULL default '',
    `owner` varchar(255) default '',
    `purchased` int(11) NOT NULL default '0',
    `quantity` int(11) NOT NULL default '0',
    `reoccuring` int(11) NOT NULL default '0',
    PRIMARY KEY(`itemid`)
    ) TYPE=MyISAM;
    Any help would be much appriciated.

    Cheers,

    ZeNLoK
    Last edited by Jarryd; 01-18-2009 at 06:02 AM.

  4. #4
    gptsven is offline x10 Lieutenant gptsven is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    253

    Re: MySQL Error Message.

    atleast put it in a codebox omg

  5. #5
    Jarryd's Avatar
    Jarryd is offline Community Advocate Jarryd has a spectacular aura about
    Join Date
    Nov 2007
    Location
    Darwin, Australia
    Posts
    5,531

    Re: MySQL Error Message.

    Edited, and put in codebox's.
    Jarryd Sullivan
    █ 888-X10-9668 - Jarryd[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004

  6. #6
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: MySQL Error Message.

    Anything like "(int)$_POST['quantity']" should be "intval($_POST['quantity'])"
    That was one of them in the sql query that I believe goes haywire.
    Here is another one: (int)$_POST['itemid'] should be changed to intval($_POST['itemid'])

  7. #7
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: MySQL Error Message.

    i was wondering what the (int) was all about last night...

  8. #8
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: MySQL Error Message.

    You can type cast in PHP, so (int)$var and intval($var) are the same. I think using a type cast is slightly faster, though, since there's no overhead from a function call.

    Anyway, I'm fairly certain the problem is this part:

    PHP Code:
    $query "insert into `thelist` (`name`,`owner`,`quantity`,`reoccuring`,`purchased`) values ";
      
    $query .= "('".db_escape($_POST['name'])."','".db_escape($_POST['owner'])."','".(int)$_POST['quantity']."'";
      if(
    $_POST['reoccuring'] == 'yes'){
       
    $query .= "'1', ";
      }else{
       
    $query .= "'0', ";
      }
      if(
    $_POST['purchased'] == 'yes'){
       
    $query .= "'1' ";
      }else{
       
    $query .= "'0' ";
      }
      
    $query .= " limit 1"
    LIMIT can't be used in an INSERT query, so you need to remove that line.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  9. #9
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: MySQL Error Message.

    Quote Originally Posted by woiwky View Post
    You can type cast in PHP, so (int)$var and intval($var) are the same. I think using a type cast is slightly faster, though, since there's no overhead from a function call.

    Anyway, I'm fairly certain the problem is this part:

    PHP Code:
    $query "insert into `thelist` (`name`,`owner`,`quantity`,`reoccuring`,`purchased`) values ";
      
    $query .= "('".db_escape($_POST['name'])."','".db_escape($_POST['owner'])."','".(int)$_POST['quantity']."'";
      if(
    $_POST['reoccuring'] == 'yes'){
       
    $query .= "'1', ";
      }else{
       
    $query .= "'0', ";
      }
      if(
    $_POST['purchased'] == 'yes'){
       
    $query .= "'1' ";
      }else{
       
    $query .= "'0' ";
      }
      
    $query .= " limit 1"
    LIMIT can't be used in an INSERT query, so you need to remove that line.
    lol, nice find... i always overlook the minor things, and that kills me sometimes when debugging my own work...

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Which is better postgresql or MySQL?
    By Kurai Kumo in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 12-10-2009, 08:17 AM
  3. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  4. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers