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.
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.
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
Post a Comment