Philwn Posted January 24, 2011 Share Posted January 24, 2011 Hoping someone can help me, ive spent the last hour looking up examples of mysql_query(Insert) and changing my query but im still gettin a syntax error and I cant understand where. Ive tried removing apostrophies and putting the longest variable string outside of the speach marks without any joy. mysql_query("INSERT INTO $QA_table (SUB_CATEGORY, QUESTION, FROM, FROM_EMAIL, DISPLAY) VALUES ('$sub_cat','$message','$from','$from_email','2')") or die(mysql_error()); I get the following 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 'FROM, FROM_EMAIL, DISPLAY) VALUES ('Range','test question bla bla bla','Phil','phi' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/ Share on other sites More sharing options...
mike12255 Posted January 24, 2011 Share Posted January 24, 2011 $query = " INSERT INTO ".$QA_table." (SUB_CATEGORY, QUESTION, FROM, FROM_EMAIL, DISPLAY) VALUES ('$sub_cat','$message','$from','$from_email','2')"; $res = mysql_query($query) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/#findComment-1164439 Share on other sites More sharing options...
Philwn Posted January 24, 2011 Author Share Posted January 24, 2011 this is still chucking out the same syntax error. could it be the fact the $message is a string with spaces in. do i need to use addslashes? Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/#findComment-1164444 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2011 Share Posted January 24, 2011 FROM (the point in the query that is being called out in the error message) is a reserved mysql keyword, as in SELECT * FROM table_name. You either need to rename your column to something else or you must enclose the column `FROM` in back-ticks every time you use it in a query. Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/#findComment-1164445 Share on other sites More sharing options...
mike12255 Posted January 24, 2011 Share Posted January 24, 2011 FROM is a reserved mysql keyword, as in SELECT * FROM table_name. You either need to rename your column to something else or you must enclose the column `FROM` in back-ticks every time you use it in a query. good call, dont know how I missed it. Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/#findComment-1164451 Share on other sites More sharing options...
Philwn Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks guys makes perfect sense, have changed the column name and works now. While im at it You may be able to help with another question I have. That code is part of a script processed by a form. each row has a column called ID which is the primary key and set to auto increment. Is there a way of after inserting the information into the table bringing back the ID number of that information entered or would I have to use a query to find the ID of the latest entry in the table. I ask as if it was done using a query to find the id of the last table entry there would be a problem if two questions were submitted at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/#findComment-1164456 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2011 Share Posted January 24, 2011 See this link - mysql_insert_id Yes it would be a problem if two or more rows were inserted at the same time. Using mysql_insert_id avoids this problem since the value returned is per connection and it will return the id of the row that was just inserted in the current invocation of your script. Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/#findComment-1164462 Share on other sites More sharing options...
Philwn Posted January 24, 2011 Author Share Posted January 24, 2011 Exactly what I was after Thank you. without knowing what it was called or how to word what im trying to achieve makes it harder to look up the function im after. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/225506-mysql_query-syntax-error/#findComment-1164469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.