php - specify unique key for mysql table in the JSON object -
i implementing app creates json string below. app records json data , send in background server every 60 seconds. user can use app query server buses locations, result displayed in google map marker.
the data of json first inserted in bus table , updated insert into. in bus table not have unique key should add bus_id json string in app know record must updated asking myself whether possible generate , add json string before sending it. how can generate bus_id unique key in case?
json object:
{ "latitude":84.16898451, "longitude":3.16561387, "route":45 } php script.
<?php $json = '{"bus_id": "gernerated value", "latitude":84.16898451,"longitude":3.16561387,"route":45}'; $data = json_decode ( $json ); $bus_id = "it should generated in app"; $route = $data->{'route'}; $latitude = $data->{'latitude'}; $longitude = $data->{'longitude'}; require 'connection.php'; // check whether route's table exist. $results = $con->query ( "show tables 'bus' " ) or die ( mysqli_error () ); if (($results->num_rows) == 1) { //"update myguests set lastname='doe' id=2" $sql = "replace bus(bus_id, route, latitude, longitude) values( ?, ?, ? , ? )"; $stmt = $con->prepare($sql) or die ( $con->error ); $stmt->bind_param("ssss",$bus_id,$route, $latitude,$longitude); $stmt->execute(); $stmt->close(); echo "table exist"; } else { $create = "create table bus (id int(11) not null auto_increment primary key, bus_id int(11) not null unique, route int(11) not null, latitude float(10,6) not null , longitude float(10,6) not null, created_at timestamp default current_timestamp)" ; $stmt = $con->prepare($create) or die ( $con->error ); $stmt->execute(); $stmt->close(); echo "table created"; }
Comments
Post a Comment