Forum Moderators: coopster

Message Too Old, No Replies

Apostrophe Problems - PHP MYSQL

         

paseo

2:58 am on Jan 7, 2007 (gmt 0)

10+ Year Member



We are having issues with users with an Apostophe in their First or Last name. Lets say Jim O'brien fills out the form and hits submit, the information in NOT inserted into the DB. After some research, it appears that to insert an ' into the db, it needs to be preceded by a \.

Example, \'

Below is the Mysql query to insert data into the db. Is there any way to streamline this before the insert is complete?

$result=MYSQL_QUERY("INSERT INTO $table (wf_FirstName1,wf_LastName3)".

"VALUES ('$wf_FirstName1', '$wf_LastName3')");

jatar_k

3:00 am on Jan 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you need to take a look at mysql_real_escape_string [php.net] and also stripslashes [php.net] in case you need to display the data after insert

mikewalterz

4:49 am on Jan 7, 2007 (gmt 0)

10+ Year Member



Hello,

Use addslashes.

So if its
$str = "Hey O'brain";

and you use addslashes like:
echo addslashes($str);

than it should come out:
Hey O\'brain.

Peace :D

paseo

7:16 am on Jan 7, 2007 (gmt 0)

10+ Year Member



Thanks! Worked great.

jatar_k

12:29 pm on Jan 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you're better off using mysql_real_escape_string as it is more secure

alfaguru

6:10 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



you're better off using mysql_real_escape_string as it is more secure

I'd put that more strongly: you should never, ever write variable data that originated outside your program to a MySQL database without quoting it using mysql_real_escape_string.

paseo

10:03 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



Would something like this work?

$result=MYSQL_QUERY("INSERT INTO $table wf_FirstName1,wf_LastName3)",
"VALUES ('$wf_FirstName1', '$wf_LastName3')",
mysql_real_escape_string($wf_FirstName1),
mysql_real_escape_string($wf_LastName3));

eelixduppy

10:07 pm on Jan 7, 2007 (gmt 0)



It would look more like this:

$result=mysql_query("INSERT INTO $table wf_FirstName1,wf_LastName3) VALUES ('".mysql_real_escape_string($wf_FirstName1)."', '".mysql_real_escape_string($wf_LastName3)."')";

:)

paseo

10:09 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



Now should i use the escape string agrument for ALL the values being inserted into the db or just the ones that might include an apostrophe? Im kinda getting the feeling that it would be needed to be added to every value being inserted to avoid SQL Injection Attacks? Am i right to assume this?

eelixduppy

10:12 pm on Jan 7, 2007 (gmt 0)



>>>Am i right to assume this?

Yes you are, however, I was unsure if

$table
was defined from the client or by you in the script before the query. If any variable is put into a query where its value comes from user input then it must be escaped first.

paseo

10:13 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



Also, i was looking at the beggingni and closing " and (). Can you double check to make sure that the statment is correctly written to begin and end the " and () because the statement starts off ass (" but ends with )"

eelixduppy

10:23 pm on Jan 7, 2007 (gmt 0)



You're right! I don't usually put the query within the mysql_query function so I forgot to this time ;)


$result=mysql_query("INSERT INTO $table wf_FirstName1,wf_LastName3) VALUES ('".mysql_real_escape_string($wf_FirstName1)."', '".mysql_real_escape_string($wf_LastName3)."')"[b])[/b];

Good luck!

paseo

10:26 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



One more question,

("INSERT INTO $table wf_FirstName1,wf_LastName3)

Is there supposed to be a ) at the end of wf_LastName3?

Because that leaves an extra ) at the end of the statment

eelixduppy

10:30 pm on Jan 7, 2007 (gmt 0)



Haha...I'm just not thinking properly today, huh? I'm actually missing a parenthesis:

$result=mysql_query("INSERT INTO $table [b]([/b]wf_FirstName1,wf_LastName3) VALUES ('".mysql_real_escape_string($wf_FirstName1)."', '".mysql_real_escape_string($wf_LastName3)."')");

paseo

10:30 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



Perfect! Thank You Very Much! I will try this out and will post the results. I really do appreciate your help!

paseo

11:22 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



It worked Great! Thank You!

eelixduppy

11:24 pm on Jan 7, 2007 (gmt 0)



Nice! Glad to see you got it sorted :)