How to Update Data into Database using PHP

How to Update Data into Database using PHP

Today we will guide you How to Update Data into Database using PHP and MySQL.

index.php

<html>
<head>
<title>Update Data</title>
</head>
<body>
<form method="post" action="update.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="<?php echo $row['name']; ?>" /></div>
</div>
<div class="name">
	<div class="name1"><h1>Father Name:</h1></div>
	<div class="name2"><input type="text" name="father_name" value="<?php echo $row['father_name']; ?>" /></div>
</div>
<div class="name">
	<div class="name1"><h1>Mother Name:</h1></div>
	<div class="name2"><input type="text" name="mother_name" value="<?php echo $row['mother_name']; ?>" /></div>
</div>
<div class="name">
    <div class="name1"><h1>E-mail:</h1></div>
    <div class="name2"><input type="email" name="email" value="<?php echo $row['e_mail']; ?>"  /></div>
</div>
<div class="name">
    <div class="name1"><h1>Address:</h1></div>
    <div class="name2"><textarea name="address"><?php echo $row['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="<?php echo $row['dob']; ?>"  /></div>
</div>
<div class="name">
	<div class="name1"><h2>Category:</h2></div>
	<div class="name3"><input type="radio" name="gender" value="male" <?php if($row['category']== 'male' ) { ?> checked="checked" <?php } ?>/></div><h2 class="cat">male</h2>
    <div class="name3"><input type="radio" name="gender" value="female" <?php if($row['category']== 'female' ) { ?> checked="checked" <?php } ?>/></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="<?php echo $row['courses']; ?>"><?php echo $row['courses']; ?></option>
    							<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>
</body></html>

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;
}

update.php

<?php
include(db.php);
$id = $_GET['id'];
if(isset($_POST['sub']) == 'Submit'){
$name = $_POST['name'];                                          // from input name/value
$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 = "update new set name='$name', father_name='$father_name', mother_name='$mother_name', address='$add', dob='$dob', e_mail='$e_mail', courses='$courses',   category='$category' where id = '$id'";                                             // from database
$num=mysql_query($query);
}
$query1 = "select * from new where id = '$id' ";
$query2 = mysql_query($query1);
$row = mysql_fetch_array($query2);
echo "<pre>";print_r($row);echo "</pre> <br>";
?>

db.php

<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('db');
?>