Kulas_Code20

Power

Jul 19th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.74 KB | None | 0 0
  1. dbhelper.php
  2. <?php
  3.  
  4. //database config
  5.  
  6. $hostname = 'localhost';
  7. $username = 'root';
  8. $password = '';
  9. $database = 'bookManagement';
  10.  
  11. function dbConnect() {
  12. global $hostname, $username, $password, $database;
  13. return mysqli_connect($hostname, $username, $password, $database);
  14. } // returns the connection (database and php)
  15.  
  16. function getAllBooks($table) {
  17. $rows = array();
  18. $sql = "SELECT * from `$table`";
  19. $connection = dbConnect();
  20. $query = mysqli_query($connection, $sql);
  21.  
  22. while($row = mysqli_fetch_assoc($query)){
  23. array_push($rows, $row);
  24. }
  25. mysqli_close($connection);
  26. return $rows;
  27. } // returns an array of books
  28.  
  29. function getBook($table, $where, $id){
  30. $rows=array();
  31. $sql = "SELECT * FROM `$table` WHERE `$where` = '$id'";
  32. $conn = dbconnect();
  33. $query = mysqli_query($conn,$sql);
  34. while($row = mysqli_fetch_assoc($query))
  35. array_push($rows,$row);
  36. mysqli_close($conn);
  37. return $rows;
  38. } // returns an array of the book found
  39.  
  40. function addBook($table, $fields, $data) {
  41. $ok = false;
  42. if(count($fields) == count($data)){
  43. $flds = implode("`,`", $fields);
  44. $dta = implode("','", $data);
  45. $sql = "INSERT INTO `$table`(`$flds`) VALUES('$dta')";
  46. $conn = dbconnect();
  47. $query = mysqli_query($conn,$sql);
  48. $ok = true;
  49. mysqli_close($conn);
  50. }
  51. return $ok;
  52. } // returns a boolean
  53.  
  54. function updateBook($table, $fields, $data, $where, $id) {
  55. $okey=-1;
  56. $flds = array();
  57. if(count($fields) == count($data)){
  58. for($i=0;$i<count($fields);$i++)
  59. $flds[] = "`".$fields[$i]."`='".$data[$i]."'";
  60. $f = implode(",",$flds);
  61. $sql = "UPDATE `$table` SET $f WHERE `$where` = '$id' ";
  62. //echo $sql;
  63. $conn = dbconnect();
  64. $query = mysqli_query($conn,$sql);
  65. $okey = mysqli_affected_rows($conn);
  66. mysqli_close($conn);
  67. }
  68. return $okey;
  69. } // returns a boolean
  70.  
  71. function deleteBook($table, $where, $id) {
  72. $count = -1;
  73. $sql = "DELETE FROM `$table` WHERE `$where` = '$id'";
  74. $conn = dbconnect();
  75. $query = mysqli_query($conn,$sql);
  76. $count = mysqli_affected_rows($conn);
  77. mysqli_close($conn);
  78. return $count;
  79. } // returns a boolean
  80.  
  81. //print_r(getBook('books', 'isbn', '0001'));
  82.  
  83. ?>
  84.  
  85. -----------------------------------
  86. actions.php
  87. <?php
  88. //session_start();
  89. include("util/dbhelper.php");
  90.  
  91. if(isset($_POST['search'])) {
  92. $isbn = $_POST['isbn'];
  93. $title = $_POST['title'];
  94. $copyright = $_POST['copyright'];
  95. $edition = $_POST['edition'];
  96. $price = $_POST['price'];
  97. $quantity = $_POST['quantity'];
  98.  
  99. $message = "";
  100.  
  101. //$ok = array();
  102. $ok = getBook('books', 'isbn', $isbn);
  103. $message = "";
  104. if(count($ok) > 0){
  105. $message="ITEM FOUND";
  106. $ok_param = urlencode(json_encode($ok));
  107. header("location:index.php?message=$message&ok=$ok_param");
  108. } else {
  109. $message="ITEM NOT FOUND";
  110. header("location:index.php?message=$message&?ok=$ok");
  111. }
  112. } elseif(isset($_POST['add'])) {
  113. $isbn = $_POST['isbn'];
  114. $title = $_POST['title'];
  115. $copyright = $_POST['copyright'];
  116. $edition = $_POST['edition'];
  117. $price = $_POST['price'];
  118. $quantity = $_POST['quantity'];
  119.  
  120. $message = "";
  121. if($isbn == null) {
  122. $message = "NO RECORD TO ADD";
  123. header("location:index.php?message=$message");
  124. } elseif(getBook('books', 'isbn', $isbn)) {
  125. $message = "RECORD ALREADY EXISTS";
  126. header("location:index.php?message=$message");
  127. } else {
  128. $ok = false;
  129. $ok = addBook('books', ['isbn','title','copyright','edition','price','quantity'], [$isbn, $title, $copyright, $edition, $price, $quantity]);
  130. if($ok){
  131. $message = "RECORD SUCCESSFULLY SAVED";
  132. }
  133. header("location:index.php?message=$message");
  134. }
  135. } elseif(isset($_POST['delete'])) {
  136. $isbn = $_POST['isbn'];
  137. $ok = deleteBook('books', 'isbn', $isbn);
  138. $message = "";
  139. if($ok) {
  140. $message = "RECORD SUCCESSFULLY DELETED";
  141. header("location:index.php?message=$message");
  142. } else {
  143. $message = "RECORD NOT DELETED";
  144. header("location:index.php?message=$message");
  145. }
  146. } elseif(isset($_POST['edit'])) {
  147. $isbn = $_POST['isbn'];
  148. $title = $_POST['title'];
  149. $copyright = $_POST['copyright'];
  150. $edition = $_POST['edition'];
  151. $price = $_POST['price'];
  152. $quantity = $_POST['quantity'];
  153. $message = "";
  154. if($title == null || $copyright == null || $edition == null || $price == null || $quantity == null) {
  155. $message = "NO RECORD TO EDIT";
  156. header("location:index.php?message=$message");
  157. } elseif(count(getBook('books', 'isbn', $isbn)) < 0) {
  158. $message = "ISBN# IS NOT FOUND";
  159. header("location:index.php?message=$message");
  160. } elseif($ok) {
  161. $ok = updateBook('books', ['title', 'copyright', 'edition', 'price', 'quantity'], [$title, $copyright, $edition, $price, $quantity], 'isbn', $isbn);
  162. $message = "RECORD SUCCESSFULLY UPDATED";
  163. header("location:index.php?message=$message");
  164. }
  165. }
  166.  
  167. ?>
  168.  
  169. -------------------------------
  170. index.php
  171. <?php
  172. //session_start();
  173.  
  174. include('util/dbhelper.php');
  175. $books = getAllBooks('books');
  176.  
  177. $ok = array();
  178. if (isset($_GET['ok'])) {
  179. // Decode the JSON string to get back the original $ok array
  180. $ok = json_decode(urldecode($_GET['ok']), true);
  181. }
  182. $isbn = '';
  183. $title = '';
  184. $copyright = '';
  185. $edition = '';
  186. $price = '';
  187. $quantity = '';
  188. foreach($ok as $k) {
  189. $isbn = $k['isbn'];
  190. $title = $k['title'];
  191. $copyright = $k['copyright'];
  192. $edition = $k['edition'];
  193. $price = $k['price'];
  194. $quantity = $k['quantity'];
  195. }
  196. ?>
  197.  
  198. <!DOCTYPE html>
  199. <html lang="en">
  200. <head>
  201. <meta charset="UTF-8">
  202. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  203. <link rel="stylesheet" href="assets/css/w3.css">
  204. <title>Books Management</title>
  205. </head>
  206. <body>
  207. <div class="w3-container w3-padding-large w3-auto">
  208. <!-- form and buttons div -->
  209. <form id="form" action="actions.php" method="post" class="w3-container w3-padding-large">
  210. <!-- form -->
  211. <div class="w3-left w3-half">
  212. <div>
  213. <div>
  214. <label for="isbn">ISBN #:</label>
  215. <input class="w3-input w3-border" type="number" id="isbn" name="isbn" value="<?php echo $isbn; ?>" autofocus>
  216. </div>
  217. <div>
  218. <label for="title">Title:</label>
  219. <input class="w3-input w3-border" type="text" id="title" name="title" value="<?php echo $title; ?>">
  220. </div>
  221. <div>
  222. <label for="copyright">Copyright:</label>
  223. <input class="w3-input w3-border" type="text" id="copyright" name="copyright" value="<?php echo $copyright; ?>">
  224. </div>
  225. <div>
  226. <label for="edition">Edition:</label>
  227. <input class="w3-input w3-border" type="text" id="edition" name="edition" value="<?php echo $edition; ?>">
  228. </div>
  229. <div>
  230. <label for="price">Price:</label>
  231. <input class="w3-input w3-border" type="number" id="price" name="price" value="<?php echo $price; ?>">
  232. </div>
  233. <div>
  234. <label for="quantity">Quantity:</label>
  235. <input class="w3-input w3-border" type="number" id="quantity" name="quantity" value="<?php echo $quantity; ?>">
  236. </div>
  237. </div>
  238. </div>
  239. <!-- form -->
  240. <!-- buttons -->
  241. <div class="w3-right w3-half w3-center">
  242. <div>
  243. <div class="w3-padding-large">
  244. <button id="search" onclick="searchBtn()" name="search" type="submit" class="w3-button w3-blue">SEARCH</button>
  245. <button id="editBtn" type="submit" onclick="editBtn()" name="edit" class="w3-button w3-amber">EDIT</button>
  246. </div>
  247. <div class="w3-padding-large">
  248. <button id="deleteBtn" type="submit" name="delete" class="w3-button w3-red">DELETE</button>
  249. <button id="addBtn" type="submit" onclick="addBtn()" name="add" class="w3-button w3-green">ADD</button>
  250. </div>
  251. </div>
  252. <div class="">
  253. <h1 class="w3-center">
  254. <!-- this is the prompt -->
  255. <?php
  256. $message="";
  257. if(isset($_GET['message'])) {
  258. $message = $_GET['message'];
  259. }
  260. // prompt = $message
  261. echo $message;
  262. ?>
  263. <!-- this is the prompt -->
  264. </h1>
  265. </div>
  266. </div>
  267. <!-- buttons -->
  268. </form>
  269. <!-- form and buttons div -->
  270. <!-- list of all books container here -->
  271. <div>
  272. <table class="w3-table-all">
  273. <tr>
  274. <th>ISBN</th>
  275. <th>Title</th>
  276. <th>Copyright</th>
  277. <th>Edition</th>
  278. <th>Price</th>
  279. <th>Quantity</th>
  280. <th>Total</th>
  281. </tr>
  282. <?php
  283. $totalQuantity=0;
  284. $totalCost=0;
  285. foreach($books as $book){
  286. echo '<tr>';
  287. echo '<td>'.$book['isbn'].'</td>';
  288. echo '<td>'.$book['title'].'</td>';
  289. echo '<td>'.$book['copyright'].'</td>';
  290. echo '<td>'.$book['edition'].'</td>';
  291. echo '<td>'.$book['price'].'</td>';
  292. echo '<td>'.$book['quantity'].'</td>';
  293. echo '<td>'.$book['price'] * $book['quantity'].'</td>';
  294. echo '</tr>';
  295. // Calculate the running totals
  296. $totalQuantity += $book['quantity'];
  297. $totalCost += ($book['price'] * $book['quantity']);
  298. }
  299. echo '<tr>';
  300. echo '<td colspan="5"></td>';
  301. echo '<td>'.$totalQuantity.'</td>';
  302. echo '<td>'.$totalCost.'</td>';
  303. echo '</tr>';
  304. ?>
  305. </table>
  306. </div>
  307. <!-- list of all books container here -->
  308. </div>
  309. <script>
  310. function addBtn() {
  311. //document.getElementById('method').value = 1;
  312. document.getElementById('isbn').focus();
  313. }
  314.  
  315. function searchBtn() {
  316. document.getElementById('isbn').autofocus = false;
  317. document.getElementById('isbn').disabled = true;
  318.  
  319. document.getElementById('isbn').value = <?php echo $isbn; ?>;
  320. document.getElementById('title').value = <?php echo $title; ?>;
  321. document.getElementById('copyright').value = <?php echo $copyright; ?>;
  322. document.getElementById('edition').value = <?php echo $edition; ?>;
  323. document.getElementById('price').value = <?php echo $price; ?>;
  324. document.getElementById('quantity').value = <?php echo $quantity; ?>;
  325. }
  326.  
  327. // function editBtn() {
  328. // document.getElementById('isbn').disabled();
  329. // }
  330. </script>
  331. </body>
  332. </html>
Advertisement
Add Comment
Please, Sign In to add comment