Jump to content

Recommended Posts

<?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 by Psycho
Formatted code content
Link to comment
https://forums.phpfreaks.com/topic/317547-what-is-wrong-with-the-code/
Share on other sites

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;

 

 

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

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.