CRUD Operation Using MySQLi Source Code

In this tutorial we will create a CRUD Operation using MySQLi. This code has several functionalities that can manipulate a data through database server using MySQLi query. The code use a MySQLi query to (Create, Read, Update, Delete) a certain data to a database server. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interepret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_mysqli_crud, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn=mysqli_connect("localhost", "root", "", "db_mysqli_crud");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4.         <head>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  6.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">CRUD Operation Using MySQLi Source Code</h3>
  17.                 <hr style="border-top:1px dotted #ccc;" />
  18.                 <div class="col-md-4">
  19.                         <form method="POST" action="add.php">
  20.                                 <div class="form-group">
  21.                                         <label>Firstname</label>
  22.                                         <input class="form-control" type="text" name="firstname"/>
  23.                                 </div>
  24.                                 <div class="form-group">
  25.                                         <label>Lastname</label>
  26.                                         <input class="form-control" type="text" name="lastname"/>
  27.                                 </div>
  28.                                 <div class="form-group">
  29.                                         <label>Address</label>
  30.                                         <input class="form-control" type="text" name="address"/>
  31.                                 </div>
  32.                                 <div class="form-group">
  33.                                         <button class="btn btn-primary form-control" type="submit" name="save">Save</button>
  34.                                 </div>
  35.                         </form>
  36.                 </div>
  37.                 <div class="col-md-8">
  38.                         <table class="table table-bordered">
  39.                                 <thead class="alert-info">
  40.                                         <tr>
  41.                                                 <th>Firstname</th>
  42.                                                 <th>Lastname</th>
  43.                                                 <th>Address</th>
  44.                                                 <th>Action</th>
  45.                                         </tr>
  46.                                 </thead>
  47.                                 <tbody>
  48.                                         <?php
  49.                                                 require 'conn.php';
  50.                                                 $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  51.                                                 while($fetch = mysqli_fetch_array($query)){
  52.                                         ?>
  53.                                         <tr>
  54.                                                 <td><?php echo $fetch['firstname']?></td>
  55.                                                 <td><?php echo $fetch['lastname']?></td>
  56.                                                 <td><?php echo $fetch['address']?></td>
  57.                                                 <td><button class="btn btn-warning btn-sm" data-toggle="modal" data-target="#update<?php echo $fetch['mem_id']?>">Edit</button> | <a class="btn btn-danger btn-sm" href="delete.php?id=<?php echo $fetch['mem_id']?>">Delete</a></td>
  58.                                         </tr>
  59.                                        
  60.                                         <div class="modal fade" id="update<?php echo $fetch['mem_id']?>" aria-hidden="true">
  61.                                                 <div class="modal-dialog">
  62.                                                         <div class="modal-content">
  63.                                                                 <form method="POST" action="update.php">
  64.                                                                         <div class="modal-header">
  65.                                                                                 <h3 class="modal-title">Update User</h3>
  66.                                                                         </div> 
  67.                                                                         <div class="modal-body">
  68.                                                                                 <div class="col-md-2"></div>
  69.                                                                                 <div class="col-md-8">
  70.                                                                                         <div class="form-group">
  71.                                                                                                 <label>Firstname</label>
  72.                                                                                                 <input class="form-control" type="text" value="<?php echo $fetch['firstname']?>" name="firstname"/>
  73.                                                                                                 <input type="hidden" value="<?php echo $fetch['mem_id']?>" name="mem_id"/>
  74.                                                                                         </div>
  75.                                                                                         <div class="form-group">
  76.                                                                                                 <label>Lastname</label>
  77.                                                                                                 <input class="form-control" type="text" value="<?php echo $fetch['lastname']?>" name="lastname"/>
  78.                                                                                         </div>
  79.                                                                                         <div class="form-group">
  80.                                                                                                 <label>Address</label>
  81.                                                                                                 <input class="form-control" type="text" value="<?php echo $fetch['address']?>" name="address"/>
  82.                                                                                         </div>
  83.                                                                                         <div class="form-group">
  84.                                                                                                 <button class="btn btn-warning form-control" type="submit" name="update">Update</button>
  85.                                                                                         </div>
  86.                                                                                 </div> 
  87.                                                                         </div> 
  88.                                                                         <br style="clear:both;"/>
  89.                                                                         <div class="modal-footer">
  90.                                                                                 <button class="btn btn-danger" data-dismiss="modal">Close</button>
  91.                                                                         </div>
  92.                                                                 </form>
  93.                                                         </div>
  94.                                                 </div>
  95.                                         </div> 
  96.                                        
  97.                                         <?php
  98.                                                 }
  99.                                         ?>
  100.                                 </tbody>
  101.                         </table>
  102.                 </div>
  103.         </div>
  104. <script src="js/jquery-3.2.1.min.js"></script> 
  105. <script src="js/bootstrap.js"></script>
  106. </body>
  107. </html>

Creating the Main Function

This code contains the main function of the application. This code contain several functionalities that help manage the data within the table. To do that write these block of codes inside the text editor and save it as shown below. add.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['save'])){
  5.                 $firstname=$_POST['firstname'];
  6.                 $lastname=$_POST['lastname'];
  7.                 $address=$_POST['address'];
  8.                 mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  9.                 header('location:index.php');
  10.         }
  11. ?>
update.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['update'])){
  5.  
  6.                 $mem_id = $_POST['mem_id'];
  7.                 $firstname=$_POST['firstname'];
  8.                 $lastname=$_POST['lastname'];
  9.                 $address=$_POST['address'];
  10.                
  11.                 mysqli_query($conn, "UPDATE `member` SET `firstname`='$firstname', `lastname`='$lastname', `address`='$address' WHERE `mem_id`='$mem_id'") or die(mysqli_error());
  12.                 header('location:index.php');
  13.         }
  14. ?>
delete.php
  1. <?php
  2.         if(ISSET($_GET['id'])){
  3.                 require_once 'conn.php';
  4.                 $id = $_GET['id'];
  5.                 mysqli_query($conn, "DELETE FROM `member` WHERE `mem_id`='$id'") or die(mysqli_error());
  6.                 header('location:index.php');
  7.         }
  8. ?>
There you have it we successfully created CRUD Operation using MySQLi. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment