微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用PHP清理PostgreSQL中所有用户的输入

该问题基于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']));

解决方法:

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

<?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不会意外插入到字符串中.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐