I'm having an issue with trying to insert my XML data into my database. I've spent the past two days researching on google but I'm still struggling. If anyone could help that would be most appreciated. Thank you very much.
I get the following error messages when I try to insert the data:
Notice: Trying to get property of non-object in insert_into_database.php on line 13
Fatal error: Call to a member function item() on null in insert_into_database.php on line 13
insert_into_database.php
<?php
$db = new PDO('mysql:host=localhost;dbname=data', 'root', '');
$xmldoc = new DOMDocument();
$xmldoc->load('data.xml');
$xmldata = $xmldoc->getElementsByTagName('group');
$xmlcount = $xmldata->length;
for ($i=0; $i < $xmlcount; $i++) {
$id = $xmldata->item($i)->getElementsByTagName('id')->item(0)->childNodes->item(0)->nodeValue;
$group = $xmldata->item($i)->getElementsByTagName('group')->item(0)->childNodes->item(0)->nodeValue;
$category = $xmldata->item($i)->getElementsByTagName('category')->item(0)->childNodes->item(0)->nodeValue;
$question = $xmldata->item($i)->getElementsByTagName('question')->item(0)->childNodes->item(0)->nodeValue;
$a = $xmldata->item($i)->getElementsByTagName('a')->item(0)->childNodes->item(0)->nodeValue;
$b = $xmldata->item($i)->getElementsByTagName('b')->item(0)->childNodes->item(0)->nodeValue;
$c = $xmldata->item($i)->getElementsByTagName('c')->item(0)->childNodes->item(0)->nodeValue;
$d = $xmldata->item($i)->getElementsByTagName('d')->item(0)->childNodes->item(0)->nodeValue;
$stmt = $db->prepare("insert into xml values(?,?,?,?,?,?,?,?)");
$stmt->bindParam(1,$group);
$stmt->bindParam(2,$category);
$stmt->bindParam(3,$question);
$stmt->bindParam(4,$a);
$stmt->bindParam(5,$b);
$stmt->bindParam(6,$c);
$stmt->bindParam(7,$d);
$stmt->execute();
printf($name.'<br />');
}
?>
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<questions>
<group name="Question Group 1">
<id>1</id>
<category>Category A</category>
<question name="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
</group>
<group name="Question Group 2">
<id>2</id>
<category>Category B</category>
<question name="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
</group>
<group name="Question Group 3">
<id>3</id>
<category>Category C</category>
<question name="Question 1" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 2" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 3" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 4" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
<question name="Question 5" a="Answer 1" b="Answer 2" c="Answer 3" d="Answer 4"/>
</group>
</questions>