Welcome to WebmasterWorld Guest from 54.167.5.15

Forum Moderators: open

Message Too Old, No Replies

Mysql Insert Problem

     

andrewsmd

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

WebmasterWorld Senior Member 5+ Year Member



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,

paladin

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

10+ Year Member



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

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

rocknbil

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

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

bizminder

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

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month