Forum Moderators: coopster

Message Too Old, No Replies

what is wrong with my SQL query?!?!

         

ocelot

3:08 am on Dec 7, 2004 (gmt 0)

10+ Year Member



I've been struggling with this thing for hours...I cannot understand what the problem is!

When I do the query

INSERT INTO forms (table,universal_id,submitted,submitted_by,notes,processed,charged) VALUES ('wqeqwew', '0', 'wqewqe', 'sDwed', 'wqewqe', '23wqe', 'qweasd')

I get

Could not query: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 'table,universal_id,submitted,submitted_by,notes,processed,charg

when I do the query

INSERT INTO forms VALUES ('wqeqwew', '0', 'wqewqe', 'sDwed', 'wqewqe', '23wqe', 'qweasd')

It works.

What in the world is the problem with my syntax?

Crustov

3:20 am on Dec 7, 2004 (gmt 0)

10+ Year Member



Get rid of the brackets around:
table,universal_id,submitted,submitted_by,notes,processed,charged

ocelot

3:23 am on Dec 7, 2004 (gmt 0)

10+ Year Member



you meant parentheses right? that didn't help. and all the sql insert into tutorials I see use parentheses around the column names

Crustov

3:31 am on Dec 7, 2004 (gmt 0)

10+ Year Member



Sorry about that, I had a bit of a brain fart.

It is possible that it has a problem with the column "table", because it's a reserved word in MySQL.

baze22

3:43 am on Dec 7, 2004 (gmt 0)

10+ Year Member



Are those all string fields? If any are integer like an id number, you wouldn't have quotes around the value.

baze

dmorison

6:48 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think Crustov is right - table is a reserved word and is messing up your query. Ideally I would change the name of the column "table", but if that's not possible you need to use back-ticks around any reserved word that you wish to use to refer to actual objects. Your original query would become:

INSERT INTO forms (`table`,`universal_id`,`submitted`,`submitted_by`,`notes`,`processed`,`charged`) VALUES ('wqeqwew', '0', 'wqewqe', 'sDwed', 'wqewqe', '23wqe', 'qweasd')

Incidentally, there is nothing wrong with the single-quotes around integer or other non-character types - in fact it is good practice - especially in a web environment where you might be constructing a query from untrusted user input.

baze22

12:21 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



Incidentally, there is nothing wrong with the single-quotes around integer or other non-character types - in fact it is good practice - especially in a web environment where you might be constructing a query from untrusted user input.

I didn't know this. Makes sense.

thanks,

baze

Salsa

12:53 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



Dmorison is right that Crustov is right. However, it wouldn't be necessary to backtick all of the field names as in the example, only `table`. It should work either way, however. I only wonder why you didn't see this yourself, ocelot, because you would have had to have known to backtick the `table` field when you created the table.

Baze: I had the same misconception about quoting numbers until a few months ago. It sure makes life easier (and safer) to just quote everything--except NULL--don't do that. Is there anything else that can't be quoted?

saoi_jp

1:31 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



backtick the `table` field when you created the table

Not necessarily. I've done this using phpMyAdmin. ("this" means given a field a reserved name)

Salsa

1:51 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



Not necessarily. I've done this using phpMyAdmin.

Ahh. I only did some testing using the mysql client, but maybe phpMyAdmin adds the backticks for you when you use a reserved word for a field name. That would explain it.