PHP Crud With MySQLI (Procedural)

PHP Crud is the necessary part of PHP language when you start any language first you need to learn Crud operation.

PHP is the world most popular language for creating websites, In web development field PHP is the key to web development using PHP Crud(create, read, update, delete) operation you can communicate with MySQL database using MySQLI queries, not MySQL because the PHP7 version is now not supported MySQL queries.  so at least you need to use MySQLI(Procedural) or MySQLI(oops) concept but in this article, we will create PHP Crud application using MySQLI(Procedural) concept. so let's get started.
php crud


[Download Source Code ##download##]

Follow the following steps:

  1. First, we need to create a database in MySQL database so for that goto phpmyadmin and click on SQL tab and copy the below query code and paste in SQL query area and click on go.
  2. create database php_crud
  3. Second, we need to create a table inside the php_crud database, so click on the php_crud database and now click on SQL tab and copy the below code and paste in SQL query area.
  4. CREATE TABLE `students` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `name` varchar(50) NOT NULL,
     `marks` varchar(50) NOT NULL,
     `department` varchar(50) NOT NULL,
     `result` varchar(50) NOT NULL,
     PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
  5. Third, we need to create a connection with MySQL database so create the connection.php file and copy the below code and paste the below code in your connection.php file and your connection is done.
  6. <?php
    $con = mysqli_connect("localhost","root","","php_crud");
    ?>
  7. Fourth, from PHP Crud we need to create and read the data, so create index.php file in index.php file we have simple bootstrap 4 modal and in modal we have user form, using that form we will insert data in MySQL database,  this script also displays the data,  copy the below code in pasted inside your index.php file
  8. 
    <!DOCTYPE html>
    <html>
    <head>
     <title>PHP Crud Using Mysqli</title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
     <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap4.min.css">
    
    </head>
    <body>
    <div class="container" style="margin-top: 70px;">
     <div class="row justify-content-center">
      <div class="col-md-10 text-center">
       <?php
    include "connection.php";
    if(isset($_POST['submit'])){
     $student_name         = $_POST['student_name'];
     $student_marks        = $_POST['marks'];
     $student_department   = $_POST['department'];
     $student_result       = $_POST['result'];
     $Query = mysqli_query($con, "INSERT INTO students (name,marks,department,result) VALUES ('$student_name','$student_marks', '$student_department','$student_result')");
     if($Query){
      echo "<script>alert('Student record is successfully inserted!')</script>";
     }else{
      echo "<script>alert('Sorry an error occured!')</script>";
     }
    
    }
    
    ?>
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Add Record
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
           <form method="POST" action="">
            <div class="form-group">
             <input type="text" name="student_name" class="form-control" placeholder="Enter Name..." required="">
            </div><!-- form-group -->
            <div class="form-group">
             <input type="text" name="marks" class="form-control" placeholder="Enter marks..." required="">
            </div><!-- form-group -->
            <div class="form-group">
             <input type="text" name="department" class="form-control" placeholder="Enter Department..." required="">
            </div><!-- form-group -->
            <div class="form-group">
             <input type="text" name="result" class="form-control" placeholder="Enter result..." required="">
            </div><!-- form-group -->
            <div class="form-group">
             <input type="submit" name="submit" class="btn btn-info" value="Add Student">
            </div><!-- form-group -->
           </form><!-- form -->
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
       
          </div>
        </div>
      </div>
    </div>
    <table id="example" class="table table-striped table-bordered" width="100%" cellspacing="0">
     <thead>
      <tr>
       <th>Name</th>
       <th>Marks</th>
       <th>Department</th>
       <th>Result</th>
       <th>Update</th>
       <th>Delete</th>
      </tr>
     </thead>
     <tbody>
      <?php
    $Show = mysqli_query($con, "SELECT * FROM students");
    while($r = mysqli_fetch_array($Show)): ?>
        <tr>
         <td><?php echo $r['name']; ?></td>
         <td><?php echo $r['marks']; ?></td>
         <td><?php echo $r['department']; ?></td>
         <td><?php echo $r['result']; ?></td>
         <td><a href="update.php?update_id=<?php echo $r['id']; ?>" class="btn btn-warning">
      Update
    </a></td>
         <td><a href="delete.php?delete_id=<?php echo $r['id']; ?>" class="btn btn-danger">
      Delete
    </a></td>
        </tr>
        <?php endwhile; ?>
     </tbody>
     </table>
      </div><!-- col -->
     </div><!-- row -->
    </div><!-- container -->
    
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
    <script type="text/javascript">
     $(document).ready(function() {
        $('#example').DataTable();
    } );
    </script>
    </body>
    
    </html>
    
  9. Now from PHP Crud we need to update the data, so for updating the data just create simple update.php file and after copy the below code and paste in your update.php file
  10. <?php include "connection.php";
    if(isset($_POST['update'])){
      $name       = $_POST['student_name'];
      $marks      = $_POST['marks'];
      $department = $_POST['department'];
      $result     = $_POST['result'];
      $id         = $_POST['edit_id'];
      $query      = mysqli_query($con, "UPDATE students SET name = '$name' ,marks = '$marks', department = '$department', result = '$result' WHERE id = '$id'");
      if ($query) {
        header("location:index.php");
      }else{
        echo "<script>alert('Sorry update query not work')</script>";
      }
    }
     ?>
    <!DOCTYPE html>
    <html>
    <head>
     <title>PHP Crud Using Mysqli</title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    </head>
    <body>
    <div class="container" style="margin-top: 70px;">
     <div class="row justify-content-center">
      <div class="col-md-10 text-center">
       <?php
    
      if(isset($_GET['update_id'])): ?>
      <?php $id = $_GET['update_id']; ?>
      <?php $query = mysqli_query($con, "SELECT * FROM students WHERE id = '$id' ");
      $r = mysqli_fetch_array($query);
      $name = $r['name'];
      $marks = $r['marks'];
      $department = $r['department'];
      $result = $r['result'];
       ?>
        <form method="POST" action="update.php">
            <div class="form-group">
              <input type="text" name="student_name" class="form-control" placeholder="Enter Name..." required="" value="<?php echo $name; ?>">
            </div><!-- form-group -->
            <div class="form-group">
              <input type="text" name="marks" class="form-control" placeholder="Enter marks..." required="" value="<?php echo $marks; ?>">
            </div><!-- form-group -->
            <div class="form-group">
              <input type="text" name="department" class="form-control" placeholder="Enter Department..." required="" value="<?php echo $department; ?>">
            </div><!-- form-group -->
            <div class="form-group">
              <input type="text" name="result" class="form-control" placeholder="Enter result..." required="" value="<?php echo $result; ?>">
            </div><!-- form-group -->
            <input type="hidden" name="edit_id" value="<?php echo $id; ?>">
            <div class="form-group">
              <input type="submit" name="update" class="btn btn-info" value="Edit Student">
            </div><!-- form-group -->
           </form><!-- form -->
    <?php endif; ?>
    
    
    
      </div><!-- col -->
     </div><!-- row -->
    </div><!-- container -->
    
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
    </body>
    
    </html>
    
    
  11. Finally, we need to delete the data so just create delete.php file and copy the below code and paste the below code into your delete.php file
  12. <?php 
    include 'connection.php';
    if(isset($_GET['delete_id'])){
     $id = $_GET['delete_id'];
     $query = mysqli_query($con, "DELETE FROM students WHERE id = '$id'");
     if($query){
      header("location:index.php");
     }else{
      echo "<script>alert('Sorry delete query not work!')</script>";
     }
    }
    
     ?>
Finally, you have done everything!

COMMENTS

Name

bootstrap,1,CSS,6,Example,3,HTML,6,login,1,mysql,3,PHP,5,
ltr
item
webtipstricks: PHP Crud With MySQLI (Procedural)
PHP Crud With MySQLI (Procedural)
PHP Crud is the necessary part of PHP language when you start any language first you need to learn Crud operation.
https://1.bp.blogspot.com/-gqNxkITP--4/WfalXytifgI/AAAAAAAABaM/NdtsfIORpTkqseHR0zZopnJdZ7y6vTRGwCLcBGAs/s640/php-crud.jpg
https://1.bp.blogspot.com/-gqNxkITP--4/WfalXytifgI/AAAAAAAABaM/NdtsfIORpTkqseHR0zZopnJdZ7y6vTRGwCLcBGAs/s72-c/php-crud.jpg
webtipstricks
http://www.webtipstricks.com/2017/10/php-crud-mysqli-procedural.html
http://www.webtipstricks.com/
http://www.webtipstricks.com/
http://www.webtipstricks.com/2017/10/php-crud-mysqli-procedural.html
true
4027471107773963481
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy