-2

This code:

<?php
  include_once 'dbh.inc.php';
  include_once 'variables.inc.php';


  $sql = "INSERT INTO status (temp, weather, precipitation, vindtype, WindSpeed, WindDirection) VALUES ('$temp*C', '$weather', '$precipitation  mm', '$Windtype', '$Windspeed m/s', '$Winddirection');";
  mysqli_query($conn, $sql);

?>

sends the current weather report to the database. MS Task scheduler does this process automatically every 10 minutes.

I want to only send the data only if any variable has changed. So basically: Check every 10 min if something has changed, if yes then send to the database, if not don't send.

Any help is greatly appreciated!

andreasv
  • 450
  • 1
  • 4
  • 15

1 Answers1

0

You can check if the same values are not already in the database before you insert. Like this:

<?php
  include_once 'dbh.inc.php';
  include_once 'variables.inc.php';

  $sql = "if not exists(select * from status where temp = '$temp*C' and weather = '$weather' and precipitation = '$precipitation  mm' and vindtype = '$Windtype' and WindSpeed = '$Windspeed m/s' and WindDirection = '$Winddirection')
begin
INSERT INTO status (temp, weather, precipitation, vindtype, WindSpeed, WindDirection) VALUES ('$temp*C', '$weather', '$precipitation  mm', '$Windtype', '$Windspeed m/s', '$Winddirection');
end;";
  mysqli_query($conn, $sql);

?>

Note I have not tested this code, but this should work for you

Cujoey
  • 147
  • 6
  • not sure if this is going to work. I am very new to coding, but the colors are all green in my coding program. – andreasv Feb 15 '20 at 17:32
  • This code should work for you. `if not exists` will check if the values are not already in the database before you do the insert. – Cujoey Feb 15 '20 at 17:40
  • i don't want to check if the date is there from before only if it is equal to the last data that was inserted. – andreasv Feb 15 '20 at 18:25
  • Can you share more details about the `status` table? If the `status` table have an `Autoincrement` ID column, you can get ID of the last data inserted by using `select max(ID) from status` which you can then use to get data for that ID and compare with the new Data... – Cujoey Feb 15 '20 at 19:48
  • yes, the table does have an `autoincrement` ID. I probably need to do something like `$variable = "select max(ID) from status"` but the how could I compare and? and how to not send if it is equal? – andreasv Feb 15 '20 at 20:06