It is required to enter data through a php script into the database table. To connect to the database, the connect.php file is used, to add data, add.php and the page where you can view the table with data is index.php. A database has been created on the server named database and a table in it named templog. There are no errors, but no data is added.
connect.php
<?php function Connection(){ $server="localhost"; $user="root"; $pass="123456"; $db="database"; $connection = mysqli_connect($server, $user, $pass); if (!$connection) { die('MySQL ERROR: ' . mysqli_error()); } mysqli_select_db($connection,$db ) or die( 'MySQL ERROR: '. mysqli_error() ); return $connection; }?> add.php
<?php include("connect.php"); $link=Connection(); $temp1=$_POST['temp1']; $hum1=$_POST['hum1']; $query = "INSERT INTO `templog` (`temperature`, `humidity`) VALUES ('".$temp1."','".$hum1."')"; mysqli_query($link,$query); mysqli_close($link); header("Location: index.php");?> index.php
<?php include("connect.php"); $link=Connection(); $result=mysqli_query($link,"SELECT * FROM `templog` ORDER BY `timeStamp` DESC"); ?> <html> <head> <title>Sensor Data</title> </head> <body> <h1>Temperature / moisture sensor readings</h1> <table border="1" cellspacing="1" cellpadding="1"> <tr> <td> Timestamp </td> <td> Temperature </td> <td> Moisture </td> </tr> <?php if($result!==FALSE){ while($row = mysqli_fetch_array($result)) { printf("<tr><td> %s </td><td> %s </td><td> %s </td></tr>", $row["timeStamp"], $row["temperature"], $row["humidity"]); } mysqli_free_result($result); mysqli_close($link); } ?> </table> </body> </html>