commit fd840aa8946205779e247f8dacb42cd16e59c446
parent 3db2c144d97d374e0f884af91710ed650c3b3208
Author: Kninteman <[email protected]>
Date: Sat, 25 Mar 2023 23:24:55 +0100
Gebeurtenissen opgesplitst
Datainserter.php:
Ik heb de gebeurtenissen ($frshtt) opgesplitst, regel 61-68 en vervolgens hierdoor regels: 52,53,70 aangepast.
Datainserter & Datavalidator.php:
Ik heb $conn veranderd in $db_connection om de leesbaarheid te verhogen.
Database:
De kolommen vorst, regen, sneeuw, hagel, onweer, tornado toegevoegd en gevuld aan de hand van de aanwezige data in de database.
Diffstat:
2 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/datainserter.php b/datainserter.php
@@ -6,10 +6,10 @@ $password = "hailiwa";
$dbname = "wap2";
// Create connection
-$conn = mysqli_connect($servername, $username, $password, $dbname);
+$db_connection = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
-if (!$conn) {
+if (!$db_connection) {
die("Connection failed: " . mysqli_connect_error());
}
@@ -45,20 +45,29 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$wnddir = $weather_obj['WNDDIR'];
// Validate temperature
- $valid = validate_temperature($stn, $temp, $conn);
+ $valid = validate_temperature($stn, $temp, $db_connection);
// Prepare SQL INSERT-statement
$sql = "INSERT INTO weerdata (station_name,datum_tijd,gevalideerd,temp,dauwpunt,ldrk_znv,ldrk_station,zichtbaarheid,
- windsnelheid,neerslag,sneeuwdpt,gebeurtenissen,bewolking,windrichting) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
+ windsnelheid,neerslag,sneeuwdpt,gebeurtenissen, vorst,regen,sneeuw,hagel,onweer,tornado, bewolking,windrichting)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
// Create prepared statement with the SQL statement
- $stmt = mysqli_prepare($conn, $sql);
+ $stmt = mysqli_prepare($db_connection, $sql);
// Concatenate date and time
$datetime = $date . " " . $time;
+ // Extract the boolean values from $frshtt
+ $vorst = (int) substr($frshtt, 0, 1);
+ $regen = (int) substr($frshtt, 1, 1);
+ $sneeuw = (int) substr($frshtt, 2, 1);
+ $hagel = (int) substr($frshtt, 3, 1);
+ $onweer = (int) substr($frshtt, 4, 1);
+ $tornado = (int) substr($frshtt, 5, 1);
+
// Bind parameters to prepared statement
- mysqli_stmt_bind_param($stmt, "isiddddddddsdi", $stn, $datetime, $valid, $temp, $dewp, $stp, $slp, $visib, $wdsp, $prcp, $sndp, $frshtt, $cldc, $wnddir);
+ mysqli_stmt_bind_param($stmt, "isiddddddddsiiiiiidi", $stn, $datetime, $valid, $temp, $dewp, $stp, $slp, $visib, $wdsp, $prcp, $sndp, $frshtt, $vorst,$regen,$sneeuw,$hagel,$onweer,$tornado, $cldc, $wnddir);
// Execute prepared statement
$result = mysqli_stmt_execute($stmt);
@@ -67,11 +76,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($result) {
echo "Data inserted successfully";
} else {
- echo "Error inserting data: " . mysqli_error($conn);
+ echo "Error inserting data: " . mysqli_error($db_connection);
}
}
}
// Close the database connection
-mysqli_close($conn);
+mysqli_close($db_connection);
?>
diff --git a/datavalidator.php b/datavalidator.php
@@ -7,13 +7,13 @@
* @param mysqli $conn The mysqli connection object.
* @return int Returns 1 if the temperature is within 20% difference from the average temperature, or 0 if it is not.
*/
-function validate_temperature($station, $temperatuur, $conn) {
+function validate_temperature($station, $temperatuur, $db_connection) {
// Retrieve the average of the last 30 rows for the given station name
$sql = "SELECT AVG(temp) AS avg_temp FROM (SELECT temp FROM weerdata
WHERE station_name = ?
ORDER BY datum_tijd
DESC LIMIT 30) AS last_30_rows";
- $stmt = $conn->prepare($sql);
+ $stmt = $db_connection->prepare($sql);
$stmt->bind_param('s', $station);
$stmt->execute();
$result = $stmt->get_result();