Home » Php » php – how to not send equal data to database?

php – how to not send equal data to database?

Posted by: admin February 25, 2020 Leave a comment

Questions:

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!

How to&Answers:

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