From 9929a1cf036a7acef5e0292b538ec2e80567da08 Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 17:29:05 +0100 Subject: [PATCH] Make the PHP Script better --- Sources/php_script/script/config.php | 13 +++++--- Sources/php_script/script/db.php | 9 ++---- Sources/php_script/script/distance.php | 4 +-- Sources/php_script/script/insert.php | 42 +++++++++++++++----------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Sources/php_script/script/config.php b/Sources/php_script/script/config.php index 3976c03..21d19bb 100644 --- a/Sources/php_script/script/config.php +++ b/Sources/php_script/script/config.php @@ -1,8 +1,13 @@ prepare($query); +$stm = $connect->prepare($query); $stm->execute(); $row = $stm->fetchAll(PDO::FETCH_ASSOC); $lat1=0; diff --git a/Sources/php_script/script/insert.php b/Sources/php_script/script/insert.php index dfae9ca..3ae92f5 100644 --- a/Sources/php_script/script/insert.php +++ b/Sources/php_script/script/insert.php @@ -1,20 +1,26 @@ 10;"; -$results = mysqli_query($connect, $query); -*/ -$query = "DELETE FROM gps WHERE id='$id';"; -$results = mysqli_query($connect, $query); - -$query = "INSERT INTO gps (id,latitude,longitude,idMusic,dateLog) VALUES('$id','$latitude','$longitude','$idMusic',CURRENT_TIMESTAMP);"; -$results = mysqli_query($connect, $query); +$connect=""; //Else PHP send an error, "connect don't exist", but that work anyway, this is just to remove a fake error +$res=include "config.php"; //$res get the result of the calling of "config.php" +if ($res != 1){ //Check if config.php work + print (json_encode("Failed to connect to MySQL")); //Return a json string, so the dart script can interpret the error +} +if (!empty($_POST)) { //Check if the method POST return something + $id = mysqli_real_escape_string($connect, $_POST['id']); //Get the result of POST method + $latitude = mysqli_real_escape_string($connect, $_POST['latitude']); //Get the result of POST method + $longitude = mysqli_real_escape_string($connect, $_POST['longitude']); //Get the result of POST method + $idMusic = mysqli_real_escape_string($connect, $_POST['idMusic']); //Get the result of POST method + $latitude = doubleval($latitude); //Convert a string to a double + $longitude = doubleval($longitude); //Convert a string to a double + /* + $query = "DELETE FROM gps WHERE (SELECT TIMESTAMPDIFF(MINUTE,CURRENT_TIMESTAMP,dateLog))>10;"; + $results = mysqli_query($connect, $query); + */ + $query = "DELETE FROM gps WHERE id='$id';"; //Delete the actual line and replace this line with the next lines + $results = mysqli_query($connect, $query); //Execute the SQL command + $query = "INSERT INTO gps (id,latitude,longitude,idMusic,dateLog) + VALUES('$id','$latitude','$longitude','$idMusic',CURRENT_TIMESTAMP);"; //Insert into the database the new data and new information about this user + $results = mysqli_query($connect, $query); //Execute the SQL command +}else{ //If the method POST return nothing + print (json_encode("The POST method failed")); //Return a json string, so the dart script can interpret the error +} \ No newline at end of file