Solved

Uploading XML to Mysql using php

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
echo button 13 45
mysql Encryption with PHP 8 47
Page showing diff display 4 19
uploading multiple image with php 14 17
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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

18 Experts available now in Live!

Get 1:1 Help Now