Insert New Entry in XML File using PHP

June 19, 2020
PHP
php insert new entry in xml

In this tutorial, we will create an Insert New Entry In XML File using PHP. This code will dynamically insert an additional entry to the XML file when the user submits the input form. The code use file_put_contents() function to add new data to the XML file by overwriting the existing data. This is a user-friendly kind of program feel free to modify it.

We will be using XML as a markup language that defines a set of rules for encoding documents in a format that is both readable in a certain way. 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 runs PHP scripts. Here’s the link for the 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/.

Creating The Interface

This is where we will create a simple form for our application. To create the structures, copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Insert New Entry In XML File</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST" action="insert.php">
                <div class="form-group">
                    <label>Firstname</label>
                    <input type="text" class="form-control" name="firstname" required="required"/>
                </div>
                <div class="form-group">
                    <label>Lastname</label>
                    <input type="text" class="form-control" name="lastname" required="required"/>
                </div>
                <div class="form-group">
                    <label>Address</label>
                    <input type="text" class="form-control" name="address" required="required"/>
                </div>
                <center><button class="btn btn-primary" name="insert">Insert</button></center>
            </form>
        </div>
        <div class="col-md-8">
            <table class="table table-bordered" >	
                <thead class="alert-info">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Address</th>
                    </tr>
                </thead>
                <tbody>
 
                    <?php
                        $xml = simplexml_load_file('member.xml');
                        foreach($xml->member as $member){
                            echo '
                                <tr>
                                    <td>'.$member->firstname.'</td>
                                    <td>'.$member->lastname.'</td>
                                    <td>'.$member->address.'</td>
                                </tr>
                            ';
                        }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</body>	
</html>

Creating the Main Function

This code contains the main function of the application. This code will add a new data in 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.
insert.php

<?php
    if(ISSET($_POST['insert'])){
 
        if(file_exists("member.xml")){
            $members = simplexml_load_file('member.xml');
            $member = $members->addChild('member');
            $member->addChild('firstname', $_POST['firstname']);
            $member->addChild('lastname', $_POST['lastname']);
            $member->addChild('address', $_POST['address']);
            file_put_contents('member.xml', $members->asXML());
 
            header('location:index.php');
        }
    }
 
?>

member.xml

<?xml version="1.0"?>
<members><member><firstname>John</firstname><lastname>Smith</lastname><address>New York</address></member><member><firstname>Claire </firstname><lastname>Temple</lastname><address>Racoon City</address></member></members>

There you have it we successfully created Insert New Entry 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!

Facebook Comments
https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published.