在下面这个基于 mysql 的逐字代码中,解析了一个 CSV 文件并动态创建了一个 mySQL 表。 我刚刚遇到了 -> http://www.johnboy.com/blog/tutorial-import-a-csv-file-using-php-and-mysql
如您所见,他们正在使用addslashes。
这段代码节省了我的时间,但在我开始使用它之前(在 5.3 的 WordPress 环境中,mysqli 或 PDO 不可用),我想请教一下它是否安全。
这一切都归结为addslashes。我很好奇为什么他们不选择 my_real_escape_string。
//connect to the database
$connect = mysql_connect("localhost","username","password");
mysql_select_db("mydatabase",$connect); //select the table
//
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: import.php?success=1'); die;
}
最佳答案
PDO
和 MySQLi
具有用于创建准备好的查询的内置函数,使您可以毫无问题地使用引号和撇号。
但是对于您的问题:将addslashes()
替换为mysql_real_escape_string()
。
addslashes
可以很容易地通过,而且一点也不安全。
关于php - SQL注入(inject)和addSlashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595181/