phppaper Posted July 1, 2008 Share Posted July 1, 2008 Hello all, can anyone tell me whats wrong here, I keep getting error message: $aname = 'tom'; $aword = 'abc'; $temp = mysql_query("INSERT INTO tablename (field) VALUES ('$aword') where name='$aname'")or die (mysql_error()); "name" is one of the field name. Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/ Share on other sites More sharing options...
LooieENG Posted July 1, 2008 Share Posted July 1, 2008 What error messages? Do the table "tablename" and column "field" exist in the database? Are you properly connected to the database? Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579084 Share on other sites More sharing options...
papaface Posted July 1, 2008 Share Posted July 1, 2008 try: $temp = mysql_query("INSERT INTO `tablename` (`field`) VALUES ('$aword') where `name`='$aname'")or die (mysql_error()); Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579087 Share on other sites More sharing options...
phppaper Posted July 1, 2008 Author Share Posted July 1, 2008 Thanks, but it seems doesn't work I get "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 'where name=...... any idea? Is it right to write it like this in a query with insert and where condition?? Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579091 Share on other sites More sharing options...
dmccabe Posted July 1, 2008 Share Posted July 1, 2008 I was just going to say I dont think you need the WHERE condition do you? Sure you are just inserting the info, so what is the WHERE there to do? Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579095 Share on other sites More sharing options...
phppaper Posted July 1, 2008 Author Share Posted July 1, 2008 well, I want to insert the data where the name field = tom for example, because in the database there are many different names in name field Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579100 Share on other sites More sharing options...
revraz Posted July 1, 2008 Share Posted July 1, 2008 Yep, can't use WHERE with INSERT. Do you want to UPDATE? Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579106 Share on other sites More sharing options...
phppaper Posted July 1, 2008 Author Share Posted July 1, 2008 thanks , but with update, I need to have data within the field right?? What if the field is empty, any way I can do the job?? Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579112 Share on other sites More sharing options...
AndyB Posted July 1, 2008 Share Posted July 1, 2008 UPDATE will normally update ALL records that match the WHERE condition. SO, if you have more than one record where the target field is 'empty' then all such records would update. If that's not what you want to do, you will need something unique in the WHERE clause to update a single record reliably. If your table happens to contain an auto-incremented record id for example ... Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579117 Share on other sites More sharing options...
revraz Posted July 1, 2008 Share Posted July 1, 2008 Use INSERT with ON DUPLICATE KEY UPDATE http://dev.mysql.com/doc/refman/5.0/en/insert.html Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579118 Share on other sites More sharing options...
phppaper Posted July 1, 2008 Author Share Posted July 1, 2008 Thanks guys!! Problem solved Link to comment https://forums.phpfreaks.com/topic/112755-sql-syntax-error-dont-know-why/#findComment-579120 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.