Solved

Uploading XML to Mysql using php

Posted on 2011-03-23
4
477 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now