0

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]

2
  • 1
    Please could add any information about what errors you are getting and also the code that reads the NamePrj from the XML. Commented Aug 19, 2020 at 12:58
  • Thanks for replying! I didn't get an error until recently (see my edit), just empty variables for everything after the variable "ProgName". The code reading NamePrj from the XML is identical to the other bindValues in my first Code Snippet, which has been edited down for the sake of not posting too long a program. Commented Aug 19, 2020 at 13:09

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Worked exactly like this. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.