· 7 years ago · Feb 09, 2019, 07:44 PM
1<?php
2session_start();
3require 'connection.php';
4
5#Alert message to be posted
6$alert = "";
7$id = "";
8
9#If the session is not set or the session id is not 1 then redirect user off page
10if(!isset($_SESSION) || $_SESSION['id'] != 1) {
11 header("Location: restricted.php");
12}
13
14#Insert button is pressed
15if(isset($_POST['insert'])) {
16
17 #Get values from the input box
18 $authorName = $_POST['authorName'];
19
20 #Create query to see if author is already in db
21 $check = $conn->query("SELECT * FROM author WHERE author.name LIKE '$authorName'");
22 $conn->error;
23 if(mysqli_num_rows($check) > 0) {
24
25 #Author already exists in the DB
26 $alert = '<div class="alert alert-danger">
27 <strong>Warning!</strong> ' . $authorName . ' already exists in the database.
28 </div>';
29 } else {
30
31 #Since author doesn't already exist we insert it into the db
32 $insertQuery = $conn->query("INSERT INTO author(name) VALUES ('$authorName')");
33 $conn->error;
34 $alert = '<div class="alert alert-success">
35 <strong>Success!</strong> ' . $authorName . ' has been added to the database.
36 </div>';
37 }
38
39}
40
41if(isset($_POST['delete'])) {
42 #Get the author ID you want to delete
43 $id = $_POST['authors'];
44
45 $deletequery = $conn->query("DELETE FROM author WHERE authorID='$id'");
46 $conn->error;
47
48 $alert = '<div class="alert alert-success">
49 <strong>Success!</strong> Author has been deleted.
50 </div>';
51}
52
53?>
54
55<!DOCTYPE html>
56<html>
57<head>
58 <title>UND Bookstore Login</title>
59 <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
60 <meta name="author" content="Sai Peri"></meta>
61 <meta name="description" content="CSCI 457 Assignment 1"></meta>
62 <!-- Latest compiled and minified CSS -->
63 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
64 <!-- Optional theme -->
65 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
66 <!-- Latest compiled and minified JavaScript -->
67 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
68 <!-- Latest jQuery libraries -->
69 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
70</head>
71<body>
72<div class="container">
73 <div class="row">
74 <div class="col-md-12">
75 <nav id="nav" class="navbar navbar-inverse text-center">
76 <ul class="nav navbar-nav">
77 <li><a href="index.php">Home</a></li>
78 <li><a href="login.php">Login</a></li>
79 <li><a href="search.php">Search</a></li>
80 <li class="active"><a href="check.php">Add/Delete/Edit</a></li>
81 </ul>
82 </nav>
83 </div> <!-- End col-md-12 -->
84 </div> <!-- End row (nav)-->
85 <div class="row">
86 <div class="col-md-12">
87 <div class="page-header">
88 <h3 class="text-center">Administrator Access</h3>
89 </div> <!-- End of page-header -->
90 </div> <!-- End col-md-12 -->
91 </div> <!-- End row (nav)-->
92 <div class="row" id="body">
93 <!-- Add text boxes and buttons -->
94 <form action="author.php" method="post">
95 <div class="form-row text-center">
96 <div class="form-group col-md-12">
97 <p class="lead text-primary">The drop down below has a list of authors already in the database. If you want to add a new author, enter the name below and than press the insert button.</p>
98 </div> <!-- End form-group -->
99 </div> <!-- End form-group -->
100 <div class="form-row text-center">
101 <div class="form-group col-md-4 col-md-offset-4">
102 <label>Existing authors: </label>
103 <?php
104 #Query to fetch all data from authors table
105 $queryDropdown = $conn->query("SELECT * FROM author");
106 $conn->error;
107 $row = $queryDropdown->fetch_all(MYSQLI_NUM);
108
109 #Dynamically load from the database into a drop down menu
110 echo '<select name="authors"> ';
111
112 #Fetch size of array and then iterate through each row
113 $size = sizeof($row);
114 for($i = 0; $i <= $size; $i++) {
115 #Data is in 2D array. $i represents row second index represents column
116 echo '<option value="' . $row[$i][0] . '">' . $row[$i][1] . '</option>';
117 }
118 echo '</select>';
119 ?>
120 <!-- Adding button to delete entry of dropdown -->
121 <button type="submit" class="btn btn-primary" name="delete" value="delete">Delete</button>
122 </div> <!-- End form-group -->
123 </div> <!-- End form-group -->
124 <div class="form-row text-center">
125 <div class="form-group col-md-4 col-md-offset-4">
126 <lable>Author Name:</lable>
127 <input type="text" class="form-control" id="authorName" name="authorName">
128 </div> <!-- End form-group -->
129 </div> <!-- End form-group -->
130 <div class="form-row text-center">
131 <div class="form-group col-md-4 col-md-offset-4">
132 <button type="submit" class="btn btn-primary" name="insert" value="insert">Insert</button>
133 <?php echo $alert;?>
134 </div> <!-- End form-group -->
135 </div> <!-- End form-group -->
136 </form>
137 </div> <!-- End of row -->
138 <div class="row" id="footer">
139 <div class="col-md-12 text-center">
140 <footer>Created 2019 by Sai Peri</footer>
141 </div> <!-- End col-md-12 -->
142 </div> <!-- End row (nav)-->
143</div> <!-- End container -->
144</body>
145</html>