在PHP中进行安全的SQL更新

问题描述 投票:1回答:1

作为工作的一部分,我想用表单更新一个数据库。由于数据库很大,而且有很多用户在使用,我希望这种操作至少是安全的,以便更安全。

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]: Invalid parameter number: :infos **。

这个错误是由于bindParam函数使用的参数造成的。然而,我在PostgreSQL数据库的属性中,有 "字符变化 "的信息。我试着把这个参数改为 "text",但错误依旧。

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

下面是我的数据库的截图。

enter image description here

截图上的info参数是 "text",但基本的属性是 "character varying"(140)。

谢谢你的帮助。

postgresql html php
1个回答
3
投票

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

这应该是可行的。$query = $dbconn->prepare("UPDATE signaletique SET infos = :infos WHERE id = :id");

请看 https:/stackoverflow.comquestions10966251sqlstatehy093-invalid-parameter-number-parameter-was-not-defined。 更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.