Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error in simple Insert Query [closed]

Tags:

php

mysql

insert

I'm trying to create some basic insert query in PHP and it keeps trowing me an error but i really don't know why , can any you guys please see if there is anything wrong ?

    mysql_query("
INSERT INTO itens (nome, data, cliente, link, desc, img) VALUES ($nome,$data,$cliente,$link,$desc,$img)
") or die(mysql_error());

Update

Pulled from deleted answer of the OP, the code is now:

mysql_query("INSERT INTO itens (nome, data, cliente, link, `desc`, img) 
VALUES ($nome,$data,$cliente,$link,$desc,$img)") or die(mysql_error());

And the error is:

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 'kl,j)' at line 2

The kl and j are the last two things i insert in the form.

like image 845
SaraVieira Avatar asked Apr 12 '26 10:04

SaraVieira


2 Answers

DESC is a MySQL Reserved Keyword, You should escaped it with backtick, ex

INSERT INTO itens (nome, data, cliente, link, `desc`, img) 
VALUES ($nome,$data,$cliente,$link,$desc,$img)
  • MySQL Reserved Keyword

you're query is vulnerable with SQL Injection, please take time to read the article below on how to prevent from it,

  • How can I prevent SQL injection in PHP?
like image 171
John Woo Avatar answered Apr 14 '26 23:04

John Woo


First and foremost - Escape, Escape, Escape OR Learn PDO / mysqli and prepared statements.

Second - know what the reserved keywords are that can't be used for column names; those have to be escaped using backticks.

$sql = sprintf("INSERT INTO itens (nome, data, cliente, link, `desc`, img) VALUES ('%s', '%s', '%s', '%s', '%s', '%s');", 
    mysql_real_escape_string($nome),
    mysql_real_escape_string($data),
    mysql_real_escape_string($cliente),
    mysql_real_escape_string($link),
    mysql_real_escape_string($desc),
    mysql_real_escape_string($img)
);

mysql_query($sql);

Third - I think you made a typo in the table name (itens vs items).

like image 25
Ja͢ck Avatar answered Apr 14 '26 23:04

Ja͢ck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!