PHP - Update Data in XML File

Submitted by: 
Visitors have accessed this post 228 times.

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

And, this is the link for the bootstrap that i used for the layout design

Lastly, this is the link for the jquery that i used in this tutorial

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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Update Data in XML File</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <table class="table table-bordered table-striped" style="margin-top:20px;">
  19. <thead class="alert-info">
  20. <th>Member ID</th>
  21. <th>Firstname</th>
  22. <th>Lastname</th>
  23. <th>Address</th>
  24. <th>Action</th>
  25. </thead>
  26. <tbody>
  27. <?php
  28. $file = simplexml_load_file('members.xml');
  29. foreach($file->member as $row){
  30. ?>
  31. <tr>
  32. <td><?php echo $row->mem_id; ?></td>
  33. <td><?php echo $row->firstname; ?></td>
  34. <td><?php echo $row->lastname; ?></td>
  35. <td><?php echo $row->address; ?></td>
  36. <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>
  37. <div class="modal fade" id="edit_<?php echo $row->mem_id; ?>" aria-hidden="true">
  38. <div class="modal-dialog">
  39. <div class="modal-content">
  40. <form method="POST" action="update.php">
  41. <div class="modal-header">
  42. <center><h4 class="modal-title" id="myModalLabel">Edit Member</h4></center>
  43. </div>
  44. <div class="modal-body">
  45. <div class="col-md-2"></div>
  46. <div class="col-md-8">
  47. <div class="form-group">
  48. <label >Member ID</label>
  49. <input type="text" class="form-control" name="id" value="<?php echo $row->mem_id; ?>" readonly="readonly" />
  50. </div>
  51. <div class="form-group">
  52. <label >Firstname:</label>
  53. <input type="text" class="form-control" name="firstname" value="<?php echo $row->firstname; ?>">
  54. </div>
  55. <div class="form-group">
  56. <label>Lastname:</label>
  57. <input type="text" class="form-control" name="lastname" value="<?php echo $row->lastname; ?>">
  58. </div>
  59. <div class="form-group">
  60. <label>Address:</label>
  61. <input type="text" class="form-control" name="address" value="<?php echo $row->address; ?>">
  62. </div>
  63. </div>
  64. </div>
  65. <br style="clear:both;"/>
  66. <div class="modal-footer">
  67. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  68. <button name="edit" class="btn btn-warning"><span class="glyphicon glyphicon-save"></span> Update</a>
  69. </div>
  70. </form>
  71. </div>
  72. </div>
  73. </div>
  74. </tr>
  75. <?php
  76. }
  78. ?>
  79. </tbody>
  80. </table>
  81. </div>
  82. <script src="js/jquery-3.2.1.min.js"></script>
  83. <script src="js/bootstrap.min.js"></script>
  84. </body>
  85. </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.

  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>


  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. }
  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!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.