I'm getting an error when I run this. I'm sure it's simple. Any thoughts?

Code:

mysql_query("INSERT INTO leads `facility` =  '{$facility}' , `leadAdded` =  '{$today}' , `updatedBy` =  '{$username}' , `manager` =  '{$username}' , `name` =  '{$_POST['name']}' , etc...  ") or die(mysql_error());

Error: "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 'facility = 'A-1' , leadAdded = '2012-05-27 20:05:46' , updatedBy = 'Kei' at line 1"

Recommended Answers

Insert syntax is wrong

INSERT INTO table_name [(column_names, ...)] VALUES (value [, values...])

In other words

INSERT INTO leads (`facility`, ...) VALUES ('{$facility}', ...)
Jump to Post

you can equally use:
$insert = "insert into leads values ('{$facility}',...); "
$query = "mysql_query($insert)";
$query;

Jump to Post

@bonsoirval you're right, but that assumes he's specifying values for all columns in the table, in the order they appear in the table schema.

Jump to Post

All 10 Replies

Insert syntax is wrong

INSERT INTO table_name [(column_names, ...)] VALUES (value [, values...])

In other words

INSERT INTO leads (`facility`, ...) VALUES ('{$facility}', ...)

you can equally use:
$insert = "insert into leads values ('{$facility}',...); "
$query = "mysql_query($insert)";
$query;

@bonsoirval you're right, but that assumes he's specifying values for all columns in the table, in the order they appear in the table schema.

Member Avatar

@KeithMon

I think you forgot the SET:

mysql_query("INSERT INTO leads SET `facility` =  '{$facility}' , `leadAdded` =  '{$today}' , `updatedBy` =  '{$username}' , `manager` =  '{$username}' , `name` =  '{$_POST['name']}' , etc...  ") or die(mysql_error());

BTW - you don't need braces around simple variables - save those for array items, like {$_POST['name']}.

... Al... that syntax is for UPDATE statements, not INSERT

I was mistakenly using UPDATE syntax.

Thanks for your help guys!

Oh and thanks @diafol. I'll remove the brackets.

Member Avatar

... Al... that syntax is for UPDATE statements, not INSERT

Erm, I think you'll find that it works.

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    SET col_name={expr | DEFAULT}, ...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

Erm, I think you'll find that it works

Conceeded.

I had to go back and read the documentation again as I've never seen it done that way before. AFIK that syntax is not standard SQL - at least not T-SQL or PL/SQL - but is MySQL specific.

Member Avatar

I think you're right. It's not standard SQL, but it's part of the MySQL spec. If using vanilla MySQL, it's quite handy. I always find single record entry a pain with the VALUES syntax - especially if you have a lot of fields to keep track of.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.