我使用此脚本将数据插入我的数据库:
<html>
<body>
<?php
include('koneksi.php');
$gid = pg_escape_string ($_POST['gid']);
$nama = pg_escape_string ($_POST['nama']);
$alamat = pg_escape_string ($_POST['alamat']);
$no_telfon = pg_escape_string ($_POST['no_telfon']);
$email = pg_escape_string ($_POST['email']);
$website = pg_escape_string ($_POST['website']);
$longitude = pg_escape_string ($_POST['longitude']);
$latitude = pg_escape_string ($_POST['latitude']);
$query = "INSERT INTO perguruantinggi( gid, nama, alamat, no_telfon, email, website, longitude, latitude )
VALUES ('" . $gid . "', '" . $nama . "', '" . $alamat . "', '" . $no_telfon . "', '" . $email . "', '" . $website . "', '" . $longitude . "', '" . $latitude . "')";
$result = pg_exec($query);
//$query = "INSERT INTO perguruantinggi (username, password, fullname, email, agama, no_hp)
//VALUES ('" . $username . "', '" . $password . "', '" . $email . "', '" . $fullname . "', '" . $agama . "', '" . $no_hp . "')" ;
if (!$result) {
$errormessage = pg_last_error();
echo "Error with query: " . $errormessage;
exit();
}
printf ("These values were inserted into the database - %s %s %s", $firstname, $surname, $emailaddress);
pg_close();
?>
</body>
编译此脚本时发现此错误。我对坐标数据类型使用双精度。
警告:pg_exec()[function.pg-exec]:查询失败:错误:双精度类型的输入语法无效:
答案 0 :(得分:3)
应避免手动引用,因为它既容易出错又容易受到注入攻击。
大多数 SQL 库都有一个带参数的函数,并在查询中使用占位符。该库负责引用,并防止注入点。
此代码的最佳方法可能是使用pg_query_params
,它允许您将参数作为数组传递,并负责为您进行转义。您在查询中使用$1
,$2
等作为占位符。
所以基本上你的VALUES
子句会被($1, $2, $3...)
替换,依此类推,将值传递给pg_query_params
而不是直接插值。
答案 1 :(得分:2)
结合@khampson的说法,我认为你的核心问题是你引用的是latitude
和longitude
字段,我猜这些字段不是字符串,但精确度是双倍的。但是你要将它们解析为PHP $_POST
中的字符串,并将它们构建为SQL查询,就好像它们是字符串一样。
所以你应该做两件事:
pg_query_params
pg_query_params
确保将这些值转换为浮点值。这个
是pg_query_params
可以有适当的知识
键入,以便它知道 NOT 引用它作为字符串,但要处理
它作为原始数字类型。微观例子:
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$params = array(floatval($latitude), floatval($longitude));
$query = "INSERT INTO table (latitude, longitude) VALUES ($1, $2)";
pg_query_params($connection, $query, params);