Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php include "../inc/dbinfo.inc"; ?>
- <html>
- <body>
- <h1>ECE 531: Communication in the Cloud</h1>
- <p>Database on Student information with Name and Address</p>
- <?php
- /* Connect to MySQL and select the database. */
- $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
- if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
- $database = mysqli_select_db($connection, DB_DATABASE);
- /* Ensure that the STUDENTS table exists. */
- VerifyStudentsTable($connection, DB_DATABASE);
- /* If input fields are populated, read the inputs. */
- $http_request = htmlentities($_POST['REQUEST']);
- $student_id = htmlentities($_POST['ID']);
- $student_name = htmlentities($_POST['NAME']);
- $student_address = htmlentities($_POST['ADDRESS']);
- /* No HTTP request error messages */
- if (!strlen($http_request) && (strlen($student_id) || strlen($student_name) || strlen($student_address))) {
- NoRequest();
- }
- /* PUT Request = Add Record */
- if ($http_request == "PUT") {
- if (!strlen($student_id) && (strlen($student_name) || strlen($student_address))) {
- if (!strlen($student_name)) $student_name = " ";
- if (!strlen($student_address)) $student_address = " ";
- AddRecord($connection, $student_name, $student_address);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* POST Request = Update Record */
- if ($http_request == "POST") {
- if (strlen($student_id) && (strlen($student_name) || strlen($student_address))) {
- $result = mysqli_query($connection, "SELECT * FROM STUDENTS WHERE ID = '$student_id';");
- $query_data = mysqli_fetch_row($result);
- if (!strlen($student_name)) {
- $student_name = $query_data[1];
- }
- elseif (!strlen($student_address)) {
- $student_address = $query_data[2];
- }
- UpdateRecord($connection, $student_id, $student_name, $student_address);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* GET Request = Retrieve Record */
- if ($http_request == "GET") {
- if (strlen($student_id) && !strlen($student_name) && !strlen($student_address)) {
- GetRecord($connection, $student_id);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* DELETE Request = Delete Record */
- if ($http_request == "DELETE") {
- if (strlen($student_id) && !strlen($student_name) && !strlen($student_address)) {
- DeleteRecord($connection, $student_id);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- ?>
- <!-- Display table data. -->
- <table border="1" cellpadding="2" cellspacing="2">
- <tr>
- <td>ID</td>
- <td>NAME</td>
- <td>ADDRESS</td>
- </tr>
- <?php
- $result = mysqli_query($connection, "SELECT * FROM STUDENTS");
- while($query_data = mysqli_fetch_row($result)) {
- echo "<tr>";
- echo "<td>", $query_data[0], "</td>",
- "<td>", $query_data[1], "</td>",
- "<td>", $query_data[2], "</td>";
- echo "</tr>";
- }
- ?>
- </table>
- <?php
- /* Clean up */
- mysqli_free_result($result);
- mysqli_close($connection);
- ?>
- </body>
- </html>
- <?php
- /* No HTTP request error messages */
- function NoRequest() {
- echo "\nNo HTTP Request Recived. Please refer to the API description for more details.\n";
- }
- /* Add a record to the table. */
- function AddRecord($connection, $name, $address) {
- $n = mysqli_real_escape_string($connection, $name);
- $a = mysqli_real_escape_string($connection, $address);
- $query = "INSERT INTO STUDENTS (NAME, ADDRESS) VALUES ('$n', '$a');";
- if(!mysqli_query($connection, $query)) echo("<p>Error adding student data.</p>");
- }
- /* Update a record to the table. */
- function UpdateRecord($connection, $id, $name, $address) {
- $i = mysqli_real_escape_string($connection, $id);
- $n = mysqli_real_escape_string($connection, $name);
- $a = mysqli_real_escape_string($connection, $address);
- $query = "UPDATE STUDENTS SET ID = '$i', NAME = '$n', ADDRESS = '$a' WHERE ID = '$i';";
- if(!mysqli_query($connection, $query)) echo("<p>Error updating student data.</p>");
- }
- /* Retrieve a record from the table. */
- function GetRecord($connection, $id) {
- $i = mysqli_real_escape_string($connection, $id);
- $query = "SELECT * FROM STUDENTS WHERE ID = '$i';";
- $result = mysqli_query($connection, $query);
- $query_data = mysqli_fetch_row($result);
- if(!empty($query_data)) {
- echo "\nHTTP GET Response: Retrieving record with ID = $id\n";
- echo "ID | NAME | ADDRESS\n";
- echo "$query_data[0] | $query_data[1] | $query_data[2]\n";
- }
- else {
- echo "\nHTTP GET Response: The given ID does not exist in the database.\n";
- }
- if(!mysqli_query($connection, $query)) echo("<p>Error getting student data.</p>");
- }
- /* Delete a record from the table. */
- function DeleteRecord($connection, $id) {
- $i = mysqli_real_escape_string($connection, $id);
- $query = "SELECT * FROM STUDENTS WHERE ID = '$i';";
- $result = mysqli_query($connection, $query);
- $query_data = mysqli_fetch_row($result);
- if(!empty($query_data)) {
- $query = "DELETE FROM STUDENTS WHERE ID = '$i';";
- }
- else {
- echo "\nHTTP DELETE Response: The given ID does not exist in the database.\n";
- }
- if(!mysqli_query($connection, $query)) echo("<p>Error deleting student data.</p>");
- }
- /* Invalid input error messages */
- function InvalidRequest($connection, $request) {
- $r = mysqli_real_escape_string($connection, $request);
- echo("\nCannot perform HTTP $r request: Invalid input.\n");
- echo("\nPlease refer to the API description for more details.\n");
- }
- /* Check whether the table exists and, if not, create it. */
- function VerifyStudentsTable($connection, $dbName) {
- if(!TableExists("STUDENTS", $connection, $dbName))
- {
- $query = "CREATE TABLE STUDENTS (
- ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- NAME VARCHAR(45),
- ADDRESS VARCHAR(90)
- )";
- if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");
- }
- }
- /* Check for the existence of a table. */
- function TableExists($tableName, $connection, $dbName) {
- $t = mysqli_real_escape_string($connection, $tableName);
- $d = mysqli_real_escape_string($connection, $dbName);
- $checktable = mysqli_query($connection,
- "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");
- if(mysqli_num_rows($checktable) > 0) return true;
- return false;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement