這裏是原代碼:魔術報價和mysql_real_escape_string
if (($handle = fopen($source_file, "r")) !== FALSE) {
$columns = fgetcsv($handle, $max_line_length, ",");
foreach ($columns as &$column) {
$column = str_replace(".","",$column);
}
while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) {
while(count($data) < count($columns)) {
array_push($data, NULL);
}
$c = count($data);
for($i = 0; $i < $c; $i++) {
$data[$i] = "'{$data[$i]}'";
}
$sql[] = '(' . implode(',', $data) . ", '" . $_POST['custgroup'] . "'," . $_POST['user_id'] . ')';
}
$query = "INSERT INTO $target_table (" . implode(',', $columns) .
',custgroup,user_id) VALUES ' . implode(',', $sql);
//mysql_query($query) or trigger_error(mysql_error());
echo $query;
fclose($handle);
}
但是,一旦我添加mysql_real_escape_string:
$query = "INSERT INTO $target_table (" . implode(',',array_map('mysql_real_escape_string', $columns)) .
',custgroup,user_id) VALUES ' . implode(',',array_map('mysql_real_escape_string', $sql));
查詢將變成:
INSERT INTO UserAddedRecord (lastname,firstname,ceLL,fax,email,code,custgroup,user_id) VALUES (\'Last\',\'First\',\'01122331\',\'\',\'[email protected]\',\'12345\', \'\',17)
我檢查了我php.ini和get_magic_quotes_gpc(),魔術報價被禁用。
magic_quotes_gpc的=關
magic_quotes_runtime的=關
magic_quotes_sybase =關
應該是什麼問題呢?或者我應該只適用stripslashes()函數?但我相信它會只有當魔法使用報價已啓用。
是什麼在'$ columns'和'$ sql'?請編輯您的問題以包含它。 – Treffynnon
您正在使用PHP和MySQL,但您沒有使用PDO。這是爲什麼? –
@Treffynnon我編輯了這個問題,謝謝。 –