0

mysqli_error() clearly states:

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 'group, uploaded_by, date_uploaded, url) VALUES (1, 1, NOW(), '/gallery/1/h' at line 2

But this is the generated SQL query

INSERT INTO
gallery_images (group, uploaded_by, date_uploaded, url)
VALUES (1, 1, NOW(), '/gallery/1/halflife2180z97stmydo1600x1200.jpg')

It's practically identical to another SQL query I have running on the same site, without errors.

I would understand if I'm trying to insert an invalid value into a field in MySQL, but it clearly states that I have a syntax error, and I just can't see it.

Reply if you can see it. Below is the PHP behind the query


$res = $con->query("
        INSERT INTO
        gallery_images (group, uploaded_by, date_uploaded, url)
        VALUES ($group, {$_SESSION[user]->id}, NOW(), '$escaped_name')
    ");
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • 1
    Just a remark, you should avoid naming a field `group` since it's a word used for query. – Shikiryu Jan 11 '11 at 08:16
  • @ClemDesm: agree with you. `grp` or `group_id` are good alternatives: the meaning remains clear and you don't have query problems. – nico Jan 11 '11 at 08:20
  • HAHA, I didn't know that was the problem and the answer. \o/ – Shikiryu Jan 11 '11 at 08:39
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Ian Ringrose May 06 '14 at 10:18

6 Answers6

4

group is a reserved keyword. Put backticks around it.

$res = $con->query("
        INSERT INTO
        gallery_images (`group`, uploaded_by, date_uploaded, url)
        VALUES ($group, {$_SESSION[user]->id}, NOW(), '$escaped_name')
    ");
nico
  • 50,859
  • 17
  • 87
  • 112
1

Group is a reserved keyword, change that column name.

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
0

group is reserved MySQL keyword, you cannot use it like that. Surround it with backtick `group ` (the symbol near 1 key)

nan
  • 19,595
  • 7
  • 48
  • 80
  • 1
    You need backticks, not single quotes. – cherouvim Jan 11 '11 at 08:16
  • Assuming you have a US keybord the symbol next to 1 is a backtick not a quote (` vs '). – nico Jan 11 '11 at 08:17
  • No problem. To be completely correct, the name of that mark is "grave accent", but "backtick" is used (generally in programming), when it is used by itself. – nico Jan 11 '11 at 08:23
0
gallery_images (group, uploaded_by, date_uploaded, url)

group is keyword

0
$res = $con->query("
        INSERT INTO
        gallery_images (`group`, `uploaded_by`, `date_uploaded`, `url`)
        VALUES ($group, {$_SESSION[user]->id}, NOW(), '$escaped_name')
    ");

Should do it.

Dennis Kreminsky
  • 2,117
  • 15
  • 23
0

group is a reserved word in MySQL. You might have to put it in quotes:

INSERT INTO
gallery_images (`group`, uploaded_by, date_uploaded, url)
VALUES (1, 1, NOW(), '/gallery/1/halflife2180z97stmydo1600x1200.jpg')
Anax
  • 9,122
  • 5
  • 34
  • 68