Advertisement
MdSadmanSiraj

index.php

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