How To update Record In PHP And MYSQL Database?

How To update Record In PHP And MYSQL Database?

In this tutorial, we will try to learn the about the updating records in the database table in PHP and MySQL.

Code of updating of records in the database table in PHP and MySQL.

We can have two pages;

  1. index.php
  2. update.php

Code of index.php

<?php
mysql_select_db('publisher',mysql_connect('localhost','root',''));
?>
<html><body>
<table border="1">
<?php
$books_query=mysql_query("select * from books");
while($books_rows=mysql_fetch_array($books_query)){
?>
<tr>
<td><?php echo $books_rows['BookID'] ; ?></td>
<td><?php echo $books_rows['BookTitleAttribute'] ; ?></td>
<td><a href="edit.php<?php echo '?id='.$books_rows['BookID']; ?>">Edit</a></td>
</tr>
<?php }?>
</table>
</body></html>

Code of update.php

<?php
mysql_select_db('publisher',mysql_connect('localhost','root','')); 
$id=$_GET['id'];
?>
<html><body>
<form method="post">
<table>
<?php $books_query=mysql_query("select * from books where BookID='$id'");
$books_rows=mysql_fetch_array($books_query);
?>
<tr><td>Title:</td><td><input type="text" name="TextBoxForTitle" value="<?php echo $books_rows['BookTitleAttribute'];  ?>"></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="save"></td></tr>
</table></form></body></html>
<?php 
if (isset($_POST['submit'])){
$TitleVariable=$_POST['TextBoxForTitle'];
 mysql_query("update books set BookTitleAttribute='$TitleVariable' where BookID='$id'");
header('location:index.php');
}
?>

Database

Database insert delete view update in php mysql

Figure: Database for update in PHP MySQL

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

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