Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. add.php
  2. ------------------------------------------------------------------------------------------------------------------------------------------
  3. <?php
  4. $server="localhost";
  5. $user="root";
  6. $pass="";
  7. $db="arduino";
  8. ?>
  9.  
  10. <html>
  11. <head>
  12. <title>Test for uploading data</title>
  13. </head>
  14. <body>
  15.  
  16. <?php
  17. if(isset($_POST['temp1']))
  18. {
  19.   $temp1=$_POST['temp1'];
  20.   $hum1=$_POST['hum1'];
  21.   $link=mysqli_connect($server, $user, $pass );
  22.   if(!$link)
  23.     {echo mysql_error();
  24.      echo "<br><br>";
  25.     }
  26.  
  27.   if(!mysqli_select_db($link,"arduino"))
  28.     {echo mysql_error();
  29.      echo "<br><br>";
  30.     }
  31.  
  32.  
  33. $query = "INSERT INTO templog(temperature,humidity) VALUES ($temp1,$hum1)";
  34.  
  35.   if(!mysqli_query($link,$query))
  36.     {echo mysql_error();
  37.      echo "<br><br>";
  38.     }
  39.  
  40. //  mysql_close($link);
  41. }
  42.  
  43. ?>
  44.  
  45. <form action="add.php" method="POST">
  46. value for temp1<br>
  47. <input type="text" name="temp1"><br><br>
  48. <input type="text" name="hum1"><br><br>
  49. <input type="submit"><br>
  50. </form>
  51. </body>
  52. </html>
  53. ------------------------------------------------------------------------------------------------------------------------------------------
  54. index.php
  55. <?php
  56.  
  57.     include("connect.php");
  58.  
  59.     $link=Connection();
  60.  
  61.     $result=mysql_query('SELECT * FROM tempLog ORDER BY timeStamp'); //DESC",$link);
  62. ?>
  63.  
  64. <html>
  65.    <head>
  66.       <title>Sensor Data</title>
  67.    </head>
  68. <body>
  69.    <h1>Temperature / moisture sensor readings</h1>
  70.  
  71.    <table border="1" cellspacing="1" cellpadding="1">
  72.         <tr>
  73.             <td>&nbsp;Timestamp&nbsp;</td>
  74.             <td>&nbsp;Temperature 1&nbsp;</td>
  75.             <td>&nbsp;Moisture 1&nbsp;</td>
  76.         </tr>
  77.  
  78.       <?php
  79.           if($result!==FALSE){
  80.              while($row = mysql_fetch_array($result)) {
  81.                 printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>",
  82.                    $row["timeStamp"], $row["temperature"], $row["humidity"]);
  83.              }
  84.              mysql_free_result($result);
  85.             // mysql_close();
  86.           }
  87.       ?>
  88.  
  89.    </table>
  90. </body>
  91. </html>
  92. ----------------------------------------------------------------------------------------------------------------------------------------
  93. connect.php
  94. <?php
  95.  
  96.     function Connection(){
  97.         $server="localhost";
  98.         $user="root";
  99.         $pass="";
  100.         $db="arduino";
  101.  
  102.         $connection = mysql_connect($server, $user, $pass);
  103.  
  104.         if (!$connection) {
  105.             die('MySQL ERROR: ' . mysql_error());
  106.         }
  107.  
  108.         mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );
  109.  
  110.         return $connection;
  111.     }
  112. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement