php - 使用带有 PHP 的简单 REST API 在我的 SQL 中存储纬度和经度时面临的问题
问题描述
我希望将纬度和经度存储在 MYSQL 数据库中。纬度和经度使用数据类型 Decimal (8, 5)。当我尝试插入存储在 MYSQL 中的所有数据时,但纬度和经度存储为 143.00000 而不是 143.99590。它不存储小数点后的值。谁能帮我这个?
我创建了一个文件 create.php 并实现了项目创建功能以将新记录插入数据库。API 将接受 HTTP POST 值来创建记录。然后创建了 Items.php 类的对象并调用方法 create() 来保存记录。
下面是 create.php 代码
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once '../config/Database.php';
include_once '../class/Items.php';
$database = new Database();
$db = $database->getConnection();
$items = new Items($db);
$data = json_decode(file_get_contents("php://input"));
if(!empty($data->name) && !empty($data->comment) &&
!empty($data->latitude) && !empty($data->longitude) &&
!empty($data->created)){
$items->name = $data->name;
$items->comment = $data->comment;
$items->latitude = $data->latitude;
$items->longitude = $data->longitude;
$items->created = date('Y-m-d H:i:s');
if($items->create()){
http_response_code(201);
echo json_encode(array("message" => "Item was created."));
} else{
http_response_code(503);
echo json_encode(array("message" => "Unable to create item."));
}
}else{
http_response_code(400);
echo json_encode(array("message" => "Unable to create item. Data is incomplete."));
}
?>
下面是 Items.php 代码
function create(){
$stmt = $this->conn->prepare("
INSERT INTO ".$this->itemsTable."(`name`, `comment`, `latitude`, `longitude`, `created`)
VALUES(?,?,?,?,?)");
$this->name = htmlspecialchars(strip_tags($this->name));
$this->comment = htmlspecialchars(strip_tags($this->comment));
$this->latitude = htmlspecialchars(strip_tags($this->latitude));
$this->longitude = htmlspecialchars(strip_tags($this->longitude));
$this->created = htmlspecialchars(strip_tags($this->created));
$stmt->bind_param("ssiis", $this->name, $this->comment, $this->latitude, $this->longitude, $this->created);
if($stmt->execute()){
return true;
}
return false;
}
解决方案
推荐阅读
- javascript - 如何从带有属性的javascript运行外部javascript并获取对象
- bash - 睡眠的资源利用
- r - 通过选择 shapefile (.shp) 作为文件输入,在 R Markdown 中使用参数编织
- angular - Angular 将路由内容作为主元素的同级元素
元素 - c# - 试图理解MVC的控制器
- javascript - 使用布尔值和对象检查 AND 条件。检查对象是否已定义
- python - Python:如何在 pygame 中使绘制的元素对齐网格
- python - 将逗号转换为破折号 Python
- flutter - 在颤动的两个不相关的屏幕之间传递数据
- list - 计算列表列表中有多少元素满足谓词