Search code examples
phpmysqlmysql-error-1064

php -> MYSQL query can't figure out what is wrong getting error #1064


I am trying to insert some values in the table the query is below:

Insert into 
auditlog (
          event,
          desc,
          userid,
          useripaddress,
          audittype
)
VALUES (
         'User Authenticated', 
         'Useradminsuccessfully logged in to the system', 
         '1', 
         '127.0.0.1','1'
) 

It gives me the following error: #1064 - 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 'desc,userid,useripaddress,audittype)VALUES ('User Authenticated', 'User admin su' at line 1

However when i run the insert using PHPMYAdmin it does insert a value and the query generated is

INSERT INTO 
     `auditlog`(
              `event`, 
              `desc`, 
              `userid`, 
              `useripaddress`, 
              `audittype`) 
     VALUES (
              'User Authenticated', 
              'Useradminsuccessfully logged in to the system', 
              '1', 
              '127.0.0.1','1'
     ) 

The only difference i see is the quotes which i dont believe are needed. I don't understand where am i going wrong and am breaking my head now :):)


Solution

  • The backticks are needed around desc because it is a reserved word.

    INSERT INTO auditlog (event, `desc`, userid, useripaddress, audittype)
    VALUES (
        'User Authenticated',
        'Useradminsuccessfully logged in to the system',
        '1',
        '127.0.0.1',
        '1'
    ) 
    

    There is also no harm in adding backticks around the other column names if you aren't sure whether or not they are reserved words.