php - 通过 PHP 清理 PostgreSQL 中所有用户的输入

标签 php postgresql sanitize

本题基于this thread .

使用 pg_prepare 时是否需要显式清理?

我觉得 pg_prepare 会自动清理用户的输入,所以我们不需要这个

 $question_id = filter_input(INPUT_GET, 'questions', FILTER_SANITIZE_NUMBER_INT);

我使用 Postgres 的环境

 $result = pg_prepare($dbconn, "query9", "SELECT title, answer
     FROM answers 
     WHERE questions_question_id = $1;");                                  
 $result = pg_execute($dbconn, "query9", array($_GET['question_id']));

最佳答案

根据 Postgres documentationpg_prepare 上,所有转义都已为您完成。请参阅示例部分,其中列出了以下代码(包括注释):

<?php
// Connect to a database named "mary"
$dbconn = pg_connect("dbname=mary");

// Prepare a query for execution
$result = pg_prepare($dbconn, "my_query", 'SELECT * FROM shops WHERE name = $1');

// Execute the prepared query.  Note that it is not necessary to escape
// the string "Joe's Widgets" in any way
$result = pg_execute($dbconn, "my_query", array("Joe's Widgets"));

// Execute the same prepared query, this time with a different parameter
$result = pg_execute($dbconn, "my_query", array("Clothes Clothes Clothes"));
?>

尽管注意它们在查询字符串周围使用单引号 (') 而不是双引号 (") 可能很有用,例如 $1 不会意外插入到字符串中。

关于php - 通过 PHP 清理 PostgreSQL 中所有用户的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1250233/

相关文章:

PHP 删除 Windows ^M 字符

ruby-on-rails - <span style=...> sanitizer 安全吗?

php - 在 JavaScript 中插入 PHP 代码

php - php方括号语法错误

postgresql - 检查数据库是否在生产环境中运行

javascript - 在 javascript 中清理innerHTML

php - 使用 php include 类函数

php - 执行 FQL 时出现 SSL 证书问题

ruby-on-rails - Rails Postgres - 日期时间值不会保存到日期时间列中

php - 如何使 php 与 postgresql 一起工作?