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 Projecth1>
- <p>Database on Set Points with Timestamp and Temperature</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 SETPOINTS table exists. */
- VerifySetPointsTable($connection, DB_DATABASE);
- /* If input fields are populated, read the inputs. */
- $http_request = htmlentities($_POST['REQUEST']);
- $sp_id = htmlentities($_POST['ID']);
- $sp_timestamp = htmlentities($_POST['TIMESTAMP']);
- $sp_temperature = htmlentities($_POST['TEMPERATURE']);
- /* No HTTP request error messages */
- if (!strlen($http_request) && (strlen($sp_id) || strlen($sp_timestamp) || strlen($sp_temperature))) {
- NoRequest();
- }
- /* PUT Request = Add Record */
- if ($http_request == "PUT") {
- if (!strlen($sp_id) && (strlen($sp_timestamp) || strlen($sp_temperature))) {
- if (!strlen($sp_timestamp)) $sp_timestamp = " ";
- if (!strlen($sp_temperature)) $sp_temperature = " ";
- AddRecord($connection, $sp_timestamp, $sp_temperature);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* POST Request = Update Record */
- if ($http_request == "POST") {
- if (strlen($sp_id) && (strlen($sp_timestamp) || strlen($sp_temperature))) {
- $result = mysqli_query($connection, "SELECT * FROM SETPOINTS WHERE ID = '$sp_id';");
- $query_data = mysqli_fetch_row($result);
- if (!strlen($sp_timestamp)) {
- $sp_timestamp = $query_data[1];
- }
- elseif (!strlen($sp_temperature)) {
- $sp_temperature = $query_data[2];
- }
- UpdateRecord($connection, $sp_id, $sp_timestamp, $sp_temperature);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* GET Request = Retrieve Record */
- if ($http_request == "GET") {
- if (strlen($sp_id) && !strlen($sp_timestamp) && !strlen($sp_temperature)) {
- GetRecord($connection, $sp_id);
- }
- else {
- InvalidRequest($connection, $http_request);
- }
- }
- /* DELETE Request = Delete Record */
- if ($http_request == "DELETE") {
- if (strlen($sp_id) && !strlen($sp_timestamp) && !strlen($sp_temperature)) {
- DeleteRecord($connection, $sp_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>
- </tr>
- <?php
- $result = mysqli_query($connection, "SELECT * FROM SETPOINTS");
- 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, $timestamp, $temperature) {
- $time = mysqli_real_escape_string($connection, $timestamp);
- $temp = mysqli_real_escape_string($connection, $temperature);
- $query = "INSERT INTO SETPOINTS (TIMESTAMP, TEMPERATURE) VALUES ('$time', '$temp');";
- if(!mysqli_query($connection, $query)) echo("<p>Error adding student data.</p>");
- }
- /* Update a record to the table. */
- function UpdateRecord($connection, $id, $timestamp, $temperature) {
- $i = mysqli_real_escape_string($connection, $id);
- $time = mysqli_real_escape_string($connection, $timestamp);
- $temp = mysqli_real_escape_string($connection, $temperature);
- $query = "UPDATE SETPOINTS SET ID = '$i', TIMESTAMP = '$time', TEMPERATURE = '$temp' 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 SETPOINTS 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\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 SETPOINTS WHERE ID = '$i';";
- $result = mysqli_query($connection, $query);
- $query_data = mysqli_fetch_row($result);
- if(!empty($query_data)) {
- $query = "DELETE FROM SETPOINTS 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 VerifySetPointsTable($connection, $dbName) {
- if(!TableExists("SETPOINTS", $connection, $dbName))
- {
- $query = "CREATE TABLE SETPOINTS (
- ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- TIMESTAMP VARCHAR(45),
- TEMPERATURE 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