Detri Amelia Chandra
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor
No Result
View All Result
Detriamelia.com
No Result
View All Result

Web and Mobile Programming – Library Part-1

09/08/2023
in Belajar PHP
Web and Mobile Programming – Library Part-1

Web and Mobile Programming – Library Part-1

This time we will discuss the creation of a basic web-based library application. Of course, we will thoroughly explore CRUD using PHP. Please follow the steps below.

1. Create a Database File

Please create a database with the name “library“, then create a table with the name “books” and create fields as shown in the image below:
Database

2. Create the Connection File (koneksi.php)

Please copy and paste the following code and save it in the htdocs/library folder with the name koneksi.php:

1
2
3
4
<?php
    error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
    $connect = mysqli_connect("localhost", "root", "", "library");
?>

3. Create the Index File (index.php)

Next, create the index.php file and save it in the htdocs/library folder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
    require("koneksi.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Book Data</title>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="container">
        <h3>BOOK DATA</h3>
        <hr>
        <a href="add.php" class="btn btn-primary mb-2">Add Book</a>
 
        <!-- PHP code for deleting data -->
        <?php
            if(isset($_POST['delete_data'])){
                $book_id = $_POST['book_id'];
                $process = mysqli_query($connect, "DELETE FROM books WHERE id_buku = '$book_id'");
                if($process){
                    echo"
                        <div class='alert alert-success'>
                            Successfully deleted book data...
                        </div>
                        <meta http-equiv='refresh' content='3;url=index.php'>
                    ";
                } else {
                    echo"
                        <div class='alert alert-danger'>
                            Failed to delete book data...
                        </div>
                    ";
                }
            }
        ?>
 
        <!-- Display table data -->
        <table class="table table-hover table-striped table-bordered">
            <thead>
                <tr>
                    <th class="text-center">#</th>
                    <th>Book ID</th>
                    <th>Book Name</th>
                    <th>Book Genre</th>
                    <th class="text-center">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    $no_data = 1;
                    $select_data = mysqli_query($connect, "SELECT * FROM books");
                    while($data = mysqli_fetch_array($select_data)){
                        echo"
                            <tr>
                                <td class='text-center'>".$no_data.".</td>
                                <td>".$data['book_id']."</td>
                                <td>".$data['book_name']."</td>
                                <td>".$data['genre']."</td>
                                <td class='text-center'>
                                    <form method='post' class='btn-group'>
                                        <a href='add.php?book_id=".$data['book_id']."' class='btn btn-success'>EDIT</a>
                                        <input type='hidden' name='book_id' value='".$data['book_id']."'>
                                        <button type='submit' name='delete_data' class='btn btn-danger'>DELETE</button>
                                    </form>
                                </td>
                            </tr>
                        ";
                        $no_data++;
                    }
                ?>
            </tbody>
        </table>
    </div>
</body>
</html>

4. Create the Add/Edit File (add.php)

Copy and paste the following code and save it in the htdocs/library folder with the name add.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
    require("koneksi.php");
    $book_id = $_GET['book_id'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add/Edit Book Data</title>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="container">
        <?php
            if(isset($_POST['add_data'])){
                $book_name = $_POST['book_name'];
                $genre = $_POST['genre'];
                $author = $_POST['author'];
                $year = $_POST['year'];
                $process = mysqli_query($connect, "INSERT INTO books (book_name, genre, author, year) VALUE ('$book_name','$genre','$author','$year')");
                if($process){
                    echo"
                        <div class='alert alert-success'>
                            Successfully added book data...
                        </div>
                        <meta http-equiv='refresh' content='3;url=index.php'>
                    ";
                } else {
                    echo"
                        <div class='alert alert-danger'>
                            Failed to add book data...
                        </div>
                    ";
                }
            } else if(isset($_POST['edit_data'])){
                $book_id = $_POST['book_id'];
                $book_name = $_POST['book_name'];
                $genre = $_POST['genre'];
                $author = $_POST['author'];
                $year = $_POST['year'];
                $process = mysqli_query($connect, "UPDATE books SET book_name = '$book_name', genre = '$genre', author = '$author', year = '$year' WHERE book_id = '$book_id'");
                if($process){
                    echo"
                        <div class='alert alert-success'>
                            Successfully edited book data...
                        </div>
                        <meta http-equiv='refresh' content='3;url=index.php'>
                    ";
                } else {
                    echo"
                        <div class='alert alert-danger'>
                            Failed to edit book data...
                        </div>
                    ";
                }
            }
            if(empty($book_id)){
        ?>
        <h3>ADD BOOK DATA</h3>
        <hr>
        <form method="post">
            <div class="form-group">
                <label>Book Name:</label>
                <input type="text" class="form-control" name="book_name" placeholder="Book Name" required>
            </div>
            <div class="form-group">
                <label>Genre:</label>
                <input type="text" class="form-control" name="genre" placeholder="Genre" required>
            </div>
            <div class="form-group">
                <label>Author:</label>
                <input type="text" class="form-control" name="author" placeholder="Author" required>
            </div>
            <div class="form-group">
                <label>Year:</label>
                <input type="number" class="form-control" name="year" placeholder="Year" required>
            </div>
            <div class="form-group">
                <a href="index.php" class="btn bg-dark text-light">Cancel</a>
                <button type="submit" class="btn bg-dark text-light" name="add_data" required>Add Data</button>
            </div>
        </form>
        <?php
            } else {
                $select_data = mysqli_query($connect, "SELECT * FROM books WHERE book_id = '$book_id'");
                $data = mysqli_fetch_array($select_data);
        ?>
        <h3>EDIT BOOK DATA</h3>
        <hr>
        <form method="post">
            <input type="hidden" name="book_id" value="<?php echo $data['book_id'];?>">
            <div class="form-group">
                <label>Book Name:</label>
                <input type="text" class="form-control" name="book_name" placeholder="Book Name" value="<?php echo $data['book_name'];?>" required>
            </div>
            <div class="form-group">
                <label>Genre:</label>
                <input type="text" class="form-control" name="genre" placeholder="Genre" value="<?php echo $data['genre'];?>" required>
            </div>
            <div class="form-group">
                <label>Author:</label>
                <input type="text" class="form-control" name="author" value="<?php echo $data['author'];?>" placeholder="Author" required>
            </div>
            <div class="form-group">
                <label>Year:</label>
                <input type="number" class="form-control" name="year" value="<?php echo $data['year'];?>" placeholder="Year" required>
            </div>
            <div class="form-group">
                <a href="index.php" class="btn bg-dark text-light">Cancel</a>
                <button type="submit" class="btn bg-dark text-light" name="edit_data" required>Edit Data</button>
            </div>
        </form>
        <?php
            }
        ?>
    </div>
</body>
</html>

5. Copy the assets Folder

Lastly, download and copy the “assets” folder into the htdocs/library directory. You can download the folder from this link:
Download Assets Folder

Feel free to run the application, and if you have any questions, you can leave a comment below.

Tags: Belajar PHPRPL
ShareTweetShare

Related Posts

Database Table Relations Using Join Operations and the Classical Model
Belajar PHP

Database Table Relations Using Join Operations and the Classical Model

MySQL is a highly popular relational database in today's programming world. With features that allow for table-to-table relationships, MySQL databases...

09/08/2023
Next Post
Database Table Relations Using Join Operations and the Classical Model

Database Table Relations Using Join Operations and the Classical Model

Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language

Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language

Comments 1

  1. Pingback: Relasi Tabel Database Menggunakan Operasi Join dan Model Klasik - Detri Amelia Chandra

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Getting Started with Python: Installation and Setup

Getting Started with Python: Installation and Setup

16/08/2023
Fungsi Peformatan Rupiah Pada Angka PHP

How to Make Rupiah Format Function for Number Using PHP

02/04/2019
Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language

Learn Python Basics for Beginners: Easy Ways to Understand the Python Programming Language

09/08/2023
Application of Jquery Mask to Numbers in HTML and PHP Forms

Application of Jquery Mask For Numbers in HTML and PHP Forms

23/04/2019

Instagram Feed

    The Instagram Access Token is expired, Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to refresh it.
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
Detriamelia.Com

© 2022 Detri Amelia Chandra - IT Tips and Programming Tutorials.

No Result
View All Result
  • HOME
  • GIS
  • HTML
  • Bootstrap
  • CSS
  • Javascript
  • Ubuntu
  • Useful Tools
    • URL Redirect Tool
    • URL Download Encryptor