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;
Code of update.php by using MySQL and without MySQLi.
Database
- index.php
- update.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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="update.php<?php echo '?id='.$books_rows['BookID']; ?>">Edit</a></td> </tr> <?php }?> </table> </body></html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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'); } ?> |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $conn = mysqli_connect('localhost','root','','publisher'); ?> <html><body> <table border="1"> <?php $books_query=mysqli_query($conn, "select * from books"); while($books_rows=mysqli_fetch_array($books_query)){ ?> <tr> <td><?php echo $books_rows['BookID'] ; ?></td> <td><?php echo $books_rows['BookTitleAttribute'] ; ?></td> <td><a href="update.php<?php echo '?id='.$books_rows['BookID']; ?>">Edit</a></td> </tr> <?php }?> </table> </body></html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $conn = mysqli_connect('localhost','root','','publisher'); $id=$_GET['id']; ?> <html><body> <form method="post"> <table> <?php $books_query=mysqli_query($conn,"select * from books where BookID='$id'"); $books_rows=mysqli_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']; mysqli_query($conn,"update books set BookTitleAttribute='$TitleVariable' where BookID='$id'"); header('location:index.php'); } ?> |