This topic is locked
[SOLVED]

How to Upload XML data from multiple files into MySQL

4/30/2021 7:08:12 AM
PHPRunner General questions
E
exora author

I want to upload several files (fileName1, fileName2, fileName3, etc ...) which contain xml data into mysql table (tblName) at once.
The xml data structure is as follows:

<tagName>
<field1></field1>
<field2></field2>
<field3></field3>
<field4></field4>
</tagName>

Actually, I want to upload these files directly from the local computer to the web server, but I'm still learning how it applies to custom button.
For simplicity, while I use the data upload menu by creating a new table called tblFile (id, nmfile) even though I don't actually need these files to be on the web server.

In the before record added event, I added the following:

$fileArray = my_json_decode($values["nmfile"]);

for($i = 0; $i < count($fileArray); $i++){
$fileuploaded = $fileArray[$i]["name"];
$xml = simplexml_load_file($fileuploaded);

foreach ($xml->children() as $row) {
$field1 = $row->field1;
$field2 = $row->field2;
$field3 = $row->field3;
$field4 = $row->field4;

$sql = "INSERT INTO tblName(field1, field2, field3, field4) VALUES ('".$field1."','".$field2."','".$field3."','".$field4."')";
$rs = DB::Query($sql);
}
}

Problem:
After the application is run, the code above only manages to move data from just one file into myssql (tblName), which is the xml data in fileName1. Meanwhile, the xml data in the table fileName2, fileName3, etc. ... is not uploaded to mysql.

Is there anything that was missed in the above event
Any solution would be appreciated.

admin 4/30/2021

The structure of the code looks good to me.

I guess you need to print some info on the page to understand what is happening like name of the files being processed, SQL strings etc.

E
exora author 4/30/2021

It turns out that I missed something during the testing. I just copied fileName1 to fileName2 and fileName3 without changing the xml data inside. So the data recorded in the mysql table is only xml data from filename1 because the data in fileName2 and fileName3 are the same and are considered duplicate data.
After the xml data in fileName2 and fileName3 was adjusted, everything worked normally.

Thank you Sergey for the response.