Search code examples
phpmysqldatabasesql-updatereserved-words

how to use a reserved keyword in query


OK so I have a query that updates a table, it only works however when I remove the group part of the update, without this it works fine.

so I understand group may be a reserved keyword, I have tried placing it like [group] but still it dose not work.

when I echo out the query I get :

UPDATE users SET username='superman', dob='0000-00-00', location='The Daily Planet ', group='2' WHERE id='136'

It just dose not insert to the database. dose any one know how I can get this to work?

Full php code inc query :

require 'core/init.php';
$username = mysql_real_escape_string($_POST["username"]);
$dob = mysql_real_escape_string($_POST["dob"]);
$location = mysql_real_escape_string($_POST["location"]);
$group = mysql_real_escape_string($_POST["group"]);
$user_id = (int)$_POST['id'];

  $result = mysql_query("UPDATE users
              SET username='$username', 
                  dob='$dob',
                  location='$location',
                  group='$group'  
              WHERE 
              id=$user_id");

    header("location:admin.php");

Solution

  • Use backticks ` for any reserved word. Better yet use backticks for any column name, table name and such.

    UPDATE `users`
    SET `username` = '$username', `dob` = '$dob',`location` = '$location',`group` = '$group'  
    WHERE `id` = $user_id