PHP - Update Data in XML File

In this tutorial we will create a Update Data in XML File using PHP. This code will launch a bootstrap modal to update a specific XML data in the table. The code use file_put_contents() to save the XML data after the php loop assign the designate key value of POST data. This is a user-friendly kind of program feel free to modify it. We will be using XML as a markup language that utilize in php as a HTML data. It is designed to store and transport data that can be manipulated within the local server.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, this is the link for the jquery that i used in this tutorial https://jquery.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Update Data in XML File</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <table class="table table-bordered table-striped" style="margin-top:20px;">
  18. <thead class="alert-info">
  19. <th>Member ID</th>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Address</th>
  23. <th>Action</th>
  24. </thead>
  25. <tbody>
  26. <?php
  27. $file = simplexml_load_file('members.xml');
  28. foreach($file->member as $row){
  29. ?>
  30. <tr>
  31. <td><?php echo $row->mem_id; ?></td>
  32. <td><?php echo $row->firstname; ?></td>
  33. <td><?php echo $row->lastname; ?></td>
  34. <td><?php echo $row->address; ?></td>
  35. <td><a href="#edit_<?php echo $row->mem_id; ?>" data-toggle="modal" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Edit</a></td>
  36. <div class="modal fade" id="edit_<?php echo $row->mem_id; ?>" aria-hidden="true">
  37. <div class="modal-dialog">
  38. <div class="modal-content">
  39. <form method="POST" action="update.php">
  40. <div class="modal-header">
  41. <center><h4 class="modal-title" id="myModalLabel">Edit Member</h4></center>
  42. </div>
  43. <div class="modal-body">
  44. <div class="col-md-2"></div>
  45. <div class="col-md-8">
  46. <div class="form-group">
  47. <label >Member ID</label>
  48. <input type="text" class="form-control" name="id" value="<?php echo $row->mem_id; ?>" readonly="readonly" />
  49. </div>
  50. <div class="form-group">
  51. <label >Firstname:</label>
  52. <input type="text" class="form-control" name="firstname" value="<?php echo $row->firstname; ?>">
  53. </div>
  54. <div class="form-group">
  55. <label>Lastname:</label>
  56. <input type="text" class="form-control" name="lastname" value="<?php echo $row->lastname; ?>">
  57. </div>
  58. <div class="form-group">
  59. <label>Address:</label>
  60. <input type="text" class="form-control" name="address" value="<?php echo $row->address; ?>">
  61. </div>
  62. </div>
  63. </div>
  64. <br style="clear:both;"/>
  65. <div class="modal-footer">
  66. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  67. <button name="edit" class="btn btn-warning"><span class="glyphicon glyphicon-save"></span> Update</a>
  68. </div>
  69. </form>
  70. </div>
  71. </div>
  72. </div>
  73. </tr>
  74. <?php
  75. }
  76.  
  77. ?>
  78. </tbody>
  79. </table>
  80. </div>
  81. <script src="js/jquery-3.2.1.min.js"></script>
  82. <script src="js/bootstrap.min.js"></script>
  83. </body>
  84. </html>

Creating the Main Function

This code contains the main function of the application. This code will automatically update the xml file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. members.xml
  1. <?xml version="1.0"?>
  2. <members>
  3. <member>
  4. <mem_id>1</mem_id>
  5. <firstname>John</firstname>
  6. <lastname>Smith</lastname>
  7. <address>New York</address>
  8. </member>
  9. <member>
  10. <mem_id>2</mem_id>
  11. <firstname>Regina</firstname>
  12. <lastname>Wilson</lastname>
  13. <address>Jurassic Park</address>
  14. </member>
  15. </members>
update.php
  1. <?php
  2. if(ISSET($_POST['edit'])){
  3. $members = simplexml_load_file('members.xml');
  4. foreach($members->member as $member){
  5. if($member->mem_id == $_POST['id']){
  6. $member->firstname = $_POST['firstname'];
  7. $member->lastname = $_POST['lastname'];
  8. $member->address = $_POST['address'];
  9. break;
  10. }
  11. }
  12.  
  13. file_put_contents('members.xml', $members->asXML());
  14. header("location: index.php");
  15. }
  16. ?>
There you have it we successfully created Update Data in XML File using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments

Submitted byNo One's King (not verified)on Sun, 06/14/2020 - 10:38

Thanks!

Add new comment