views:

82

answers:

4

I'm running the following line :

mysql_query("INSERT INTO tags 
                SET tag = '".$onesearch."',  
                SET date = '".date('d-m-Y')."'") or die(mysql_error());

...and its dieing saying this:

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 'SET date = '29-08-2010'' at line 1

I can't figure out what's wrong.

+3  A: 

Are you able to output the query that is actually being run? It could be that the tag contains a single quote, which escapes the closing quote & causes problems... Which is why you'd want to use:

mysql_query("INSERT INTO tags 
                SET tag = '". mysql_real_escape_string($onesearch) ."',  
                    date = '".date('d-m-Y')."'") or die(mysql_error());

Which is it you're trying to do here?
You start with INSERT syntax, but after the table reference convert to using UPDATE syntax.

INSERT

mysql_query("INSERT INTO tags 
               (tag, date) 
             VALUES 
               ('".$onesearch."', '".date('d-m-Y')."')") or die(mysql_error());

UPDATE

mysql_query("UPDATE tags 
                SET tag = '".$onesearch."',  
                    date = '".date('d-m-Y')."'") or die(mysql_error());

...but you'll get records in the table to those two values. You'd want a WHERE clause on that...

OMG Ponies
The OP's UPDATE type syntax is valid INSERT syntax, just has too many SET words in it. http://dev.mysql.com/doc/refman/5.1/en/insert.html See the second example.
Brad F Jacobs
@permiso: News to me, thx. I'd still avoid using that syntax for sake of the confusion.
OMG Ponies
Oops -- I had answered the same, but `mysql` **does** support a `SET` clause in `INSERT`, and @Adeel has it right, it's just the _repetition_ of `SET` that's an error. So +1 to her, and I deleted my A (and I recommend the same course to you!-).
Alex Martelli
@OMG Ponies, I would agree with you there :)
Brad F Jacobs
And besides, UPDATE doesn't accept two SET keywords either. :)
Bill Karwin
@Bill Karwin: Thx - corrected. I clearly need to get some sleep
OMG Ponies
+4  A: 

Invalid syntax, you should only use one SET:

mysql_query("INSERT INTO tags 
            SET tag = '".$onesearch."',  
            date = '".date('d-m-Y')."'") or die(mysql_error());

And that is valid INSERT syntax, just an FYI (in response to one of the other answers).

Brad F Jacobs
+7  A: 

Remove second SET from your insert query. it should like be:

mysql_query("INSERT INTO tags 
                SET tag = '".$onesearch."',  
                date = '".date('d-m-Y')."'") or die(mysql_error());
Adeel
Congrats and +1 -- I had never seen that `SET` clause in `INSERT` (a mysql-special way to duplicate standard SQL's `INTO` and `VALUES`, which every other engine supports _as_ does mysql!-), so I deleted my A that specified `INTO` and `VALUES` instead (but it looks like @OMG may not want to delete his, possibly in defense of avoiding wanton, useless use of non-standard SQL constructs when perfectly good equivalent standard ones are available, I guess-).
Alex Martelli
@Alex Martelli yes agreed. Different databse vendors ANSI implementations confuse end users. May be MySQL team REUSES update statement:). One advantage atleast I feel is that it is easy for developers to write this query instead of INSERT-VALUES if your number of columns are more than 7.
Adeel
+2  A: 

i think your question was already answered by the others. but you should take care of that $onesearch variable. if it's not properly sanitized, a quote in it could break your code as well. if you are not doing it already, you should consider using mysql_real_escape_string() to protect you from errors and sql injection.

hugo_leonardo