PHP/MySQLi CRUD Operation with Bootstrap/Modal
This tutorial will teach you the Basic Create, Read, Update and Delete Operations using PHP/MySQLi with Boostrap/Modal. Note: Bootstrap and Javascripts used in this tutorial are hosted so you need internet connection for them to work.
Creating our Database
First step is to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "sample".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
CREATE TABLE `user` ( `userid` INT(11) NOT NULL AUTO_INCREMENT, `firstname` VARCHAR(30) NOT NULL, `lastname` VARCHAR(30) NOT NULL, `address` VARCHAR(100) NOT NULL, PRIMARY KEY(`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Creating our Connection
Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.
<?php //MySQLi Procedural if (!$conn) { } ?>
index.php
Next step is to create our sample table. This will be our Read.
<!DOCTYPE html> <html> <head> <title>PHP/MySQLi CRUD Operation using Bootstrap/Modal</title> <script src="<a href="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> " rel="nofollow">https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></scri...</a> <link rel="stylesheet" href="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"</a> /> <script src="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></s...</a> class="container"> <div style="height:50px;"></div> <div class="well" style="margin:auto; padding:auto; width:80%;"> <span style="font-size:25px; color:blue"><center><strong>PHP/MySQLi CRUD Operation using Bootstrap</strong></center></span> <span class="pull-left"><a href="#addnew" data-toggle="modal" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Add New</a></span> <div style="height:50px;"></div> <table class="table table-striped table-bordered table-hover"> <thead> <th>Firstname</th> <th>Lastname</th> <th>Address</th> <th>Action</th> </thead> <tbody> <?php include('conn.php'); ?> <tr> <td><?php echo $row['firstname']; ?></td> <td><?php echo $row['lastname']; ?></td> <td><?php echo $row['address']; ?></td> <td> <a href="#edit<?php echo $row['userid']; ?>" data-toggle="modal" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit</a> || <a href="#del<?php echo $row['userid']; ?>" data-toggle="modal" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</a> <?php include('button.php'); ?> </td> </tr> <?php } ?> </tbody> </table> </div> <?php include('add_modal.php'); ?> </div> </body> </html>
add_modal.php
This is the our form to add new row to our database.
<!-- Add New --> <div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> </div> <div class="modal-body"> <div class="container-fluid"> <form method="POST" action="addnew.php"> <div class="row"> <div class="col-lg-2"> </div> <div class="col-lg-10"> <input type="text" class="form-control" name="firstname"> </div> </div> <div class="row"> <div class="col-lg-2"> </div> <div class="col-lg-10"> <input type="text" class="form-control" name="lastname"> </div> </div> <div class="row"> <div class="col-lg-2"> </div> <div class="col-lg-10"> <input type="text" class="form-control" name="address"> </div> </div> </div> </div> <div class="modal-footer"> </form> </div> </div> </div> </div>
button.php
This contains our Edit Modal and Delete Modal.
<!-- Delete --> <div class="modal fade" id="del<?php echo $row['userid']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <center><h4 class="modal-title" id="myModalLabel">Delete</h4></center> </div> <div class="modal-body"> <?php ?> <div class="container-fluid"> <h5><center>Firstname: <strong><?php echo $drow['firstname']; ?></strong></center></h5> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button> <a href="delete.php?id=<?php echo $row['userid']; ?>" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</a> </div> </div> </div> </div> <!-- /.modal --> <!-- Edit --> <div class="modal fade" id="edit<?php echo $row['userid']; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <center><h4 class="modal-title" id="myModalLabel">Edit</h4></center> </div> <div class="modal-body"> <?php ?> <div class="container-fluid"> <form method="POST" action="edit.php?id=<?php echo $erow['userid']; ?>"> <div class="row"> <div class="col-lg-2"> <label style="position:relative; top:7px;">Firstname:</label> </div> <div class="col-lg-10"> <input type="text" name="firstname" class="form-control" value="<?php echo $erow['firstname']; ?>"> </div> </div> <div style="height:10px;"></div> <div class="row"> <div class="col-lg-2"> <label style="position:relative; top:7px;">Lastname:</label> </div> <div class="col-lg-10"> <input type="text" name="lastname" class="form-control" value="<?php echo $erow['lastname']; ?>"> </div> </div> <div style="height:10px;"></div> <div class="row"> <div class="col-lg-2"> <label style="position:relative; top:7px;">Address:</label> </div> <div class="col-lg-10"> <input type="text" name="address" class="form-control" value="<?php echo $erow['address']; ?>"> </div> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button> <button type="submit" class="btn btn-warning"><span class="glyphicon glyphicon-check"></span> Save</button> </div> </form> </div> </div> </div> <!-- /.modal -->
addnew.php
This is our code in adding the data in our add form to our database.
<?php include('conn.php'); $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; $address=$_POST['address']; mysqli_query($conn,"insert into user (firstname, lastname, address) values ('$firstname', '$lastname', '$address')"); ?>
edit.php
The code for our Update Form.
<?php include('conn.php'); $id=$_GET['id']; $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; $address=$_POST['address']; mysqli_query($conn,"update user set firstname='$firstname', lastname='$lastname', address='$address' where userid='$id'"); ?>
delete.php
Lastly, this our delete code to delete row in our table.
<?php include('conn.php'); $id=$_GET['id']; ?>
That ends this tutorial. For any questions or comments, feel free to comment below or message me here at sourcecodester. Hope this tutorial helps. Happy Coding :)
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Comments
Tiago França (not verified)
Thu, 03/08/2018 - 20:38
Permalink
Gostei É incrível amazing
amazing, it helped me a lot, I was testing some functions
Hermon (not verified)
Thu, 09/21/2017 - 16:38
Permalink
Comment
Nice and works as desired and serves the purpose as intended
Anonymous (not verified)
Fri, 07/12/2019 - 01:53
Permalink
how to creat comment feature
how to creat comment feature like this?
Add new comment