Forum Moderators: coopster
I have a form that has a text field in it where companies will be inputting thier names (other fileds may have this also). Some of them may look like this:
Some Guy's Company
The problem arises with the ' or even an "
It gives an error obviously when it tries to write it to the database. I know there is a way to "strip slashes" or something like that, that will allow me to actually put something like:
Some Guy\'s Company
into the database. I just cant seem to find it. If someone can pint me in the right direction that would be great!
Modern Merlin
$bob = "Can't you see!";
$bob = str_replace('"', '\"', $bob);
$bob = str_replace("'", "\'", $bob);
$bob = str_replace("$", "\$", $bob);
$bob = str_replace("@", "\@", $bob);
You could probably automate the process by making an array of symbols you want converted.
$bob = "Can't you see!";
$checksym = Array('"', "'", "$", "@", "!", "#");//Just add the symbols to the array.
foreach($checksym as $tmp)
{
$bob = str_replace($tmp, "\" . $tmp, $bob);
}
And to reverse the process, just switch the first to options in the str_replace.
$bob = str_replace("\" . $tmp, $tmp, $bob);
The array is probably easiest to do for many different replacements. If you wanted, you could probably do it to place smilies inside of a message or something using a second array.
[EDIT]
O-o I forgot that in order to use a \ in a string, you have to put \\, so the above code would need two backslashes. Example:
<?
$bob = "Can't you \" see!";
$checksym = Array("'", '"', "!");
foreach($checksym as $tmp)
{
$tmpback = "\\" . $tmp;
$bob = str_replace($tmp, $tmpback, $bob);
}
echo $bob;
?>
That will display out the code as it would be input into the database.
[edited by: Caliber_Mengsk at 6:55 pm (utc) on Nov. 16, 2007]
Connection code and select DB Code (which I know works)
mysql_real_escape_string (('$order_id','$on_camera','$graphics_animation','$photo_shoot','$stock_video','$producer','$script_writer'), $link);
$insert_orders = mysql_query ("INSERT INTO orders VALUES ('$order_id','$on_camera','$graphics_animation','$photo_shoot','$stock_video','$producer','$script_writer')", $link) or die("There was an error: orders");
Or do I have to do them one at a time?
Modern Merlin
Ok so is this correct?
Connection code and select DB Code (which I know works)
mysql_real_escape_string (('$order_id'), $link);
mysql_real_escape_string (('$on_camera'), $link);
mysql_real_escape_string (('$graphics_animation'), $link);
mysql_real_escape_string (('$photo_shoot'), $link);
mysql_real_escape_string (('$stock_video'), $link);
mysql_real_escape_string (('$producer'), $link);
mysql_real_escape_string (('$script_writer'), $link);
$insert_orders = mysql_query ("INSERT INTO orders VALUES ('$order_id','$on_camera','$graphics_animation','$photo_shoot','$stock_video','$producer','$script_writer')", $link) or die("There was an error: orders");