Advertisement
MdSadmanSiraj

set_points.php

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