PHP中的安全SQL更新

泰赫留

作为工作的一部分,我想使用表单更新数据库。由于数据库很大并且为许多用户所用,因此我希望这种操作至少是安全的,以提高安全性。

HTML脚本:

<form action="http://localhost/modifier_infos_signaletique.php" method=POST >
    <div class="id_sign">
    <h5>Id "Signalétique" :</h5>
    <input type="text" name="id_sign" id="id_sign"/><br>
    </div>
    <h5>Infos "Signalétique" : </h5>
    <input class="comment" type="text" id="maj_infos" name="maj_infos" required maxlength='140'/><br>
    <input type="submit" value="Submit" />
</form>

PHP脚本:

<?php
    $user = 'xxxx'; 
    $pass = 'xxxx';

    try{
        $dbconn = new PDO('pgsql:host=localhost;port=5432;dbname=xxxx',$user, $pass);
        $dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $maj = $_POST['maj_infos'];
        $id = $_POST['id_sign'];

        $query = $dbconn->prepare("UPDATE signaletique SET infos = ':infos' WHERE id = ':id'");
        $query->bindParam(':infos', $maj, PDO::PARAM_STR);
        $query->bindParam(':id', $id, PDO::PARAM_INT);
        $query->execute();

        echo 'Données mises à jour';
    }
    catch(PDOException $e){
        echo "Erreur : " . $e->getMessage();
    }
?>

但是,当我使用此脚本时,会出现此错误:

** Erreur:SQLSTATE [HY093]:无效的参数号:: infos **

该错误归因于用于bindParam函数的参数。但是,我在PostgreSQL数据库的属性中具有“字符变化”中的信息。我试图将此参数更改为“文本”,但错误仍然相同。

原谅我这个问题,但是我是PHP的新手,因为我经常使用pgAdmin及其工具来构建数据库并与数据库交互,所以我的SQL技能很薄。

这是我的数据库的屏幕截图:

在此处输入图片说明

info参数在屏幕快照的“文本”中,但基本的此属性在“字符变化”中(140)。

感谢您的帮助。

bugmenot123

在查询字符串中,在占位符两边加上单引号。这使它们成为字符串,而不是占位符。使用占位符时不需要引号。

这应该工作:
$query = $dbconn->prepare("UPDATE signaletique SET infos = :infos WHERE id = :id");

有关更多信息,请参见https://stackoverflow.com/questions/10966251/sqlstatehy093-invalid-parameter-number-parameter-was-not-defined

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章