CHAPTER 9 ■ IOT PATTERNS: LOCATION AWARE
itude=41.83 &longitude=-87.68. Your PHP code will need to extract client ID and GPS
coordinates from the query string using $_GET['parameterName'] statement.
Now you need to store these GPS coordinates in the database table, either in an
existing row or by inserting them in a new row. Prepare an INSERT OR UPDATE SQL
statement in the $sql variable. You will need to pass the CLIENT_ID , LATITUDE , and
LONGITUDE values in the SQL query while TIMESTAMP will be auto-generated by the
database.
Finally, execute the INSERT OR UPDATE SQL statement using $mysqli->query($sql)
and check the $result variable for success or failure.
Listing 9-3. Code to Receive and Add/Update Data in update.php
<?php
include('util-dbconn.php');
$clientID = $_GET['clientID'];
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
$sql = "INSERT INTO GPS_TRACKER_DATA
(CLIENT_ID, LATITUDE, LONGITUDE)
VALUES('$clientID', $latitude, $longitude) ";
$sql = $sql. "ON DUPLICATE KEY UPDATE CLIENT_ID='$clientID',
LATITUDE=$latitude, LONGITUDE=$longitude";
echo $sql;
if (!$result = $mysqli->query($sql))
{
echo "[Error] ". mysqli_error(). "\n";
exit();
}
$mysqli->close();
echo "[DEBUG] Updated GPS Coordinates Successfully\n";
?>