PHP Mysqli 将 POINT 插入列

PHP Mysqli insert POINT to column

我正在尝试使用以下代码将经度和纬度存储在 mysql table 数据类型 POINT 中:-

$name = $_POST['name']
$lat = $_POST['lat'];
$long = $_POST['longi'];
$location = 'POINT('.$lat."".$long.')';
$sql = "insert into `metrix` (`name`, `coord`) values ('".$name.", '".$location."')";
$result = mysqli_query($con,$sql);
if (empty($result)) {
Echo mysqli_error($con);}

并给出以下 MySql 语法错误:-

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'POINT(13,132)')' at line 1

正确的做法是什么?

谢谢

您在 ".$name.":

之后漏掉了一个引号
$sql = "insert into `metrix` (`name`, `coord`) values ('".$name."', ".$location.")";

并删除 ".$location." 周围的单引号,因为 POINT 是一个函数而不是字符串。

而且你应该使用准备好的语句。

location 中删除单引号,因为 POINT 是函数,并在名称前后放置单引号。

$sql = "insert into `metrix` (`name`, `coord`) values ('".$name."', ".$location.")";

去掉单引号因为Point是一个函数

$sql = "insert into `metrix` (`name`, `coord`) values ('".$name."', ".$location.")";

此外,我建议您使用 Prepared 语句,因为您当前的查询非常容易 SQL 注入。