私は、HTML形式のデータをPostgres DBに保存する次のスクリプトを使用しています。フォームからphp変数への値を格納するこのpg_escape_string関数があります。ウェブ全体を検索すると、pg_escape_stringが文字列をエスケープしてデータベースに挿入されていることがわかりました。私はこれについてあまり明確ではない。実際に何が逃げ出していますか?文字列がエスケープされていると言ったときに実際に何が起こるのですか?pg_escape_stringは正確に何をしますか?
<html>
<head></head>
<body>
<?php
if ($_POST['submit']) {
// attempt a connection
$dbh = pg_connect("host=localhost dbname=test user=postgres");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
// escape strings in input data
$code = pg_escape_string($_POST['ccode']);
$name = pg_escape_string($_POST['cname']);
// execute query
$sql = "INSERT INTO Countries (CountryID, CountryName) VALUES('$code', '$name')";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
echo "Data successfully inserted!";
// free memory
pg_free_result($result);
// close connection
pg_close($dbh);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Country code: <br> <input type="text" name="ccode" size="2">
<p>
Country name: <br> <input type="text" name="cname">
<p>
<input type="submit" name="submit">
</form>
</body>
</html>
これは本当に古いですが、誰もが)(pg_escape_string、この後に出くわすためにSQLインジェクションに対して1つの防衛であるが、それを防ぐことはできません。プリペアドステートメントを使う方が良いです。 (http://stackoverflow.com/questions/732561/why-is-using-a-mysql-prepared-statement-more-secure-than-using-the-common-escape) – pandubear