Reputation: 123
I am currently working on a highly convoluted XML-document, trying to parse some of its contents into PhpMyadmin using PHP. So far, I have done the following:
try {
if(isset($_POST['buttonImport'])) {
move_uploaded_file($_FILES['xmlFile']['tmp_name'],
'daten/'.$_FILES['xmlFile']['name']);
$kopf = simplexml_load_file('daten/'.$_FILES['xmlFile']['name']);
$filename=pathinfo($_FILES['xmlFile']['name'], PATHINFO_FILENAME);
foreach($kopf as $insert){
$stmt = $conn->prepare('insert into
Database(uid, id, bez, file, xmlns, version, versdate, xdate, time, progsystem, progname, nameprj, lblprj, cur, curlbl, dp, name1, name2, name3, street, pcode, city, country, phone, fax, email, vatid)
values(:uid, :lfnr, :bez, :file, :xmlns, :version, :versdate, :xdate, :time, :progsystem, :progname, :nameprj, :lblprj, :cur, :curlbl, :dp, :name1, :name2, :name3, :street, :pcode, :city, :country, :phone, :fax, :email, :vatid)');
$stmt->bindValue('bez', $insert->Version);
$stmt->bindValue('file', $filename);
//etc, until...
$stmt->execute();
}
}
} catch (PDOException $e) {
echo "An error has occured: . $e->getMessage();
}
?>
This works right up until the node "NamePrj", which is encapsulated within another node that I don't need to add into the database:
<PrjInfo xmlns=""><NamePrj>[CENSORED]</NamePrj><LblPrj>[CENSORED]</LblPrj><Cur>EUR</Cur><CurLbl>[CENSORED]</CurLbl></PrjInfo>
What I find odd about this is the fact that the nodes before this have been similarly encapsulated:
<GAEBInfo xmlns=""><Version>[CENSORED]</Version><VersDate>[CENSORED]</VersDate><Date>[CENSORED]</Date><Time>[CENSORED]</Time><ProgSystem>[CENSORED]</ProgSystem><ProgName>[CENSORED]</ProgName></GAEBInfo>
And help or input would be greatly appreciated. I have not worked with PHP in combination with XML and MySQL before, so there might be some terribly easy thing that I'm overlooking here.
EDIT: I have since tried something based on the solution given here:
foreach ($insert->PrjInfo as $proj) {
$stmt->bindValue('nameprj', $proj->NamePrj);
$stmt->bindValue('lblprj', $proj->LblPrj);
$stmt->bindValue('cur', $proj->Cur);
$stmt->bindValue('curlbl', $proj->CurLbl);
}
However, this returns the following: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in [LOCATION]
Upvotes: 0
Views: 50
Reputation: 1039
I think the problem here is the foreach
. It appears as though your sql statement is expecting all the values in one go but you loop the outer nodes so the first loop you have some of the nodes and then the next time around the rest.
If you know the outer nodes then remove the foreach
loop altogether and reference the full node path. e.g.
$stmt->bindValue('nameprj', $kopf->PrjInfo->NamePrj);
Upvotes: 1