Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php include "../inc/dbinfo.inc"; ?>
- <html>
- <body>
- <h1>ECE 531: Final Project<h1>
- <p>Database on Heater Status & Actions</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 SETACTIONS table exists. */
- VerifySetActionsTable($connection, DB_DATABASE);
- /* If input fields are populated, read the inputs. */
- $http_request = htmlentities($_POST['REQUEST']);
- $sa_id = htmlentities($_POST['ID']);
- $sa_timestamp = htmlentities($_POST['TIMESTAMP']);
- $sa_temperature = htmlentities($_POST['TEMPERATURE']);
- $sa_set_points = htmlentities($_POST['SET_POINTS']);
- $sa_current_status = htmlentities($_POST['CURRENT_STATUS']);
- $sa_action = htmlentities($_POST['ACTION']);
- /* No HTTP request error messages */
- if (!strlen($http_request) && (strlen($sa_id) || strlen($sa_timestamp) || strlen($sa_temperature) || strlen($sa_set_points) || strlen($sa_current_status) || strlen($sa_action))) {
- NoRequest();
- }
- /* PUT Request = Add Record */
- if ($http_request == "PUT") {
- if (!strlen($sa_id) && (strlen($sa_timestamp) || strlen($sa_temperature) || strlen($sa_set_points) || strlen($sa_current_status) || strlen($sa_action))) {
- if (!strlen($sa_timestamp)) $sa_timestamp = " ";
- if (!strlen($sa_temperature)) $sa_temperature = " ";
- if (!strlen($sa_set_points)) $sa_set_points = " ";
- if (!strlen($sa_current_status)) $sa_current_status = " ";
- if (!strlen($sa_action)) $sa_action = " ";
- AddRecord($connection, $sa_timestamp, $sa_temperature, $sa_set_points, $sa_current_status, $sa_action);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* POST Request = Update Record */
- if ($http_request == "POST") {
- if (strlen($sa_id) && (strlen($sa_timestamp) || strlen($sa_temperature) || strlen($sa_set_points) || strlen($sa_current_status) || strlen($sa_action))) {
- $result = mysqli_query($connection, "SELECT * FROM SETACTIONS WHERE ID = '$sa_id';");
- $query_data = mysqli_fetch_row($result);
- if (!strlen($sa_timestamp)) {
- $sa_timestamp = $query_data[1];
- }
- elseif (!strlen($sa_temperature)) {
- $sa_temperature = $query_data[2];
- }
- elseif (!strlen($sa_set_points)) {
- $sa_set_points = $query_data[3];
- }
- elseif (!strlen($sa_current_status)) {
- $sa_current_status = $query_data[4];
- }
- elseif (!strlen($sa_action)) {
- $sa_action = $query_data[5];
- }
- UpdateRecord($connection, $sa_id, $sa_timestamp, $sa_temperature, $sa_set_points, $sa_current_status, $sa_action);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* GET Request = Retrieve Record */
- if ($http_request == "GET") {
- if (strlen($sa_id) && !strlen($sa_timestamp) && !strlen($sa_temperature) && !strlen($sa_set_points) && !strlen($sa_current_status) && !strlen($sa_action)) {
- GetRecord($connection, $sa_id);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* DELETE Request = Delete Record */
- if ($http_request == "DELETE") {
- if (strlen($sa_id) && !strlen($sa_timestamp) && !strlen($sa_temperature) && !strlen($sa_set_points) && !strlen($sa_current_status) && !strlen($sa_action)) {
- DeleteRecord($connection, $sa_id);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- ?>
- <!-- Display table data. -->
- <table border="1" cellpadding="2" cellspacing="2">
- <tr>
- <td>ID</td>
- <td>TIMESTAMP</td>
- <td>TEMPERATURE</td>
- <td>SET_POINTS</td>
- <td>CURRENT_STATUS</td>
- <td>ACTION</td>
- </tr>
- <?php
- $result = mysqli_query($connection, "SELECT * FROM SETACTIONS");
- 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>",
- "<td>", $query_data[3], "</td>",
- "<td>", $query_data[4], "</td>",
- "<td>", $query_data[5], "</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, $timestamp, $temperature, $set_points, $current_status, $action) {
- $time = mysqli_real_escape_string($connection, $timestamp);
- $temp = mysqli_real_escape_string($connection, $temperature);
- $spts = mysqli_real_escape_string($connection, $set_points);
- $csts = mysqli_real_escape_string($connection, $current_status);
- $actn = mysqli_real_escape_string($connection, $action);
- $query = "INSERT INTO SETACTIONS (TIMESTAMP, TEMPERATURE, SET_POINTS, CURRENT_STATUS, ACTION) VALUES ('$time', '$temp', '$spts', '$csts', '$actn');";
- if(!mysqli_query($connection, $query)) echo("<p>Error adding record.</p>");
- }
- /* Update a record to the table. */
- function UpdateRecord($connection, $id, $timestamp, $temperature, $set_points, $current_status, $action) {
- $i = mysqli_real_escape_string($connection, $id);
- $time = mysqli_real_escape_string($connection, $timestamp);
- $temp = mysqli_real_escape_string($connection, $temperature);
- $spts = mysqli_real_escape_string($connection, $set_points);
- $csts = mysqli_real_escape_string($connection, $current_status);
- $actn = mysqli_real_escape_string($connection, $action);
- $query = "UPDATE SETACTIONS SET ID = '$i', TIMESTAMP = '$time', TEMPERATURE = '$temp', SET_POINTS = '$spts', CURRENT_STATUS = '$csts', ACTION = '$actn' WHERE ID = '$i';";
- if(!mysqli_query($connection, $query)) echo("<p>Error updating record.</p>");
- }
- /* Retrieve a record from the table. */
- function GetRecord($connection, $id) {
- $i = mysqli_real_escape_string($connection, $id);
- $query = "SELECT * FROM SETACTIONS 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 | TIMESTAMP | TEMPERATURE | SET_POINTS | CURRENT_STATUS | ACTION\n";
- echo "$query_data[0] | $query_data[1] | $query_data[2] | $query_data[3] | $query_data[4] | $query_data[5]\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 record.</p>");
- }
- /* Delete a record from the table. */
- function DeleteRecord($connection, $id) {
- $i = mysqli_real_escape_string($connection, $id);
- $query = "SELECT * FROM SETACTIONS WHERE ID = '$i';";
- $result = mysqli_query($connection, $query);
- $query_data = mysqli_fetch_row($result);
- if(!empty($query_data)) {
- $query = "DELETE FROM SETACTIONS 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 record.</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 VerifySetActionsTable($connection, $dbName) {
- if(!TableExists("SETACTIONS", $connection, $dbName))
- {
- $query = "CREATE TABLE SETACTIONS (
- ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- TIMESTAMP VARCHAR(45),
- TEMPERATURE VARCHAR(90),
- SET_POINTS VARCHAR(90),
- CURRENT_STATUS VARCHAR(90),
- ACTION 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