Insert New Entry In XML File Using 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

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

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">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href=""/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <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">
                    <input type="text" class="form-control" name="firstname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="lastname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="address" required="required"/>
                <center><button class="btn btn-primary" name="insert">Insert</button></center>
        <div class="col-md-8">
            <table class="table table-bordered" >	
                <thead class="alert-info">
                        $xml = simplexml_load_file('member.xml');
                        foreach($xml->member as $member){
                            echo '

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.

            $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());


<?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!

Sharing is caring!

Facebook Comments

Leave a Reply