Navbar

My Blog List

Wednesday, September 16, 2020

PHP CRUD-1: Bootstrap Modal: How to Insert Data into Database in PHP


Here in this above video, we are learning about how to Insert Data into database in php,

so, now you have to create a folder named phpcrud inside your htdocs in your localhost or if you are using direct on your server then not required, now can directly copy the below code and start using as per your requirement.

Step 1: Create a table named students :

Create a table in your database in your phpmyadmin and add the below columns:
1. id (PrimaryKey, AI)
2. fname
3. lname
4. class
5. section

After creating student table successfully, then lets get started with the coding part as given below:

Step 2: Create a file named index.php  and paste the below code init:

This below html code is about how to design a pop up dialog box using bootstrap modal where it is know as Modal. we are using this modal to save the Student data into student_table in database using php and then after saving the data, we return  a status/success message about the Record stored successfully or not. we are showing this message using php session.

<?php session_start(); ?>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <title>PHP CRUD -  Bootstrap Modal (POP UP)</title>
</head>
<body>
  
<!-- Student ADD Modal -->
<div class="modal fade" id="studentModal" tabindex="-1" role="dialog" aria-labelledby="studentModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="studentModalLabel">Student Data</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
        <form action="code.php" method="POST">
            <div class="modal-body">
                <div class="form-group">
                    <label for="">First Name</label>
                    <input type="text" name="fname" class="form-control" placeholder="Enter First Name">
                </div>
                <div class="form-group">
                    <label for="">Last Name</label>
                    <input type="text" name="lname" class="form-control" placeholder="Enter Last Name">
                </div>
                <div class="form-group">
                    <label for="">Class</label>
                    <input type="text" name="class" class="form-control" placeholder="Enter Class">
                </div>
                <div class="form-group">
                    <label for="">Section</label>
                    <input type="text" name="section" class="form-control" placeholder="Enter Section">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" name="save_student" class="btn btn-primary">Save</button>
            </div>
        </form>
    </div>
  </div>
</div>


<div class="container mt-5">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <?php 
                    if(isset($_SESSION['status']) && $_SESSION['status'] !='')
                    {
                        ?>
                        
                        <div class="alert alert-warning alert-dismissible fade show" role="alert">
                            <strong>Hey!</strong> <?php echo $_SESSION['status']; ?>
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <?php
                        unset($_SESSION['status']);
                    }
                    ?>
                    <div class="card-header">
                        <h5>
                            PHP CRUD -  Bootstrap Modal (POP UP)
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-primary float-right" data-toggle="modal" data-target="#studentModal">
                            Add Student
                        </button>
                        </h5>
                    </div>
                    <div class="card-body">
                        <h5>Database Records</h5>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    
    </body>
</html>

Step 3: Create a file named code.php and paste the below code init:

This below php code are about the MySQLi and PHP queries where it is performing the task of inserting or storing the data/record of student like, first_name, Lastname, class, seection, into the database.
   
<?php session_start(); $conn = mysqli_connect("localhost","root","","phpcrud"); if(isset($_POST['save_student'])) { $fname = $_POST['fname']; $lname = $_POST['lname']; $class = $_POST['class']; $section = $_POST['section']; $query = "INSERT INTO students (fname,lname,class,section) VALUES ('$fname','$lname','$class','$section')"; $query_run = mysqli_query($conn, $query); if($query_run) { $_SESSION['status'] = "Successfully Saved"; header('Location: index.php'); } else { $_SESSION['status'] = "Not Saved"; header('Location: index.php'); } } ?>


Thank you.
Regards,

Ved Prakash N.

No comments:

Post a Comment