Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dbhelper.php
- <?php
- //database config
- $hostname = 'localhost';
- $username = 'root';
- $password = '';
- $database = 'bookManagement';
- function dbConnect() {
- global $hostname, $username, $password, $database;
- return mysqli_connect($hostname, $username, $password, $database);
- } // returns the connection (database and php)
- function getAllBooks($table) {
- $rows = array();
- $sql = "SELECT * from `$table`";
- $connection = dbConnect();
- $query = mysqli_query($connection, $sql);
- while($row = mysqli_fetch_assoc($query)){
- array_push($rows, $row);
- }
- mysqli_close($connection);
- return $rows;
- } // returns an array of books
- function getBook($table, $where, $id){
- $rows=array();
- $sql = "SELECT * FROM `$table` WHERE `$where` = '$id'";
- $conn = dbconnect();
- $query = mysqli_query($conn,$sql);
- while($row = mysqli_fetch_assoc($query))
- array_push($rows,$row);
- mysqli_close($conn);
- return $rows;
- } // returns an array of the book found
- function addBook($table, $fields, $data) {
- $ok = false;
- if(count($fields) == count($data)){
- $flds = implode("`,`", $fields);
- $dta = implode("','", $data);
- $sql = "INSERT INTO `$table`(`$flds`) VALUES('$dta')";
- $conn = dbconnect();
- $query = mysqli_query($conn,$sql);
- $ok = true;
- mysqli_close($conn);
- }
- return $ok;
- } // returns a boolean
- function updateBook($table, $fields, $data, $where, $id) {
- $okey=-1;
- $flds = array();
- if(count($fields) == count($data)){
- for($i=0;$i<count($fields);$i++)
- $flds[] = "`".$fields[$i]."`='".$data[$i]."'";
- $f = implode(",",$flds);
- $sql = "UPDATE `$table` SET $f WHERE `$where` = '$id' ";
- //echo $sql;
- $conn = dbconnect();
- $query = mysqli_query($conn,$sql);
- $okey = mysqli_affected_rows($conn);
- mysqli_close($conn);
- }
- return $okey;
- } // returns a boolean
- function deleteBook($table, $where, $id) {
- $count = -1;
- $sql = "DELETE FROM `$table` WHERE `$where` = '$id'";
- $conn = dbconnect();
- $query = mysqli_query($conn,$sql);
- $count = mysqli_affected_rows($conn);
- mysqli_close($conn);
- return $count;
- } // returns a boolean
- //print_r(getBook('books', 'isbn', '0001'));
- ?>
- -----------------------------------
- actions.php
- <?php
- //session_start();
- include("util/dbhelper.php");
- if(isset($_POST['search'])) {
- $isbn = $_POST['isbn'];
- $title = $_POST['title'];
- $copyright = $_POST['copyright'];
- $edition = $_POST['edition'];
- $price = $_POST['price'];
- $quantity = $_POST['quantity'];
- $message = "";
- //$ok = array();
- $ok = getBook('books', 'isbn', $isbn);
- $message = "";
- if(count($ok) > 0){
- $message="ITEM FOUND";
- $ok_param = urlencode(json_encode($ok));
- header("location:index.php?message=$message&ok=$ok_param");
- } else {
- $message="ITEM NOT FOUND";
- header("location:index.php?message=$message&?ok=$ok");
- }
- } elseif(isset($_POST['add'])) {
- $isbn = $_POST['isbn'];
- $title = $_POST['title'];
- $copyright = $_POST['copyright'];
- $edition = $_POST['edition'];
- $price = $_POST['price'];
- $quantity = $_POST['quantity'];
- $message = "";
- if($isbn == null) {
- $message = "NO RECORD TO ADD";
- header("location:index.php?message=$message");
- } elseif(getBook('books', 'isbn', $isbn)) {
- $message = "RECORD ALREADY EXISTS";
- header("location:index.php?message=$message");
- } else {
- $ok = false;
- $ok = addBook('books', ['isbn','title','copyright','edition','price','quantity'], [$isbn, $title, $copyright, $edition, $price, $quantity]);
- if($ok){
- $message = "RECORD SUCCESSFULLY SAVED";
- }
- header("location:index.php?message=$message");
- }
- } elseif(isset($_POST['delete'])) {
- $isbn = $_POST['isbn'];
- $ok = deleteBook('books', 'isbn', $isbn);
- $message = "";
- if($ok) {
- $message = "RECORD SUCCESSFULLY DELETED";
- header("location:index.php?message=$message");
- } else {
- $message = "RECORD NOT DELETED";
- header("location:index.php?message=$message");
- }
- } elseif(isset($_POST['edit'])) {
- $isbn = $_POST['isbn'];
- $title = $_POST['title'];
- $copyright = $_POST['copyright'];
- $edition = $_POST['edition'];
- $price = $_POST['price'];
- $quantity = $_POST['quantity'];
- $message = "";
- if($title == null || $copyright == null || $edition == null || $price == null || $quantity == null) {
- $message = "NO RECORD TO EDIT";
- header("location:index.php?message=$message");
- } elseif(count(getBook('books', 'isbn', $isbn)) < 0) {
- $message = "ISBN# IS NOT FOUND";
- header("location:index.php?message=$message");
- } elseif($ok) {
- $ok = updateBook('books', ['title', 'copyright', 'edition', 'price', 'quantity'], [$title, $copyright, $edition, $price, $quantity], 'isbn', $isbn);
- $message = "RECORD SUCCESSFULLY UPDATED";
- header("location:index.php?message=$message");
- }
- }
- ?>
- -------------------------------
- index.php
- <?php
- //session_start();
- include('util/dbhelper.php');
- $books = getAllBooks('books');
- $ok = array();
- if (isset($_GET['ok'])) {
- // Decode the JSON string to get back the original $ok array
- $ok = json_decode(urldecode($_GET['ok']), true);
- }
- $isbn = '';
- $title = '';
- $copyright = '';
- $edition = '';
- $price = '';
- $quantity = '';
- foreach($ok as $k) {
- $isbn = $k['isbn'];
- $title = $k['title'];
- $copyright = $k['copyright'];
- $edition = $k['edition'];
- $price = $k['price'];
- $quantity = $k['quantity'];
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="assets/css/w3.css">
- <title>Books Management</title>
- </head>
- <body>
- <div class="w3-container w3-padding-large w3-auto">
- <!-- form and buttons div -->
- <form id="form" action="actions.php" method="post" class="w3-container w3-padding-large">
- <!-- form -->
- <div class="w3-left w3-half">
- <div>
- <div>
- <label for="isbn">ISBN #:</label>
- <input class="w3-input w3-border" type="number" id="isbn" name="isbn" value="<?php echo $isbn; ?>" autofocus>
- </div>
- <div>
- <label for="title">Title:</label>
- <input class="w3-input w3-border" type="text" id="title" name="title" value="<?php echo $title; ?>">
- </div>
- <div>
- <label for="copyright">Copyright:</label>
- <input class="w3-input w3-border" type="text" id="copyright" name="copyright" value="<?php echo $copyright; ?>">
- </div>
- <div>
- <label for="edition">Edition:</label>
- <input class="w3-input w3-border" type="text" id="edition" name="edition" value="<?php echo $edition; ?>">
- </div>
- <div>
- <label for="price">Price:</label>
- <input class="w3-input w3-border" type="number" id="price" name="price" value="<?php echo $price; ?>">
- </div>
- <div>
- <label for="quantity">Quantity:</label>
- <input class="w3-input w3-border" type="number" id="quantity" name="quantity" value="<?php echo $quantity; ?>">
- </div>
- </div>
- </div>
- <!-- form -->
- <!-- buttons -->
- <div class="w3-right w3-half w3-center">
- <div>
- <div class="w3-padding-large">
- <button id="search" onclick="searchBtn()" name="search" type="submit" class="w3-button w3-blue">SEARCH</button>
- <button id="editBtn" type="submit" onclick="editBtn()" name="edit" class="w3-button w3-amber">EDIT</button>
- </div>
- <div class="w3-padding-large">
- <button id="deleteBtn" type="submit" name="delete" class="w3-button w3-red">DELETE</button>
- <button id="addBtn" type="submit" onclick="addBtn()" name="add" class="w3-button w3-green">ADD</button>
- </div>
- </div>
- <div class="">
- <h1 class="w3-center">
- <!-- this is the prompt -->
- <?php
- $message="";
- if(isset($_GET['message'])) {
- $message = $_GET['message'];
- }
- // prompt = $message
- echo $message;
- ?>
- <!-- this is the prompt -->
- </h1>
- </div>
- </div>
- <!-- buttons -->
- </form>
- <!-- form and buttons div -->
- <!-- list of all books container here -->
- <div>
- <table class="w3-table-all">
- <tr>
- <th>ISBN</th>
- <th>Title</th>
- <th>Copyright</th>
- <th>Edition</th>
- <th>Price</th>
- <th>Quantity</th>
- <th>Total</th>
- </tr>
- <?php
- $totalQuantity=0;
- $totalCost=0;
- foreach($books as $book){
- echo '<tr>';
- echo '<td>'.$book['isbn'].'</td>';
- echo '<td>'.$book['title'].'</td>';
- echo '<td>'.$book['copyright'].'</td>';
- echo '<td>'.$book['edition'].'</td>';
- echo '<td>'.$book['price'].'</td>';
- echo '<td>'.$book['quantity'].'</td>';
- echo '<td>'.$book['price'] * $book['quantity'].'</td>';
- echo '</tr>';
- // Calculate the running totals
- $totalQuantity += $book['quantity'];
- $totalCost += ($book['price'] * $book['quantity']);
- }
- echo '<tr>';
- echo '<td colspan="5"></td>';
- echo '<td>'.$totalQuantity.'</td>';
- echo '<td>'.$totalCost.'</td>';
- echo '</tr>';
- ?>
- </table>
- </div>
- <!-- list of all books container here -->
- </div>
- <script>
- function addBtn() {
- //document.getElementById('method').value = 1;
- document.getElementById('isbn').focus();
- }
- function searchBtn() {
- document.getElementById('isbn').autofocus = false;
- document.getElementById('isbn').disabled = true;
- document.getElementById('isbn').value = <?php echo $isbn; ?>;
- document.getElementById('title').value = <?php echo $title; ?>;
- document.getElementById('copyright').value = <?php echo $copyright; ?>;
- document.getElementById('edition').value = <?php echo $edition; ?>;
- document.getElementById('price').value = <?php echo $price; ?>;
- document.getElementById('quantity').value = <?php echo $quantity; ?>;
- }
- // function editBtn() {
- // document.getElementById('isbn').disabled();
- // }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment