I am trying to test if my PHP script works by entering the values in add.php
directly via URL but the table displays only zeroes.
The Connect.php
is used when accessing the database. The Connect.php
code is the one used mainly for connection here it is, the add.php
process the POST from arduino.indexx.php
displays the values in a table:
Below is 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: indexx.php");
?>
Below is connect.php:
<?php
function Connection(){
$server="localhost";
$user="root";
$pass="";
$db="database";
$connection = mysqli_connect($server, $user, $pass);
//$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'user', 'pass');
if (!$connection) {
die('MySQL ERROR: ' . mysql_error());
}
mysqli_select_db($connection,$db) or die(mysqli_error($connection));
return $connection;
}
?>
indexx.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> Hall Sensor </td>
<td> Thermistor </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>