Forum Moderators: coopster
On the form page:
<input type="text" name="TextField">
In the processing script:
<?php
include('/path/to/DatabaseConnect.php');
CheckTextboxes('TextField');
$TextFieldEscaped = mysql_real_escape_string($_SESSION['TextField']);
mysql_query("INSERT INTO TableName SET TextField='$TextFieldEscaped';");
function CheckTextboxes ($FieldName) {
$fv = $_POST[$FieldName];
$fv = preg_replace("/[^@\w\d\?\"\'\.\,\ \_\-]/", "", $fv);
$_SESSION[$FieldName] = $fv;
}
?>
The purpose of the CheckTextboxes() function is to strip out unwanted characters (anything not listed in the regex) from the POST'ed data and add the data to a session. I then retrieve the data from the session and add it to the database (only using the session in the first place because I'll also want the data later on).
When using the code as shown above, Line 3 doesn't seem to work, meaning unescaped data is being inserted into the database. If I comment out the preg_replace line from the CheckTextboxes function, the data DOES get escaped properly.
There's got to be a reason for this, but it's beyond me. Anybody have an idea on what's going on here?
This is such a strange error to have happening. I mean, the preg_replace and mysql_real_escape_string lines are both dealing with strings. They should both modify the string independently of one another. But that doesn't seem to be the case.
<?php
session_start();
$link = mysql_connect("localhost","username","password");
mysql_select_db("test");
CheckTextboxes(addslashes('Mom & Pa %%* said "You eat" that dinner!'));//simulate magic_quotes...the slashes get removed by the preg_replace (i think)
$TextFieldEscaped = mysql_real_escape_string($_SESSION['test']);
$query ="INSERT INTO TableName SET TextField='$TextFieldEscaped'";
echo $query; // echos INSERT INTO TableName SET TextField='Mom Pa said \"You eat\" that dinner'function CheckTextboxes ($str) {
$fv = $str;
$fv = preg_replace("/[^@\w\d\?\"\'\.\,\ \_\-]/", "", $fv);
$_SESSION["test"] = $fv;
}
?>