dudejma Posted August 1, 2011 Share Posted August 1, 2011 I have this INSERT query: $sql2 = "INSERT INTO hub_change (pilotID, from, to, reason, expDate) VALUES ('$pilotid', '$oldHub', '$newHub', '$reason', '$expDate')"; And I always get this 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, to, reason, expDate) VALUES ('1', '2', '3', '4', '5')' at line 1 What's wrong with it, anyone know? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/ Share on other sites More sharing options...
Muddy_Funster Posted August 1, 2011 Share Posted August 1, 2011 you are wrapping integer values within quotes, thus making them strings. This will not please your database if the fields are set to int, take the quotes out. Also, your actual error is coming from the fact that you have names a column in your table as "from". This is a reserved word. you will either have to rename the column (Recomended) or put backticks arround the colum title to tell your database not to process this as it normaly would. Final result using backticks should be: INSERT INTO hub_change (pilotID, `from`, to, reason, expDate) VALUES ($pilotid, '$oldHub', '$newHub', '$reason', '$expDate' I have only removed the single quotes from $pilotid as I expect that the others are possible varchar/text/date fields that you are entering numerical values into as test data, if they are not then you will need to remove the quotes from thise values aswell. Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250106 Share on other sites More sharing options...
The Little Guy Posted August 1, 2011 Share Posted August 1, 2011 it is highly recommended that you don't use reserved words as table columns. It just makes debugging harder. Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250480 Share on other sites More sharing options...
chintansshah Posted August 2, 2011 Share Posted August 2, 2011 make a habit to add (`) for table fields in queries, But before that you should understand the database designing. Because you should not take (from) as a field name. DO NOT USE reserve keyword as a field name. Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250640 Share on other sites More sharing options...
Muddy_Funster Posted August 2, 2011 Share Posted August 2, 2011 make a habit to add (`) for table fields in queries, But before that you should understand the database designing. Because you should not take (from) as a field name. DO NOT USE reserve keyword as a field name. If you do not use reserverd words then there is absoloutly no need to use backticks either. Why should anyone get into that habit? Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250647 Share on other sites More sharing options...
fenway Posted August 2, 2011 Share Posted August 2, 2011 you are wrapping integer values within quotes, thus making them strings. This will not please your database if the fields are set to int, take the quotes out. But if you're not quoting your literals, you will be open to injection attacks. Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250662 Share on other sites More sharing options...
Muddy_Funster Posted August 2, 2011 Share Posted August 2, 2011 But if you're not quoting your literals, you will be open to injection attacks. shouldn't that be dealt with independantly? Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250673 Share on other sites More sharing options...
fenway Posted August 2, 2011 Share Posted August 2, 2011 I like to strictly enforce where expressions can be used and where the can't be. Quoting an integer doesn't really hurt. Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250729 Share on other sites More sharing options...
kickstart Posted August 2, 2011 Share Posted August 2, 2011 make a habit to add (`) for table fields in queries, But before that you should understand the database designing. Because you should not take (from) as a field name. DO NOT USE reserve keyword as a field name. I would be inclined to say to never use a back tick around field names, etc. That way mistakenly using a reserved word for a field name is obvious as early as possible and can be changed to something less likely to cause issues before much code has been written. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250767 Share on other sites More sharing options...
fenway Posted August 2, 2011 Share Posted August 2, 2011 Yup, agreed -- it's a horrible crutch, useful only for dealing with legacy schemas or random third-party garbage. Quote Link to comment https://forums.phpfreaks.com/topic/243453-whats-wrong-with-this-query/#findComment-1250928 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.