Page 1 of 1

[SOLVED] String problem

Posted: Mon May 16, 2005 11:13 pm
by S_henry
Let say i have 2 string variables:

Code: Select all

$a = "Book of student";
$b = "Student's book";

$query1 = "INSERT INTO table1 (str1) VALUES ('$a')";
...

$query2 = "INSERT INTO table1 (str1) VALUES ('$b')";
...
If i execute query1 and query2, query1 is successed and query2 is failed. This is because in string $b, there is a character ('). So anybody knows how to solve this problem? I mean the problem if we want to save a string variable that contain a character (').

Posted: Mon May 16, 2005 11:26 pm
by SBro
addslashes()

Code: Select all

$b = addslashes("Student's Book");
$query2 = "INSERT INTO table1 (str1) VALUES ('$b')";

Posted: Mon May 16, 2005 11:56 pm
by Burrito
use mysql_escape_string() to escape ALL the chars that might cause it to bomb...

Posted: Tue May 17, 2005 1:45 am
by S_henry
Yes. Perfect! Thanx guys.. :D