0

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"

4
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by diafol
0

Insert syntax is wrong

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

In other words

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

Edited by |-|x: link

0

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

0

@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.

0

@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']}.

Edited by diafol

0

I was mistakenly using UPDATE syntax.

Thanks for your help guys!

0

... 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] ... ]
0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.