I have an XML file like :
<competition competition_id="9"> <season season_id="11643"> <round round_id="31545" name="Regular Season"> <match match_id="2054191" team_A_name="Bayern München" team_B_name="Hamburger SV" /> <match match_id="2054193" team_A_name="Bayer Leverkusen" team_B_name="Hoffenheim" /> </round> </season> </competition>
No i try to save from this file into my Data Base like :
$xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA); $competitions = $xml->xpath('//competition'); $seasons = $xml->xpath('//season'); $rounds = $xml->xpath('//round'); $matchs = $xml->xpath('//match'); foreach ($competitions as $competition) { foreach ($seasons as $season) { foreach ($rounds as $round) { foreach ($matchs as $match) { $competition_id = $competition->attributes()->competition_id; $season_id = $competition->season->attributes()->season_id; $round_id = $competition->season->round->attributes()->round_id; $match_id = $competition->season->round->match->attributes()->match_id; $matches_insert = mysql_query("INSERT INTO `matches` (`competition_id` ,`season_id` ,`round_id` ,`match_id` ,`team_A_name`,`team_B_name`) VALUES ('$competition_id', '$season_id', '$round_id', '$match_id', '$team_A_name', '$team_B_name');"); } } } }
This code is save only first record, and duplicate it two time.
How can save this tow records without duplicate?
No comments:
Post a Comment