tags:

views:

110

answers:

4

I'm trying to display info from a mysql row on this page. I'm 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 that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 1

Does anyone know how to fix this?

code 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';
?>
A: 

One problem:

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

results in no space 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 escape the fields using mysql_real_escape_string().

cletus
+1  A: 

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

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

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

and your query is not acting as expected. use mysql_real_escape_string() instead.

Alex
A: 

Remove the quotes around

 {$_GET['id']}

and

 $id

in all your queries.

Your id is of type integer I assume, which can't take a quoted version or it tries to match the integer key to 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 what query is going into the DB. Which you can then post here for us to see.

Also please post a

describe <tablename>;
jim
hmmm, I'm pretty new to all this, I've done this, and also what cletus suggested, but no joy. Thanks for all your helpany other suggestions?
updated can you do what I asked. Thanks
jim
Thanks dude, I tried what Kyle suggested and it seemed to work. Thanks heaps for your help!
A: 

Try this:

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

I always try to leave the variables out of my strings, just add them in with periods, I find it eliminates a lot of confusion.

Kyle
Thanks Kyle, Problem solved! I never thought of putting the $_GET in there, and now it makes perfect sense!
.. it doesn't make a difference...
jim
My pleasure. Also, as a few people below said, you should always escape user submitted content with mysql_real_escape_string() to help prevent mysql injection. Just change $_GET['id'] to mysql_real_escape_string($_GET['id']).
Kyle