Skip to main content

Uploading XML file into PHP

If you want to upload XML file into PHP, this tutorial will teach you first to make XML file and then mechanism to upload into your database using PHP.
To Download full files, Click Here 

First start with creating XML documents



1) Open Microsoft Excel and make a table like below:


2) Save file as Excel Workbook as below.


3) Again save as the file in XML Spreadsheet 2003.




Now your XML file is ready to be uploaded. If we need to edit anything then open the excel file and edit and again save in xml file. Don't edit xml file and save.

But we are not finished yet.

For uploading XML. Let's make a database and a table inside it.

1) Create a database called xml.

2) Create a table called tblmembers.

CREATE TABLE IF NOT EXISTS `tblmembers` (
  `mem_id` int(5) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `address` varchar(150) NOT NULL,
  `email_id` varchar(150) NOT NULL,
  `mob_no` varchar(100) NOT NULL,
  `status` int(3) NOT NULL,
  PRIMARY KEY (`mem_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


Okay, now we are done. Now lets move to php part

1) Create a php file and make a table to display uploader.

<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<table align="center" width="300" border="0" bgcolor="#F3F3F3">
<tr>
    <td>Upload xml file below:</td>
    </tr>
    <tr>
        <td> <input type="file" name="file" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="submit" value="Upload" /></td>
            </tr>
</table>
</form>


There's nothing new. Just we have created a table with file input and a submit button.

2) Now on the top of the document. Write these codes.

<?php

//connecting to the database and selecting table
$conn=mysql_connect("localhost","root","");
mysql_select_db("xml",$conn);

//error reporting off
error_reporting(0);

//creating an array
$data = array();

//after submit button is clicked
if(isset($_POST['submit']))
{
//creating function
function add_person($name,$address,$email_id,$mob_no,$status )
{
global $data;

$data []= array(    
'name' => $name,
'address' => $address,
'email_id' => $email_id, 
'mob_no'=>$mob_no,
'status'=>$status
);
}

if ( $_FILES['file']['tmp_name'] )
{
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
$first_row = true;
foreach ($rows as $row)
{
    //ignoring first line of the xml file which will remove to enters the titles like Name, Address,etc.
if ( !$first_row )
{

$name = "";
$address= "";
$email_id = "";
$mob_no = ""; 
$status = "";

$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{ 
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;

if ( $index == 1 ) $name = $cell->nodeValue;
if ( $index == 2 ) $address = $cell->nodeValue;
if ( $index == 3 ) $email_id = $cell->nodeValue;
if ( $index == 4 ) $mob_no= $cell->nodeValue;
if ( $index == 5 )$status = $cell->nodeValue;


$index += 1;
}
add_person($name,$address,$email_id,$mob_no,$status);
}
$first_row = false;
}
}
?>
<html>
<body>

<table border="1" align="center">
<tr>
<td colspan="5"><?php echo 'Your Data has been Successfully uploaded'  ; ?></td>
<tr>
<th>Name</th>
<th>Address</th>
<th>Email id</th>
<th>Mobile no</th>
<th>Status</th>
</tr>
<?php
$counterZero = 0;
$getTitle  = "";
    foreach( $data as $row ) {
    //connect to the data base and insert these data in the table
    if($counterZero==0)
    {
        $getTitle = $row['name'];

    }
    $query_bulk=mysql_query("INSERT INTO tblmembers      (
                                            name,
                                            address,
                                            email_id,
                                            mob_no,
                                            status
                                            ) values  (
                                            '".$row['name']."',
                                            '".$row['address']."',
                                            '".$row['email_id']."',
                                            '".$row['mob_no']."',
                                            '".$row['status']."' ) ") or
                                            
                                        die(mysql_error());    
/*
(type_id,brand_id,name,alias,product_code,price,size,color,specification)
*/
?>
<tr>
<td><?php echo( $row['name'] ); ?></td>
<td><?php echo( $row['address'] ); ?></td>
<td><?php echo( $row['email_id'] ); ?></td>
<td><?php echo( $row['mob_no'] ); ?></td>
<td><?php echo( $row['status'] ); ?></td>
</tr>
<?php  ++$counterZero;
}


//now post-insert into the 
mysql_close($con);
?>
<!--</table>-->
<?php } ?>
Now save and run the code. It will work like magic.

Comments

Popular posts from this blog

MySQL Connection

Connection with MySQL Database Before accessing database, you must create a connection to the database Syntax: mysql_connect(servername,username,password); where, servername specifies the server to connect to. Default value is “localhost” username specifies the username to log in with. Default value is the name of the user that owns the server process. To connect offline we use username “root”. password specifies the password to log in with. Default is “” Code : Creating connection to the database and selecting the required database <?php $con = mysql_connect(“localhost”,”root”,”"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } else{ mysql_select_db(“test”, $con) }; ?> Here, we have store connection in a variable called $con and trap error using die function. Closing connection The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function: <?php $con = mysql_conne...

Join in MySQL

Join clause is used to combine rows from two or more tables depending upon common field between them. There are different kinds of Joins. The most common and useful Join is Inner Join. Inner Join: It will return all rows from multiple tables where the condition is met.

Div and span

Div: Div <div> tag is that it divides the HTML document into sections. Proper usage of the <div> tag is fundamental to good HTML coding, the <div> tag is one of the most powerful tools available to a web developer. Span: Span <span> is  in-line level elements, they only span across small amounts of content, typically words or phrases. For example: a <span> tag might be used to make a word red in color or to give it an underline.