• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

Uploading XML to Mysql using php

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
JCS_Unlimited
Asked:
JCS_Unlimited
  • 2
  • 2
1 Solution
 
tsmgeekCommented:
<?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
 
JCS_UnlimitedAuthor Commented:

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
 
tsmgeekCommented:
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
 
JCS_UnlimitedAuthor Commented:
Worked Great Tsmgeek. Thansk for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now