我有使用 mysql_real_escape_string 将名称输入 MySQL 数据库的脚本,以便正确处理撇号。我遇到的问题是使用下面的脚本检查使用另一种形式输入的名称是否与我的数据库中已有的名称相对应,如果找到名称会更新该行。
当我尝试在此脚本处理的表单中输入带有撇号的名称时,我收到一条错误消息,指出未找到该名称,并且错误消息中的名称在撇号前包含一个反斜杠,这显然是问题。
所以问题是,我该如何修改下面的脚本,以便它可以使用带有撇号的名称?
谢谢,
缺口
$row_count = count($_POST['name']);
if ($row_count > 0) {
mysql_select_db($database, $connection);
$name = array();
$workshop = array();
$not_found = array();
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name[$i] = mysql_real_escape_string(ucwords($_POST['name'][$i]));
$workshop[$i] = mysql_real_escape_string($_POST['workshop'][$i]);
}
$names = "('".implode("','",$name)."')";
$not_in = Array();
// lets say all names doesn't exist in `conference`
foreach($name as $value) {
// names in array are keys, not values
$not_in[$value] = true;
}
$query = mysql_query("SELECT Name FROM conference WHERE Name IN $names");
while(list($dbname) = @mysql_fetch_row($query)) {
// delete those name from $not_in who exists
unset($not_in[$dbname]);
}
// names in $not_in array are keys, not values
$not_in = array_keys($not_in);
if(empty($not_in)) {
// its ok, all names have been found. do the magic.
for($i = 0; $i < $row_count; $i++) {
$sql = "UPDATE conference SET Workshop = '$workshop[$i]' WHERE Name LIKE '$name[$i]'";
mysql_query($sql);
$body .= "Name: " . $name[$i] . " Workshop: " . $workshop[$i] . "\n\n";
}