为什么我的数据没有使用mysql_real_escape_string正确剥离?

时间:2017-10-13 19:40:17

标签: php mysql

以下是我的代码:

    $studentTalking = mysql_real_escape_string($_POST['studentTalking']);

    //Finally, we can actually the field with the student's information
    $sql = <<<SQL
    UPDATE `database` SET
        `studentName`='$studentName',
        `studentEmail`='{$data['studentEmail']}',
        `studentPhone`='{$data['studentPhone']}',
        `studentID`='{$data['studentID']}',
        `studentTalking`= '{$studentTalking}',
        `resume` = '{$data['resume']}'
    WHERE `id`={$data['date_time']} AND (`studentName` IS NULL OR `studentName`='')
SQL;

我正在尝试使用mysql_real_escape_string来允许用户在不破坏数据库的情况下进入我们的表单的撇号,但数据将作为null或撇号将破坏数据库。我已经改变了我能想到的一切,并且无法弄清楚为什么这不起作用。是的,我理解注入可能会破坏我们的数据库,我们将尽快更新代码到mysqli,但我们现在需要这个工作。我怀疑我的语法不正确,第一行可能需要移动到某处,但我不是PHP中最强的,我正在使用以前的实习生编写的代码。提前谢谢。

2 个答案:

答案 0 :(得分:4)

切换到mysqli_*功能是正确的答案。

如果打算使用已弃用且危险的mysql_*函数,那么答案是:

在此设置一个等于转义$_POST[]的新变量:

$studentTalking = mysql_real_escape_string($_POST['studentTalking']);

但是在你的SQL中,你仍然会引用$ _POST数组...切换你的SQL以使用你创建的新变量

$sql = <<<SQL
    UPDATE `tgtw_rsvp` SET
        `studentName`='$studentName',
        `studentEmail`='{$data['studentEmail']}',
        `studentPhone`='{$data['studentPhone']}',
        `studentID`='{$data['studentID']}',
        `studentTalking`= '$studentTalking',
        `resume` = '{$data['resume']}'
    WHERE `id`={$data['date_time']} AND (`studentName` IS NULL OR `studentName`='')
SQL;

答案 1 :(得分:2)

因为您没有使用剥离的变量,但仍然是原始的POST数据。