kiril86 Posted December 17, 2023 Share Posted December 17, 2023 (edited) <?php $servername = "localhost"; $username = "xxx"; $password = "xxx"; $dbname = "energy"; date_default_timezone_set("Europe/Sofia"); $h = date('H'); $m = date('i'); $month = date('n'); //$year = date('Y'); // Create connection // $conn = mysqli_connect($servername, $username, $password, $dbname); $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } switch ($month) { case 12: { if ($h >= 06 && $m >= 00) { $sql = "INSERT INTO test2 (resultat, current ) VALUES (100 , 50)"; } } break; } $result = $conn->query($sql); if ($result == true) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> Edited December 19, 2023 by Psycho Formatted code content Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/ Share on other sites More sharing options...
Moorcam Posted December 17, 2023 Share Posted December 17, 2023 What errors are you getting, if any? Also, use the code tags when posting to make it easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613611 Share on other sites More sharing options...
kiril86 Posted December 17, 2023 Author Share Posted December 17, 2023 I don't get any mistakes- the code is not acting automatically, but only when i access it through "Localhost". Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613612 Share on other sites More sharing options...
requinix Posted December 17, 2023 Share Posted December 17, 2023 43 minutes ago, kiril86 said: but only when i access it through "Localhost". ...how else are you accessing it? Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613613 Share on other sites More sharing options...
Barand Posted December 17, 2023 Share Posted December 17, 2023 47 minutes ago, kiril86 said: I don't get any mistakes Then try running it when it isn't December or before 6am. Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613614 Share on other sites More sharing options...
kiril86 Posted December 17, 2023 Author Share Posted December 17, 2023 I dont't want to access it at all- wihtout accessing it through localhost i want automatically to post the data at the database. Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613615 Share on other sites More sharing options...
kiril86 Posted December 17, 2023 Author Share Posted December 17, 2023 It's working like that: $servername = "localhost"; $username = "xxx"; $password = "xxx"; $dbname = "energy"; date_default_timezone_set("Europe/Sofia"); $h = date('H'); $m = date('i'); $month = date('n'); //$year = date('Y'); $conn = mysqli_connect($dbservername, $dbusername, $dbpassword, $dbname); $api_key_value = "tPmAT5Ab3j7F9"; $api_key = $voltage = $current = $frequency = $power = $energy = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $api_key = ($_POST["api_key"]); if($api_key == $api_key_value) { $voltage = ($_POST["voltage"]); $current = ($_POST["current"]); $frequency = ($_POST["frequency"]); $power = ($_POST["power"]); $energy = ($_POST["energy"]); // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } switch($month) { case 12:{ if (($h >= 06 && $m >=00 ) && ($h <= 21 && $m <=59)) { $sql = "INSERT INTO energy_home_January (voltage,current,frequency,power,energy) VALUES ('" . $voltage . "', '" . $current . "', '" . $frequency . "', '" . $power . "', '" . $energy . "')"; } else{ $sql = "INSERT INTO energy_home_January_night (voltage,current,frequency,power,energy) VALUES ('" . $voltage . "', '" . $current . "', '" . $frequency . "', '" . $power . "', '" . $energy . "')"; } } break; Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613616 Share on other sites More sharing options...
kiril86 Posted December 17, 2023 Author Share Posted December 17, 2023 But I want to use this. INSERT INTO test3( id, voltage, current, frequency, power, energy, reading_time, resultat ) SELECT id, voltage, current, frequency, power, energy, reading_time, energy - LAG(energy) OVER(ORDER BY id) AS temp FROM energy_home_november Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613617 Share on other sites More sharing options...
Moorcam Posted December 17, 2023 Share Posted December 17, 2023 Insert Automatically? I think you need to learn about php and MySQL etc. Here's a good place to start - https://www.phptutorial.net/php-pdo/php-pdo-insert/ Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613618 Share on other sites More sharing options...
Barand Posted December 17, 2023 Share Posted December 17, 2023 24 minutes ago, kiril86 said: I dont't want to access it at all- wihtout accessing it through localhost i want automatically to post the data at the database. Write it as a CLI script and use a CRON job to run it as frequently as required Quote Link to comment https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/#findComment-1613619 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.