In this php tutorial we will learn how to update records in php using database of mysql. To update records in php is quite easy, let's have a look over below mentioned example to learn how to update records in php.
edit-recs.php
=========(Code)edit-recs.php=========
<?php
require_once('conn.php');
$select_query="select * from student";
$result=mysql_query($select_query);
?>
<body>
<table width="100%" cellpadding="2" cellspacing="2" class="fontclass">
<tr>
<td width="12%">
<strong>Roll No</strong></td>
<td width="16%">
<strong>First Name</strong></td>
<td width="20%">
<strong>Last Name</strong></td>
<td colspan="2">
<strong>Father Name</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result))
{
?>
<tr>
<td width="12%">
<?php echo $rows['rollno'];?></td>
<td width="16%">
<?php echo $rows['fname'];?></td>
<td width="20%">
<?php echo $rows['lname'];?></td>
<td width="12%">
<?php echo $rows['fathername'];?></td>
<td width="40%"><strong><a href="update-recs.php?rollno=<?php echo $rows['rollno'];?>">Edit</a></strong></td>
</tr>
<?php
}
?>
</table>
</body>
update-recs.php
=========(Code)update-recs.php=========
<?php
require_once('conn.php');
$rollno=$_GET['rollno'];
$select_query="select * from student where rollno='$rollno'";
$result=mysql_query($select_query);
$rows=mysql_fetch_array($result);
?>
<table width="600" cellpadding="2" cellspacing="2">
<tr>
<form name="form1" method="post" action="do-update.php">
<tr>
<td>First Name</td><td><input type="text" name="fname" value="<?php echo $rows['fname'];?>"/></td>
</tr>
<tr>
<td>Last Name</td><td><input type="text" name="lname" value="<?php echo $rows['lname'];?>" /></td>
</tr>
<tr>
<td>Father Name</td><td><input type="text" name="fathername" value="<?php echo $rows['fathername'];?>" /></td>
</tr>
<tr>
<td><input type="hidden" name="rollno" value="<?php echo $rollno;?>" /></td><td><input type="submit" value="Update Records" /></td>
</tr>
</form>
</tr>
</table>
do-update.php
<?php
require_once('conn.php');
$rollno=$_POST['rollno'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$fathername=$_POST['fathername'];
$sql="update student set fname='$fname',lname='$lname',fathername='$fathername' where rollno='$rollno'";
$result=mysql_query($sql);
if($result)
{
header("location:display.php");
}
else{
mysql_error();
}
?>
So this is the way to update records in php.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment