abch624 Posted July 1, 2009 Share Posted July 1, 2009 Hi Guys, I have a sql query that is not working when I enter a value with single quotes. The query is listed bellow: $sql_check = "SELECT * FROM venue WHERE name = '$restaurant_name' AND address = '$address' AND city = '$city' AND country = '$country' AND postcode LIKE '%$postcode%'"; SELECT * FROM venue WHERE name = 'Inn The Park Restaurant' AND address LIKE '%St James's Park, Westminster%' AND city = 'London' AND country = 'United Kingdom' AND postcode LIKE '%W86TA%' I carry out the following functions on the user input: $address = $_POST['address']; $address = filter_var($address, FILTER_SANITIZE_STRIPPED); $address = mysql_real_escape_string($address); Any help why I get an error or any other function I need to run on the user input? Cheers - Zahid Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2009 Share Posted July 1, 2009 Any help why I get an error Only if you post the error so we would have a clue about the point where the problem is being detected. And the raw query that you posted does not have the same syntax for the address as the populated query, so we are somewhat unsure as to what your code really is. Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867073 Share on other sites More sharing options...
abch624 Posted July 1, 2009 Author Share Posted July 1, 2009 The query is SELECT * FROM venue WHERE name = 'Inn The Park Restaurant' AND address = 'St James's Park, Westminster' AND city = 'London' AND country = 'United Kingdom' AND postcode LIKE '%W86TA%' and SELECT * FROM venue WHERE name = '$restaurant_name' AND address = '$address' AND city = '$city' AND country = '$country' AND postcode LIKE '%$postcode%' I get the error of "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 's Park, Westminster' AND city = 'London' AND country = 'United Kingdom' AND post'" Do let me know if you need more info. Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867368 Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2009 Share Posted July 1, 2009 You need to use mysql_real_escape_string() on each piece of string data put into the query. Your actual code is not doing that or your actual code is removing the escape characters after they have been added. Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867371 Share on other sites More sharing options...
AwptiK Posted July 1, 2009 Share Posted July 1, 2009 The apostrophe (') in James's. That makes address = 'St James'(error here) Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867373 Share on other sites More sharing options...
abch624 Posted July 1, 2009 Author Share Posted July 1, 2009 The apostrophe (') in James's. That makes address = 'St James'(error here) I know that is causing the problem the remedy is what I was looking for... Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867380 Share on other sites More sharing options...
JJ2K Posted July 1, 2009 Share Posted July 1, 2009 You need to escape the single apostrophe with a backslash like so: St Jame\'s Park which makes the query: SELECT * FROM venue WHERE name = 'Inn The Park Restaurant' AND address = 'St James\'s Park, Westminster' AND city = 'London' AND country = 'United Kingdom' AND postcode LIKE '%W86TA%' Notice the backslash Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867393 Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2009 Share Posted July 1, 2009 Cannot really help you without seeing the code responsible for the symptoms - You need to use mysql_real_escape_string() on each piece of string data put into the query. Your actual code is not doing that or your actual code is removing the escape characters after they have been added. Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867398 Share on other sites More sharing options...
abch624 Posted July 1, 2009 Author Share Posted July 1, 2009 Cannot really help you without seeing the code responsible for the symptoms - You need to use mysql_real_escape_string() on each piece of string data put into the query. Your actual code is not doing that or your actual code is removing the escape characters after they have been added. Thanks a lot. I could not copy and paste the full code here due to copyright. But I have solved the problem Cheers Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867409 Share on other sites More sharing options...
PFMaBiSmAd Posted July 2, 2009 Share Posted July 2, 2009 No one asked you to post the full code, only the code responsible for the symptoms, i.e. the relevant code from the point it is escaping the data up to and including the code putting that data into the query. Quote Link to comment https://forums.phpfreaks.com/topic/164369-solved-php-mysql-insert-problem/#findComment-867762 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.