-1

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.phpis 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>&nbsp;Timestamp&nbsp;</td>
            <td>&nbsp;Hall Sensor&nbsp;</td>
            <td>&nbsp;Thermistor&nbsp;</td>
        </tr>

      <?php 
          if($result!==FALSE){
             while($row = mysqli_fetch_array($result)) {
                printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
                   $row["timeStamp"], $row["temperature"], $row["humidity"]);
             }
             mysqli_free_result($result);
             mysqli_close($link);
          }
      ?>

   </table>
</body>
</html>
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Moloi Mokete
  • 31
  • 1
  • 3

1 Answers1

0

The problem is that when you 'enter the values via the url', they'll end up in $_GET instead of $_POST. Try changing those lines in add.php.

Kris
  • 2,108
  • 18
  • 19