0

I want latitude and longitude to be stored in MYSQL database. Latitude and Longitude has using datatype Decimal (8, 5). When I try to insert all the data get stored in MYSQL but the latitude and longitude is stored as 143.00000 rather then 143.99590. It doesn't store the value after the decimal point. Can any one help me with this?

I have created a file create.php and implemented items create functionality to insert new records into database. The API will accept HTTP POST values to create record. And then have created object of class Items.php and call method create() to save records.

Below is the create.php code

<?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."));
}
   ?>

Below is the Items.php code

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;
}
miken32
  • 42,008
  • 16
  • 111
  • 154
Priti Rathod
  • 117
  • 1
  • 10

0 Answers0