How to Insert Data into Mysql Database using PHP

In this tutorial, we will explain How to insert data into Mysql database using PHP.

Below Steps:

index.php

<form method="post" action="insert.php" enctype="multipart/form-data">
<div class="name">
	<div class="name1"><h1>Student Name:</h1></div>
	<div class="name2"><input type="text" name="name" value="" /></div>
</div>
<div class="name">
	<div class="name1"><h1>Father Name:</h1></div>
	<div class="name2"><input type="text" name="father_name" value="" /></div>
</div>
<div class="name">
	<div class="name1"><h1>Mother Name:</h1></div>
	<div class="name2"><input type="text" name="mother_name" value="" /></div>
</div>
<div class="name">
    <div class="name1"><h1>E-mail:</h1></div>
    <div class="name2"><input type="email" name="email" value=""  /></div>
</div>
<div class="name">
    <div class="name1"><h1>Address:</h1></div>
    <div class="name2"><textarea name="address"> </textarea></div>
</div>
<div class="name">
	<div class="name1"><h2>DOB:</h2></div>
    <div class="name2"><input type="text" class="inp" name="dob" value=""  /></div>
</div>
<div class="name">
	<div class="name1"><h2>Category:</h2></div>
	<div class="name3"><input type="radio" name="gender" value="male" /></div><h2 class="cat">male</h2>
    <div class="name3"><input type="radio" name="gender" value="female"/></div><h2 class="cat">female</h2>
</div>
<div class="name">
	<div class="name1"><h2>Course:</h2></div>
    <div class="name4"><select name="course">
    							<option value="BCA">BCA</option>
                                <option value="MCA">MCA</option>
                                <option value="BBA">BBA</option>
                                <option value="DCA">DCA</option>
    					</select></div>
</div>
    <input type="submit" class="sub" name="sub" value="Submit"  />
</form>

style.css

.name{
width:100%;
min-height:30px;
height:auto;
float:left;
margin-bottom:20px;
}
.name1{
width:14%;
min-height:20px;
float:left;
height:auto;
}
.name1 h1{
font-size:18px;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
.name1 h2{
font-size:18px;
font-family:Verdana, Arial, Helvetica, sans-serif;
margin-top:55px;
}
.name2{
width:30%;
height:30px;
float:left;
}
.name input{
margin-top:10px;
height:25px;
width:50%;
border:#999999 1px solid;
border-radius:5px;
}
.name textarea{
margin-top:10px;
height:70px;
width:50%;
border:#999999 1px solid;
border-radius:5px;
}
.name2 input.inp{
margin-top:50px;
}
.sub{
margin-top:50px;
margin-left:120px;
}
.name3{
width:3%;
height:30px;
float:left;
margin-top:45px;
}
.cat{
float:left;
font-size:20px;
margin-top:55px;
}
.name4{
width:3%;
height:30px;
float:left;
margin-top:60px;
}

insert.php

<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('db');
if(isset($_POST['sub']) == 'Submit'){
 $name = $_POST['name'];
$father_name = $_POST['father_name'];
$mother_name = $_POST['mother_name'];
$e_mail = $_POST['email'];
$add = $_POST['address'];
$dob = $_POST['dob'];
$category = $_POST['gender'];
$courses = $_POST['course'];

$query = "insert into new set name='$name', father_name='$father_name', mother_name='$mother_name', address='$add', dob='$dob', e_mail='$e_mail', courses='$courses',   category='$category' "; 
$num =mysql_query($query);
}
?>

How to Update Data into Database using PHP

3 thoughts on “How to Insert Data into Mysql Database using PHP

Leave a Reply

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