魔术引号和 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\',\'首先\',\'01122331\',\'\',\'[电子邮件受保护]\',\'12345\', \'\', 17)
我检查了我的 php.ini 并get_magic_quotes_gpc(),魔术报价被禁用。
magic_quotes_gpc = 关
magic_quotes_runtime = 关
magic_quotes_sybase = 关
应该是什么问题?或者我应该只应用 stripslashes()?但我假设只有在启用魔术引用时才会使用它。
Here is the original code:
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);
}
But once I added 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));
The query will become :
INSERT INTO UserAddedRecord (lastname,firstname,ceLL,fax,email,code,custgroup,user_id) VALUES (\'Last\',\'First\',\'01122331\',\'\',\'[email protected]\',\'12345\', \'\',17)
I checked my php.ini and get_magic_quotes_gpc(),magic quote is disabled.
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
What should be the problem?or I should just apply stripslashes()?But I assume it will be used only when magic quote is enabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该遵循以下原则:
mysql_real_escape_string
对 mysql_real_escape_string 是转义测试内容中的特殊字符,因此使用 stripslashes 会将其删除。
解决这个问题的另一种方法是查看
preparedStatements
您不需要向客户端转义。请记住,这样做的全部目的是为了防止SQL注入并且采取相应的行动:)
You should to something along the lines of:
mysql_real_escape_string
on all string valuesThe point of mysql_real_escape_string is to escape the special chars within your test content so using stripslashes would take that away.
Another way to solve that is to look into
preparedStatements
where you don't need to to client side esacping.Just remember that the whole point of doing this is to prevent SQL-Injection and act accordingly :)