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

Léo*_* 준영 4 php postgresql sanitize

这个问题是基于这个线程

使用 pg_prepare 时是否需要明确的消毒?

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

 $question_id = filter_input(INPUT_GET, 'questions', FILTER_SANITIZE_NUMBER_INT);
Run Code Online (Sandbox Code Playgroud)

我使用 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']));
Run Code Online (Sandbox Code Playgroud)

Ada*_*kin 6

据Postgres的文件pg_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"));
?>
Run Code Online (Sandbox Code Playgroud)

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