Mysql syntax

I am trying to display information from mysql row on this page. I am using $ _GET because the id is included in the link to the page: www.example.com/page.php?id=1, but it returns this error:

Error: you have an error in your SQL syntax; check the manual corresponding to your MySQL server version for the correct syntax to use next to '=' 1 '' on line 1

Does anyone know how to fix this?

below:

<?php

    $username="xxx";
    $password="xxx";
    $database="xxx";
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");


include 'library/config.php';
include 'library/opendb.php';

if(isset($_GET['id']))
{
   $query  = "SELECT id, title, content, contactname, contactemail, contactnumber ".
             "FROM vacancies".
             "WHERE id = '{$_GET['id']}'";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
   list($id, $title, $content, $contactname, $contactemail, $contactnumber) = mysql_fetch_array($result, MYSQL_NUM);

   $content = htmlspecialchars($content);
}

if(isset($_POST['update']))
{
   $id = $_POST['id'];
   $title   = $_POST['title'];
   $content = $_POST['content'];
   $contactname = $_POST['contactname'];
   $contactemail = $_POST['contactemail'];
   $contactnumber = $_POST['contactnumber'];

   if(!get_magic_quotes_gpc())
   {
      $title   = addslashes($title);
      $content = addslashes($content);
      $contactname = addslashes($contactname);
      $contactemail = addslashes($contactemail);
      $contactnumber = addslashes($contactnumber);
   }

   // update the article in the database
   $query = "UPDATE vacancies
            SET title = '$title', content = '$content', contactname = '$contactname', contactemail = '$contactemail', contactnumber = '$contactnumber'".
        "WHERE id = '$id'";
   mysql_query($query) or die('Error : ' . mysql_error());

   // then remove the cached file
   $cacheDir = dirname(__FILE__) . '/cache/';
   $cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';

   @unlink($cacheFile);

   // and remove the index.html too because the file list
   // is changed
   @unlink($cacheDir . 'index.html');

   echo "<b>Job Entry: '$title' updated</b>";

   // now we will display $title & content
   // so strip out any slashes
      $title   = stripslashes($title);
      $content = stripslashes($content);
      $contactname = stripslashes($contactname);
      $contactemail = stripslashes($contactemail);
      $contactnumber = stripslashes($contactnumber);

}

include 'library/closedb.php';
?>

      

0


source to share


4 answers


Try the following:

$query  = "SELECT id, title, content, contactname, contactemail, contactnumber ".
         "FROM vacancies ".
         "WHERE id = '".$_GET['id']."'";

      



I always try to keep the variables out of my strings, just add them in periods, I find this removes a lot of confusion.

0


source


Check out http://us2.php.net/manual/en/function.mysql-query.php

The problem is you are using too many single quotes here:



"WHERE id = '{$ _GET [' id ']}'";

and your request is not working as expected. Use mysql_real_escape_string () instead.

+1


source


One problem:

   $query = "UPDATE vacancies
            SET title = '$title', content = '$content', contactname = '$contactname', contactemail = '$contactemail', contactnumber = '$contactnumber'".
        "WHERE id = '$id'";

      

contains no spaces between the last column and the WHERE clause. Change it to:

   $query = "UPDATE vacancies
            SET title = '$title', content = '$content', contactname = '$contactname', contactemail = '$contactemail', contactnumber = '$contactnumber' ".
        "WHERE id = '$id'";

      

or my preferred format:

$query = <<<END
UPDATE vacancies
SET title = '$title',
    content = '$content',
    contactname = '$contactname',
    contactemail = '$contactemail',
    contactnumber = '$contactnumber'
WHERE id = '$id'
END;

      

Note. ... You should really avoid fields using mysql_real_escape_string ().

0


source


Remove quotes around

 {$_GET['id']}

      

and

 $id

      

in all your inquiries.

Your id is of type integer, I guess I can't take the quoted version or trying to match an integer key with the string "1"

- Change this line

$result = mysql_query($query) or die('Error : ' . mysql_error());

      

to

$result = mysql_query($query) or die('Error : ' . mysql_error() . "\n\n" . $query);

      

Then you can see exactly which query is going into the DB. Which you can then post here for us to see.

Also please write

describe <tablename>;

      

0


source







All Articles