I am trying to delete the last record in the table before update it but with the current state, a new one is being inserted if there are some changes in the following fiedls route
, langitude
, latitude
. This is a database for bus tracker app where the ride can input the desired route to get it's location displayed in the google map.
I appreciate any help.
<?php
$json = '{"latitude":74.16898451,"longitude":18.16561387,"route":13}';
$data = json_decode ( $json );
$route = $data->{'route'};
$latitude = $data->{'latitude'};
$longitude = $data->{'longitude'};
require 'connection.php';
// check whether route's table exist.
$results = $con->query ( "SHOW TABLES like 'bus' " ) or die ( mysqli_error () );
if (($results->num_rows) == 1) {
//"UPDATE MyGuests SET lastname='Doe' WHERE id=2"
$sql = "REPLACE INTO bus(route, latitude, longitude)
VALUES( ?, ? , ? )";
$stmt = $con->prepare($sql) or die ( $con->error );
$stmt->bind_param("sss",$route, $latitude,$longitude);
$stmt->execute();
$stmt->close();
echo "Table exist";
} else {
$create = "CREATE TABLE bus
(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
route int(11) NOT NULL UNIQUE,
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 was created";
}