Solved

Uploading XML to Mysql using php

Posted on 2011-03-23
4
480 Views
Last Modified: 2012-05-11
Hello Everyone

I am trying to upload a xml file into Mysql using php. The xml file is layed out pretty easy:

<Member>
<Name>Bob</Name>
<other stuff>
</Member>
<Member>
<Name>Jim</Name>
<other stuff>
</Member>

I thought this would be:

$xml = simplexml_load_string($data);
$charname = (string) $xml->Name;
mysql_query("INSERT INTO aTable ( Date,charname, ) VALUES ('$Date','$charname', ') ");
?>

Which inserts one line.I want to be able to insert using the date/time from now and insert Jim and Bob. Could someone give me a example of the Date snippet and how to make it loop from each <member> to the next?





0
Comment
Question by:JCS_Unlimited
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
tsmgeek earned 500 total points
ID: 35203731
<?php

$data="
<Members>
  <Member>
    <Name>Bob</Name>
  </Member>
  <Member>
    <Name>Jim</Name>
  </Member>
</Members>
";


$xml = simplexml_load_string($data);
foreach($xml->Member as $member){
  $charname = $member->Name;
  $date = date( 'Y-m-d H:i:s' );
  mysql_query("INSERT INTO aTable ( Date,charname ) VALUES ('$Date','$charname');");
}

?>

Open in new window

0
 

Author Comment

by:JCS_Unlimited
ID: 35203801

Thank you tsmgeek,

What if I have a lot more people than just Bob and Jim? and the length of people gets longer or shorter sometimes Will this loop through each of the member sections?
0
 
LVL 5

Expert Comment

by:tsmgeek
ID: 35203858
yes it loops though each member till it gets to the end of the file, all elements under each member are accesable though the variable $member
0
 

Author Comment

by:JCS_Unlimited
ID: 35210303
Worked Great Tsmgeek. Thansk for the help
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question