繁体   English   中英

防止在插入,PHP上进行SQL注入

[英]Prevent SQL Injection on Insert, php

有人可以救我几个小时的生命吗?我完全按照以下步骤操作: http : //docs.php.net/manual/en/pdo.prepared-statements.php

我在sqli上使用了此工具,但该小组告诉我,这对sql注入是有利的,因此我正在尝试改善我的代码

此处的许多主题都在SELECT上。当我尝试执行此操作时,我得到一个空白页,但我仍然没有得到apache来呈现错误,这是一个单独的问题。

这是PHP:

$dbh = new PDO('mysql:host=localhost;dbname=table', $DBuser, $DBpswd );

$stmt = $dbh->prepare("INSERT INTO `sonyCES2013`.`registration`  (`id`, `firstName`, `lastName`, `eMail`, `telephone`, `outlet`, `comfirm`, `boothTour`)  VALUES (
    :id,
    :firstName, 
    :lastName,
    :eMail,
    :telephone,
    :outlet,
    :comfirm,
    :boothTour
    )");


    $stmt->bindParam(':id', NULL);
    $stmt->bindParam(':firstName', $fName);
    $stmt->bindParam(':lastName',$lName);
    $stmt->bindParam(':eMail', $eMail);
    $stmt->bindParam(':telephone', $telephone);
    $stmt->bindParam(':outlet', $outlet);   
    $stmt->bindParam(':comfirm',$comfirmation);
    $stmt->bindParam(':boothTour', $dateFormatted);

    $stmt->execute();

问题是我已经将一些属性设置为NULL。 Comfirm是表单上的可选值,如果没有人选择它,我将变量设为NULL。 我改为将它们设置为空字符串“”。 解决了问题。

空字符串与NULL不同。 同样,您必须通过引用将变量传递给bindParam()

如果要传递NULL作为查询参数,请使用

$stmt->bindValue(':id', NULL);

或者,您可以创建一个虚拟变量并将其传递给PDO提示它为NULL:

$null = null;
$stmt->bindParam(':id', $null, PDO::PARAM_NULL);

否则,只需从INSERT列中省略id

$stmt = $dbh->prepare("INSERT INTO `sonyCES2013`.`registration`  
    (`firstName`, `lastName`, `eMail`, `telephone`, `outlet`, `comfirm`,
     `boothTour`) VALUES  ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM