【问题标题】:To sanitize all user's input in PostgreSQL by PHP通过 PHP 清理 PostgreSQL 中所有用户的输入
【发布时间】:2010-11-18 00:36:36
【问题描述】:

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

【问题讨论】:

  • filter_input() 函数与 postgres 有什么关系……还是我遗漏了什么?

标签: php postgresql sanitize


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 2010-12-25
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2013-09-14
    相关资源
    最近更新 更多