插入数据库表时striplashes无法正常工作

时间:2012-10-12 03:48:18

标签: php mysql

当我尝试运行此代码时:

<?php
$str = "Patty O'Furniture";
if(get_magic_quotes_gpc())
    echo stripslashes($str);
?>

输出为Patty O'Furniture但是当我尝试运行此代码(将数据上传到数据库表)时

<?php
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]) {
            if(get_magic_quotes_gpc())
            mysql_query("INSERT INTO sms_recipient (store_id, recipient_name, recipient_phone) VALUES
                (
                    '".$login_id."',
                    '".stripslashes($data[0])."',
                    '".stripslashes($data[1])."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    //
}
?>

仅输出Patty O。 (见截图)

enter image description here

我不确定为什么当我尝试使用函数stripslashes将数据保存到数据库时,它无效。

3 个答案:

答案 0 :(得分:1)

因为您正在撤消magic_quotes尝试执行的操作。

  

当magic_quotes打开时,所有'(单引号),(双引号),\   (反斜杠)和NUL会自动使用反斜杠进行转义。

因此,您要删除正在添加的stripslashes的反斜杠,并在查询到达'中的O'Furniture

时将其缩短

答案 1 :(得分:0)

使用mysql_real_escape_string()函数代替stripslashes()

尽量避免mysql_*个功能,尝试使用mysqliPDO

答案 2 :(得分:0)

您所看到的是SQL Injection的基础。删除斜杠后,您的输入根本不会被转义。想象一下,如果攻击者提供的输入字符串关闭了您的查询并开始使用'; UPDATE users SET password WHERE username="admin"新的查询,会发生什么?

至少,您需要使用mysql_real_escape_string转义输入,但实际上您需要停止使用mysql扩展名。请改用mysqli扩展名的预备语句。

相关问题