Advertisement
MdSadmanSiraj

set_actions.php

Jul 27th, 2022 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.77 KB | None | 0 0
  1. <?php include "../inc/dbinfo.inc"; ?>
  2. <html>
  3. <body>
  4. <h1>ECE 531: Final Project<h1>
  5. <p>Database on Heater Status & Actions</p>
  6. <?php
  7.  
  8.   /* Connect to MySQL and select the database. */
  9.   $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
  10.  
  11.   if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
  12.  
  13.   $database = mysqli_select_db($connection, DB_DATABASE);
  14.  
  15.   /* Ensure that the SETACTIONS table exists. */
  16.   VerifySetActionsTable($connection, DB_DATABASE);
  17.  
  18.   /* If input fields are populated, read the inputs. */
  19.   $http_request = htmlentities($_POST['REQUEST']);
  20.   $sa_id = htmlentities($_POST['ID']);
  21.   $sa_timestamp = htmlentities($_POST['TIMESTAMP']);
  22.   $sa_temperature = htmlentities($_POST['TEMPERATURE']);
  23.   $sa_set_points = htmlentities($_POST['SET_POINTS']);
  24.   $sa_current_status = htmlentities($_POST['CURRENT_STATUS']);
  25.   $sa_action = htmlentities($_POST['ACTION']);
  26.  
  27.   /* No HTTP request error messages */
  28.   if (!strlen($http_request) && (strlen($sa_id) || strlen($sa_timestamp) || strlen($sa_temperature) || strlen($sa_set_points) || strlen($sa_current_status) || strlen($sa_action))) {
  29.         NoRequest();
  30.   }
  31.  
  32.  /* PUT Request = Add Record */
  33.   if ($http_request == "PUT") {
  34.         if (!strlen($sa_id) && (strlen($sa_timestamp) || strlen($sa_temperature) || strlen($sa_set_points) || strlen($sa_current_status) || strlen($sa_action))) {
  35.             if (!strlen($sa_timestamp)) $sa_timestamp = " ";
  36.             if (!strlen($sa_temperature)) $sa_temperature = " ";
  37.         if (!strlen($sa_set_points)) $sa_set_points = " ";
  38.             if (!strlen($sa_current_status)) $sa_current_status = " ";
  39.         if (!strlen($sa_action)) $sa_action = " ";
  40.         AddRecord($connection, $sa_timestamp, $sa_temperature, $sa_set_points, $sa_current_status, $sa_action);
  41.         }
  42.         else {
  43.             InvalidRequest($connection, $http_request);
  44.         }
  45.   }
  46.  
  47.   /* POST Request = Update Record */
  48.   if ($http_request == "POST") {
  49.         if (strlen($sa_id) && (strlen($sa_timestamp) || strlen($sa_temperature) || strlen($sa_set_points) || strlen($sa_current_status) || strlen($sa_action))) {
  50.             $result = mysqli_query($connection, "SELECT * FROM SETACTIONS WHERE ID = '$sa_id';");
  51.             $query_data = mysqli_fetch_row($result);
  52.             if (!strlen($sa_timestamp)) {
  53.               $sa_timestamp = $query_data[1];
  54.             }
  55.             elseif (!strlen($sa_temperature)) {
  56.               $sa_temperature = $query_data[2];
  57.             }
  58.         elseif (!strlen($sa_set_points)) {
  59.               $sa_set_points = $query_data[3];
  60.             }
  61.             elseif (!strlen($sa_current_status)) {
  62.               $sa_current_status = $query_data[4];
  63.             }
  64.         elseif (!strlen($sa_action)) {
  65.               $sa_action = $query_data[5];
  66.             }
  67.             UpdateRecord($connection, $sa_id, $sa_timestamp, $sa_temperature, $sa_set_points, $sa_current_status, $sa_action);
  68.         }
  69.         else {
  70.             InvalidRequest($connection, $http_request);
  71.         }
  72.   }
  73.    
  74.   /* GET Request = Retrieve Record */
  75.   if ($http_request == "GET") {
  76.         if (strlen($sa_id) && !strlen($sa_timestamp) && !strlen($sa_temperature) && !strlen($sa_set_points) && !strlen($sa_current_status) && !strlen($sa_action)) {
  77.             GetRecord($connection, $sa_id);
  78.         }
  79.         else {
  80.             InvalidRequest($connection, $http_request);
  81.         }
  82.   }
  83.  
  84.   /* DELETE Request = Delete Record */
  85.   if ($http_request == "DELETE") {
  86.         if (strlen($sa_id) && !strlen($sa_timestamp) && !strlen($sa_temperature) && !strlen($sa_set_points) && !strlen($sa_current_status) && !strlen($sa_action)) {
  87.             DeleteRecord($connection, $sa_id);
  88.         }
  89.         else {
  90.             InvalidRequest($connection, $http_request);
  91.         }
  92.   }
  93.  
  94. ?>
  95.  
  96. <!-- Display table data. -->
  97. <table border="1" cellpadding="2" cellspacing="2">
  98.   <tr>
  99.     <td>ID</td>
  100.     <td>TIMESTAMP</td>
  101.     <td>TEMPERATURE</td>
  102.     <td>SET_POINTS</td>
  103.     <td>CURRENT_STATUS</td>
  104.     <td>ACTION</td>
  105.   </tr>
  106.  
  107. <?php
  108.  
  109. $result = mysqli_query($connection, "SELECT * FROM SETACTIONS");
  110.  
  111. while($query_data = mysqli_fetch_row($result)) {
  112.   echo "<tr>";
  113.   echo "<td>", $query_data[0], "</td>",
  114.        "<td>", $query_data[1], "</td>",
  115.        "<td>", $query_data[2], "</td>",
  116.        "<td>", $query_data[3], "</td>",
  117.        "<td>", $query_data[4], "</td>",
  118.        "<td>", $query_data[5], "</td>";
  119.   echo "</tr>";
  120. }
  121.  
  122. ?>
  123.  
  124. </table>
  125.  
  126. <?php
  127.   /* Clean up */
  128.   mysqli_free_result($result);
  129.   mysqli_close($connection);
  130. ?>
  131.  
  132. </body>
  133. </html>
  134.  
  135. <?php
  136.  
  137. /* No HTTP request error messages */
  138. function NoRequest() {
  139.    echo "\nNo HTTP Request Recived. Please refer to the API description for more details.\n";
  140. }
  141.  
  142. /* Add a record to the table. */
  143. function AddRecord($connection, $timestamp, $temperature, $set_points, $current_status, $action) {
  144.    $time = mysqli_real_escape_string($connection, $timestamp);
  145.    $temp = mysqli_real_escape_string($connection, $temperature);
  146.    $spts = mysqli_real_escape_string($connection, $set_points);
  147.    $csts = mysqli_real_escape_string($connection, $current_status);
  148.    $actn = mysqli_real_escape_string($connection, $action);
  149.  
  150.    $query = "INSERT INTO SETACTIONS (TIMESTAMP, TEMPERATURE, SET_POINTS, CURRENT_STATUS, ACTION) VALUES ('$time', '$temp', '$spts', '$csts', '$actn');";
  151.  
  152.    if(!mysqli_query($connection, $query)) echo("<p>Error adding record.</p>");
  153. }
  154.  
  155. /* Update a record to the table. */
  156. function UpdateRecord($connection, $id, $timestamp, $temperature, $set_points, $current_status, $action) {
  157.    $i = mysqli_real_escape_string($connection, $id);
  158.    $time = mysqli_real_escape_string($connection, $timestamp);
  159.    $temp = mysqli_real_escape_string($connection, $temperature);
  160.    $spts = mysqli_real_escape_string($connection, $set_points);
  161.    $csts = mysqli_real_escape_string($connection, $current_status);
  162.    $actn = mysqli_real_escape_string($connection, $action);
  163.  
  164.    $query = "UPDATE SETACTIONS SET ID = '$i', TIMESTAMP = '$time', TEMPERATURE = '$temp', SET_POINTS = '$spts', CURRENT_STATUS = '$csts', ACTION = '$actn' WHERE ID = '$i';";
  165.  
  166.    if(!mysqli_query($connection, $query)) echo("<p>Error updating record.</p>");
  167. }
  168.  
  169. /* Retrieve a record from the table. */
  170. function GetRecord($connection, $id) {
  171.    $i = mysqli_real_escape_string($connection, $id);
  172.  
  173.    $query = "SELECT * FROM SETACTIONS WHERE ID = '$i';";
  174.    $result = mysqli_query($connection, $query);
  175.    $query_data = mysqli_fetch_row($result);
  176.    if(!empty($query_data)) {
  177.        echo "\nHTTP GET Response: Retrieving record with ID = $id\n";
  178.        echo "ID | TIMESTAMP | TEMPERATURE | SET_POINTS | CURRENT_STATUS | ACTION\n";
  179.        echo "$query_data[0] | $query_data[1] | $query_data[2] | $query_data[3] | $query_data[4] | $query_data[5]\n";
  180.    }
  181.    else {
  182.        echo "\nHTTP GET Response: The given ID does not exist in the database.\n";
  183.    }
  184.  
  185.    if(!mysqli_query($connection, $query)) echo("<p>Error getting record.</p>");
  186. }
  187.  
  188. /* Delete a record from the table. */
  189. function DeleteRecord($connection, $id) {
  190.    $i = mysqli_real_escape_string($connection, $id);
  191.  
  192.    $query = "SELECT * FROM SETACTIONS WHERE ID = '$i';";
  193.    $result = mysqli_query($connection, $query);
  194.    $query_data = mysqli_fetch_row($result);
  195.    if(!empty($query_data)) {
  196.        $query = "DELETE FROM SETACTIONS WHERE ID = '$i';";
  197.    }
  198.    else {
  199.        echo "\nHTTP DELETE Response: The given ID does not exist in the database.\n";
  200.    }
  201.  
  202.    if(!mysqli_query($connection, $query)) echo("<p>Error deleting record.</p>");
  203. }
  204.  
  205. /* Invalid input error messages */
  206. function InvalidRequest($connection, $request) {
  207.    $r = mysqli_real_escape_string($connection, $request);
  208.  
  209.    echo("\nCannot perform HTTP $r request: Invalid input.\n");
  210.    echo("\nPlease refer to the API description for more details.\n");
  211. }
  212.  
  213. /* Check whether the table exists and, if not, create it. */
  214. function VerifySetActionsTable($connection, $dbName) {
  215.   if(!TableExists("SETACTIONS", $connection, $dbName))
  216.   {
  217.      $query = "CREATE TABLE SETACTIONS (
  218.         ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  219.         TIMESTAMP VARCHAR(45),
  220.         TEMPERATURE VARCHAR(90),
  221.        SET_POINTS VARCHAR(90),
  222.         CURRENT_STATUS VARCHAR(90),
  223.        ACTION VARCHAR(90)
  224.       )";
  225.  
  226.      if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");
  227.   }
  228. }
  229.  
  230. /* Check for the existence of a table. */
  231. function TableExists($tableName, $connection, $dbName) {
  232.   $t = mysqli_real_escape_string($connection, $tableName);
  233.   $d = mysqli_real_escape_string($connection, $dbName);
  234.  
  235.   $checktable = mysqli_query($connection,
  236.       "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");
  237.  
  238.   if(mysqli_num_rows($checktable) > 0) return true;
  239.  
  240.   return false;
  241. }
  242. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement