PHP-MySQL

Display and Update records using PHP

In this tutorials you’ll able to List / Display records and Update records using PHP program with MySQL as a back-end database.

First of all we will display list of all records entered into MySQL database using PHP page. Here is the code for that.

1. File name : list_records.php

<?php
$host="localhost"; 
$username="root";
$password=""; 
$db_name="mars_db"; 
$tbl_name="list"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>

<h1> Update records </h1> <hr color="yellow">
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List of Employee Records </strong> </td>
</tr>

<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>First name</strong></td>
<td align="center"><strong>Last Name</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['fname']; ?></td>
<td><?php echo $rows['lname']; ?></td>


<td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>

<?php
mysql_close();
?>
<img src=icons/k.png>
<img src=icons/b.png>
<img src=icons/y.png>

2. Out put from above code looks as following.

3. Now, copy the below code and save as “update.php”.

<?php
$host="localhost"; 
$username="root";
$password=""; 
$db_name="mars_db"; 
$tbl_name="list"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$id=$_GET['id'];

$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Last Name</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="id" type="text" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input name="fname" type="text" id="fname" value="<?php echo $rows['fname']; ?>" size="15">
</td>
<td>
<input name="lname" type="text" id="lname" value="<?php echo $rows['lname']; ?>" size="15">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
mysql_close();
?>

4. The out put of the above file looks as below.

5. When you want to update, just click on Submit button and it will take you to next page called “update_ac.php”, refer the below code.

<?php
$id=$_POST["id"];
$fname=$_POST["fname"];
$lname=$_POST["lname"];

$host="localhost"; 
$username="root";
$password=""; 
$db_name="mars_db"; 
$tbl_name="list"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="UPDATE $tbl_name SET fname='$fname', lname='$lname' WHERE id='$id'";
$result=mysql_query($sql);

if($result)
{
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else 
{
echo "ERROR";
}
?>

6. The above code is having following output.

7. once you click on “View result” link as per above image, it will redirect to the “list_records.php”, hence you’ll have updated records.

Thank you for reading and referring my blog. Keep reading mitindia.in for latest updates and don’t forget to subscribe!

SKB

Leave a Reply

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