Author Topic: [SOLVED] PHP mysql insert problem  (Read 493 times)

0 Members and 1 Guest are viewing this topic.

Offline abch624Topic starter

  • Enthusiast
  • Posts: 90
    • View Profile
[SOLVED] PHP mysql insert problem
« on: July 01, 2009, 08:19:05 AM »
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

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: PHP mysql insert problem
« Reply #1 on: July 01, 2009, 08:44:04 AM »
Quote
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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline abch624Topic starter

  • Enthusiast
  • Posts: 90
    • View Profile
Re: PHP mysql insert problem
« Reply #2 on: July 01, 2009, 04:50:55 PM »
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.

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: PHP mysql insert problem
« Reply #3 on: July 01, 2009, 04:58:26 PM »
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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline AwptiK

  • Irregular
  • Posts: 33
  • Gender: Male
    • View Profile
Re: PHP mysql insert problem
« Reply #4 on: July 01, 2009, 05:00:57 PM »
The apostrophe (') in James's.

That makes address = 'St James'(error here)

« Last Edit: July 01, 2009, 05:02:49 PM by AwptiK »

Offline abch624Topic starter

  • Enthusiast
  • Posts: 90
    • View Profile
Re: PHP mysql insert problem
« Reply #5 on: July 01, 2009, 05:12:46 PM »
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...

Offline JJ2K

  • Enthusiast
  • Posts: 75
    • View Profile
Re: PHP mysql insert problem
« Reply #6 on: July 01, 2009, 05:41:48 PM »
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

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: PHP mysql insert problem
« Reply #7 on: July 01, 2009, 05:53:02 PM »
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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline abch624Topic starter

  • Enthusiast
  • Posts: 90
    • View Profile
Re: PHP mysql insert problem
« Reply #8 on: July 01, 2009, 06:16:07 PM »
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

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: [SOLVED] PHP mysql insert problem
« Reply #9 on: July 02, 2009, 09:53:25 AM »
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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.