Buonasera, premetto di essere un principiante del php. Sto cercando di far funzionare degli sript php trovati su un tutorial per passare dei valori ad una scheda Nodemcu attraverso una pagina php ed un database mysql.
Questo è il codice php:
Nome file: index.php
<?php
require_once("dbIoT.php");
if(isset($_POST['ledOn']))$sql="update led set led_status='1' where led_id='1'";
elseif(isset($_POST['ledOff']))$sql="update led set led_status='2' where led_id='1'";
if(isset($_POST['ledOn']) || isset($_POST['ledOff']))$result=$conn->query($sql);
?>
<html>
<head>
<title>Class Connect IoT Dashboard </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="
https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="jumbotron"><h1>ClassConnect - IoT Application </h1></div>
<div class="container">
<h3 align="center">Controlling LED</h3>
<form action="led_update.php" method="post">
<div class="card-group">
<div class="card bg-secondary text-white">
<div class="card-body">
<input type="submit" style="font-size: 50;" class="btn btn-secondary btn-block" name="ledOn" value="Switch On LED"/>
</div>
</div>
<div class="card bg-info text-white">
<div class="card-body">
<input type="submit" style="font-size: 50;" class="btn btn-info btn-block" name="ledOff" value="Switch Off LED"/>
</div>
</div>
</div>
</form>
</div>
-----------------------------
Nome file: ledstatus.php
<?php
//Creating an array with name 'data' for JSON response
$data = array();
require_once("dbIoT.php");
if(isset($_GET['id']))
{
$id=$_GET['id'];
$sql="SELECT * FROM led where led_id='$id'";
$result=$conn->query($sql);
if($result->num_rows==0)
{
$data["success"] = 0;
$data["message"] = "No data on led found.";
echo json_encode($data);
}
else
{
$row=$result->fetch_assoc();
$data["ID"] = $row["led_id"];
$data["STATUS"] = $row["led_status"];
echo json_encode($data); // Show JSON response
}
}
else
{
$data["success"] = 0;
$data["message"] = $conn->error;
echo json_encode($data);
}
?>
-----------------------
Nome file: dbIoT.php
<?php
$servername = "localhost"; //Host Name
$username = "********"; // Database User Name
$password = "*********"; // Database User Password
$db="********"; // Database Name
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection and terminate the code if failed
if($conn->connect_error)die("Connection failed: " . $conn->connect_error);
?>
-----------------------
Qualcuno potrebbe gentilmente darmi una mano prima chi io impazzisca?
La pagina php non aggiona i dati sul db. Come mai?
Grazie