首页 > 解决方案 > 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();
    }
?>

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

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

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

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

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

在此处输入图像描述

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

感谢您的帮助。

标签: postgresqlhtmlphp

解决方案


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

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

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


推荐阅读