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

    $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 及其工具来构建我的数据库并与之交互。

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

http://img.mukewang.com/6442335c00012dec06540027.jpg

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

感谢您的帮助。


一只萌萌小番薯
浏览 101回答 1
1回答

POPMUISE

在您的查询字符串中,您将单引号放在占位符周围。这使它们成为字符串,而不是占位符。使用占位符时不需要引号。这应该工作:$query = $dbconn->prepare("UPDATE signaletique SET infos = :infos WHERE id = :id");
打开App,查看更多内容
随时随地看视频慕课网APP