Insert New Entry in XML File using PHP

By CampCodes Administrator

Updated on:

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!

READ ALSO:   Sort MySQL Data Using AngularJS in PHP

array to xml php c# xml parser convert mysql to xml using php convert php to xml create xml file in php simplexml echo xml in php generate and download xml file in php how append html in php? how do i add data to an xml file? how parse xml in php? how to create dynamic xml file in php how to create xml file in php example how to display xml data in php how to get attribute value in xml using php how to get data from xml file in php how to parse xml in php insert new entry in xml file using php parse xml with php php array to xml php array to xml with attributes php convert xml to array with attributes php convert xml to object php create xml php create xml file php create xml file and save php create xml string php display xml in browser php domdocument php echo xml php echo xml string php edit xml file php get xml from url php load xml php load xml file php load xml from string php load xml from url php new simplexmlelement php output xml php output xml file php parse simplexmlelement object php parse xml php parse xml file php parse xml from url php parse xml string php parse xml to array php parsing xml file php read xml php read xml file php read xml file into array php read xml from url php return xml response php save xml string to file php search xml php simplexml add child with attribute php simplexml example php simplexml get attribute by name php simplexml load file php simplexml_load_file php simplexml_load_string php simplexmlelement get value php to xml php to xml converter php tutorial php tutorials php with xml php write xml php write xml file php xml php xml api php xml appendchild php xml class php xml document php xml editor php xml get attribute php xml get element by attribute value php xml library php xml manipulation php xml parse php xml parser php xml reader php xml request php xml to array with attributes php xml to associative array php xml to json with attributes php xml to object mapping php xml tree read xml file in php read xml in php read xml php read xml using php reading xml file in php reading xml file using php save xml file php simplexml simple xml php simplexml simplexml addchild simplexml get attribute simplexml_load_file in php simplexml_load_file php 7 simplexml_load_file url simplexml_load_string returns empty simplexmlelement example simplexmlelement get value simplexmlelement remove child using simplexml what is dom what is xml tag insertion? what is xml used for xml xml api in php xml example xml export php xml file xml in php xml parser php xml program example with output xml to array php xml to html php xml tutorial

Leave a Comment