我正在使用以下脚本从html表单中获取数据并存储在Postgres数据库中.有这个pg_escape_string函数,它将表单中的值存储到php变量中.在整个网络中搜索,我发现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);
}
?> …Run Code Online (Sandbox Code Playgroud)