首页 > 解决方案 > 使用带有 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;
}

标签: phpmysql

解决方案


推荐阅读