postgresql - 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)中。
感谢您的帮助。
解决方案
在您的查询字符串中,您在占位符周围加上单引号。这使它们成为字符串,而不是占位符。使用占位符时不需要引号。
这应该有效:
$query = $dbconn->prepare("UPDATE signaletique SET infos = :infos WHERE id = :id");
推荐阅读
- rstudio - 可以在 rstudio 中突出显示特定文本(如用户定义的单词)吗?
- php - Imagick 给出了错误的 gif 宽度和高度?
- android - 应用因违反 SMS 权限而从 google playstore 下架
- apache - 多个后端服务的 Apache 反向代理上的上下文路径
- c# - 从 windows 窗体调用 .NET core REST api 以发送包括 IFormFile 在内的数据
- python-3.x - 计算单个数据框中具有相同几何形状的多边形数量
- python - 在 GitPython 中迭代提交 b/w 2 个指定的提交
- mysql - Talend Open Studio 使用 MySQL 进行父子关系数据迁移
- mysql - 通过合并两个 ROW 值更新 mysql 列
- html - 覆盖滚动条在 Mozilla 和 IE 中不起作用