Jump to content

[SOLVED] MYSQL Insert Data from PHP XML DOM


CorruptBlood

Recommended Posts

Hello everyone. I am new to the forums, so let me know if I am doing something wrong. I didn't find a "read first" post, so I am assuming I am good to go!

 

Basically, I am attempting to pull armory data from world of warcraft's site for a guild roster. I have successfully pulled the data into guildroster.xml (below), and now I am trying to insert it into MySQL.

 

roster.php:

<?php

$link = mysql_connect("#","#","#");
if (!$link)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("#", $link);

mysql_query("DELETE FROM roster_guild");

$xmlDoc = new DOMDocument();
$xmlDoc -> load( 'guildroster.xml' );

$searchNode = $xmlDoc->getElementsByTagName( "character" );

foreach( $searchNode as $character )
{
	$charactername = $character->getAttribute('name');
	$characterlevel = $character->getAttribute('level');
	$characterclass = $character->getAttribute('classId');
	$characterrace = $character->getAttribute('raceId');
	$characterrank = $character->getAttribute('rank');
	$characterrankid = $character->getAttribute('rank');

	if ($characterclass == 1) {
		$characterclass = "Warrior";
	} elseif ($characterclass == 2) {
		$characterclass = "Paladin";
	} elseif ($characterclass == 3) {
		$characterclass = "Hunter";
	} elseif ($characterclass == 4) {
		$characterclass = "Rogue";
	} elseif ($characterclass == 5) {
		$characterclass = "Priest";
	} elseif ($characterclass == 6) {
		$characterclass = "Death Knight";
	} elseif ($characterclass == 7) {
		$characterclass = "Shaman";
	} elseif ($characterclass ==  {
		$characterclass = "Mage";
	} elseif ($characterclass == 9) {
		$characterclass = "Warlock";
	} elseif ($characterclass == 11) {
		$characterclass = "Druid";
	}

	if ($characterrace == 10) {
		$characterrace = "Blood Elf";
	} elseif ($characterrace == 2) {
		$characterrace = "Orc";
	} elseif ($characterrace == 6) {
		$characterrace = "Tauren";
	} elseif ($characterrace ==  {
		$characterrace = "Troll";
	} elseif ($characterrace == 5) {
		$characterrace = "Undead";
	}

	if ($characterrank == 0) {
		$characterrank = "Head Captain";
	} elseif ($characterrank == 1) {
		$characterrank = "Captain";
	} elseif ($characterrank == 2) {
		$characterrank = "Lieutenant";
	} elseif ($characterrank == 3) {
		$characterrank = "Soul Reaper";
	} elseif ($characterrank == 4) {
		$characterrank = "Human";
	} elseif ($characterrank == 5) {
		$characterrank = "Mod Soul";
	}

	mysql_query("INSERT INTO roster_guild (NAME, LEVEL, CLASS, RANK, RACE, RANKID) VALUES ('$charactername','$characterlevel','$characterclass','$characterrank','$characterrace','$characterrankid')");
}

mysql_close($link);

?>

 

guildroster.xml:

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/Users/Shadow/Desktop/Roster/roster.xsl"?>
  <tabInfo subTab="guildRoster" tab="guild" tabGroup="guild" tabUrl="r=Mal%27Ganis&gn=Ryoka"/>
  <guildInfo>
    <guildHeader battleGroup="Stormstrike" faction="1" members="18" name="Ryoka" nameUrl="Ryoka" realm="Mal'Ganis" realmUrl="Mal%27Ganis" url="r=Mal%27Ganis&gn=Ryoka">
      <emblem emblemBackground="6" emblemBorderColor="14" emblemBorderStyle="3" emblemIconColor="15" emblemIconStyle="79"/>
    </guildHeader>
    <guild>
      <members memberCount="18">
        <character achPoints="60" classId="1" genderId="0" level="17" name="Roflrage" raceId="5" rank="5" url="r=Mal%27Ganis&n=Roflrage"/>
        <character achPoints="0" classId="4" genderId="1" level="72" name="Jyyn" raceId="5" rank="4" url="r=Mal%27Ganis&n=Jyyn"/>
        <character achPoints="1570" classId="11" genderId="1" level="80" name="Moobaalalala" raceId="6" rank="3" url="r=Mal%27Ganis&n=Moobaalalala"/>
        <character achPoints="170" classId="6" genderId="0" level="61" name="Kizuke" raceId="10" rank="5" url="r=Mal%27Ganis&n=Kizuke"/>
        <character achPoints="2245" classId="6" genderId="1" level="80" name="Reyvyn" raceId="5" rank="3" url="r=Mal%27Ganis&n=Reyvyn"/>
        <character achPoints="0" classId="8" genderId="1" level="80" name="Jynn" raceId="5" rank="3" url="r=Mal%27Ganis&n=Jynn"/>
        <character achPoints="50" classId="2" genderId="0" level="10" name="Kyoroku" raceId="10" rank="5" url="r=Mal%27Ganis&n=Kyoroku"/>
        <character achPoints="330" classId="2" genderId="1" level="80" name="Hinamori" raceId="10" rank="0" url="r=Mal%27Ganis&n=Hinamori"/>
        <character achPoints="250" classId="4" genderId="1" level="80" name="Blint" raceId="5" rank="1" url="r=Mal%27Ganis&n=Blint"/>
        <character achPoints="320" classId="6" genderId="0" level="80" name="Roflplague" raceId="10" rank="1" url="r=Mal%27Ganis&n=Roflplague"/>
        <character achPoints="0" classId="2" genderId="1" level="72" name="Yuyaa" raceId="10" rank="4" url="r=Mal%27Ganis&n=Yuyaa"/>
        <character achPoints="2960" classId="7" genderId="1" level="80" name="Helaena" raceId="8" rank="1" url="r=Mal%27Ganis&n=Helaena"/>
        <character achPoints="10" classId="3" genderId="1" level="78" name="Ambarel" raceId="10" rank="5" url="r=Mal%27Ganis&n=Ambarel"/>
        <character achPoints="0" classId="5" genderId="1" level="80" name="Lirayelle" raceId="5" rank="3" url="r=Mal%27Ganis&n=Lirayelle"/>
        <character achPoints="110" classId="8" genderId="0" level="21" name="Anarez" raceId="5" rank="4" url="r=Mal%27Ganis&n=Anarez"/>
        <character achPoints="150" classId="6" genderId="0" level="61" name="Curbchecked" raceId="5" rank="5" url="r=Mal%27Ganis&n=Curbchecked"/>
        <character achPoints="2670" classId="6" genderId="1" level="80" name="Nýx" raceId="10" rank="3" url="r=Mal%27Ganis&n=N%C3%BDx"/>
      </members>
    </guild>
  </guildInfo>

 

The problem is the only character that actually gets inserted is:

ID: 0

NAME: Roflrage

LEVEL: 17

CLASS: Warrior

RANK: Mod Soul

RACE: Undead

RANKID: 5

 

No other character gets inserted, I only have 1 record in the database, can someone explain what I am doing wrong? I need each of the characters above to be inserted in its' own record.

 

Thanks!

-CorruptBlood

 

EDIT: If I echo the $charactername, $characterlevel, etc... in the foreach loop I actually will get each of the character's names, level, etc... It is only when I attempt to insert. So it looks like the XML is being read correctly. (correct me if I am wrong.)

The problem is the only character that actually gets inserted is:

ID: 0

NAME: Roflrage

LEVEL: 17

CLASS: Warrior

RANK: Mod Soul

RACE: Undead

RANKID: 5

 

Actually not ;) you forgot to add an auto_increment to your primary id so it inserts the first row and throws sql error on all others something like: duplicate entry '0' for key '..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.