Insert data into database in php mysql


you will learn how to insert data into database in php mysql, where we will create a form, database connection and finally the processing code to save your data in MySQL Database.

Let's create a table named as students in database as given below:

CREATE TABLE `students` (
    id int NOT NULL AUTO_INCREMENT,
    fullname varchar(55) NOT NULL,
    course varchar(55) NOT NULL,
    email varchar(55) NOT NULL,
    PRIMARY KEY (id)
)  ENGINE=InnoDB DEFAULT CHARSET=latin1;


So guys, Lets get started to insert data in database in php mysql:

Step 1: Create a file named dbconfig.php to give the database connection in php.

<?php
    $host = "localhost";
    $username = "your_username";
    $password = "your_password";
    $database = "your_database_name";

    // Create DB Connection
    $conn = mysqli_connect($host, $username, $password, $database);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
?>


Step 2: Create a file named student.php to make a HTML FORM. Also we will be using bootstrap v5 to design the FORM.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Insert Data in PHP MySQL</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
   
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card shadow">
                    <div class="card-header">
                        <h4>Insert data into database in PHP MySQL</h4>
                    </div>
                    <div class="card-body">

                        <form action="code.php" method="POST">

                            <div class="mb-3">
                                <label>Full Name</label>
                                <input type="text" name="fullname" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label>Course</label>
                                <input type="text" name="course" class="form-control" required>
                            </div>
                            <div class="mb-3">
                                <label>Email ID</label>
                                <input type="email" name="email" class="form-control" required>
                            </div>
                         
                            <div class="mb-3">
                                <hr/>
                                <button type="submit" name="insert_buttton" class="btn btn-primary">Insert Data</button>
                            </div>

                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>


Step 3: Create a file named code.php to write the code to save data into database in php mysql.

<?php
    include_once 'dbconfig.php';

    if(isset($_POST['insert_buttton']))
    {    
        $fullname = $_POST['fullname'];
        $course = $_POST['course'];
        $email = $_POST['email'];

        $query = "INSERT INTO students (fullname,course,email) VALUES ('$fullname','$course','$email')";
        $result = mysqli_query($conn, $query);
        if($result)
        {
            echo "Data Inserted Successfully!";
        }
        else
        {
            echo "Data Not Inserted!. Error: " . $sql . "" . mysqli_error($conn);
        }
    }
?>


That's it. We successfully stored the Data.

Thanks for reading.