homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

Mysql Insert Problem

 3:39 pm on Mar 26, 2010 (gmt 0)

I can't seem to insert some data. When I write the query
INSERT INTO table (col1, col2) values ('val1', 'val2');
it does not work

When I write
INSERT INTO table ('col1', 'col2') values ('val1', 'val2');
it does not work.

However, if I copy an insert statement from phpmyadmin it looks like the second query, except I notice the quotes on col1 and col2 seem different. If I copy those quotes around the column names, then it works fine. Any ideas why and how I can get the ' that I type to work so I don't have to copy it? Thanks,



 5:33 pm on Mar 26, 2010 (gmt 0)

Those ticks (`) is what the MySql uses as a delimiter.

Just use them - next to the 1 on the top left of your keyboard


 5:47 pm on Mar 26, 2010 (gmt 0)

INSERT INTO table (col1, col2) values ('val1', 'val2');
it does not work

It should . . . there's nothing wrong with this statement. How are you executing this, via phpMyAdmin? PHP? If PHP, use the mySQL error function in PHP, store the select in a variable, for example $query . . .

$result = mysql_query($query) or die(mysql_error());

See if it gives you any clues.

INSERT INTO table ('col1', 'col2') values ('val1', 'val2');

This will not work, an extension on paladin's post,

INSERT INTO table (`col1`, `col2`) values ('val1', 'val2');

Note they are backticks, not single quotes, but it's relevant to note that backticks are only necessary if you use field names that conflict or have other issues, like starting with a number or a reserved word. For example, int and date are data types in mySQL, if you try to have fields with those names it will definately need backticks.


 9:34 am on Apr 7, 2010 (gmt 0)

Try INSERT INTO TABLE VALUES("val1", "val2"), before that check if the table exists and check if you have defined the primary key.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved