How to insert records in mySQL using PHP?

In this tutorial i explained how to add records from HTML forms to phpmyadmin in mySQL database Table. I used Bootstrap 4 for designing forms and button to click for execution of INSERT INTO Query.

First of all you need to create form in HTML. I have used bootstrap 4 so you need to download and link css and js files in your page. see given code below:

add-student.php

<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1", charset="utf-8"/>
<title>Add New Student Record</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
	<div class="row">
    	<div class="col-md-12">
        
        <div class="p-3 bg-success text-center">
        <h1 class="text-white">Add New Student Record</h1>
        </div>
       <div class="p-3">
            <form action="insert-std.php" method="post">
            
            <div class="form-group">
            <label>Student Name : </label>
            <input type="text" name="stdName" class="form-control" placeholder="Enter student name ... ">
            </div>
            
            <div class="form-group">
            <label>Student Class : </label>
            <select name="stdClass" class="form-control">
            <option>Class A</option><option>Class B</option><option>Class C</option>
            <option>Class D</option><option>Class E</option><option>Class F</option>
            </select>
            </div>
            
            <div class="form-group">
            <label>Student Marks : </label>
            <input type="text" name="stdMarks" class="form-control" placeholder="Enter student Marks ... ">
            </div>
            
            <div class="form-group">
            <label>Student Address : </label>
            <textarea class="form-control" rows="5" name="stdAddress"></textarea>
            </div>
            
            <button type="submit" class="btn btn-dark">Add New Record</button>
            </form>
        </div>  
        </div>
    </div>
</div>
</body>
</html>

insert-std.php

<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1", charset="utf-8"/>
<title>Add New Student Record</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>

<body>
<?php 
include "connect.php";

$sname = $_POST['stdName'];
$sclass = $_POST['stdClass'];
$smarks = $_POST['stdMarks'];
$saddress = $_POST['stdAddress'];

$sql = "INSERT INTO students (std_name, std_class, std_marks, std_address)VALUES('$sname', '$sclass', '$smarks', '$saddress');";

mysqli_query($con, $sql);
mysqli_close($con);
?>

<div class="jumbotron jumbotron-fluid bg-dark">
<div class="container text-center">
<h1 class="text-white display-4">Record Added Successfully</h1>
</div>
</div>
</body>
</html>

Congratulations you have completed successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pageviews