I have a program in PHP
that reads a xml
and inserts the data into a mysql database but when I run it inserts only empty data. Next I put the xml and the code that I am using:
XML
<?xml version="1.0" encoding="utf-8"?>
<InvoiceControl>
<InvoiceAuthorization>320001</InvoiceAuthorization>
<AuthorizationPeriod>
<StartDate>2014-01-13</StartDate>
<EndDate>2015-01-13</EndDate>
</AuthorizationPeriod>
<AuthorizedInvoices>
<Prefix>PR</Prefix>
<From>769700</From>
<To>769702</To>
</AuthorizedInvoices>
</InvoiceControl>
<InvoiceSource>
<IdentificationCode>CO</IdentificationCode>
</InvoiceSource>
<SoftwareProvider>
<ProviderID>90030</ProviderID>
<SoftwareID>a9c0758b-735b-4951</SoftwareID>
</SoftwareProvider>
<SoftwareSecurityCode>8f9d9d22d4c3381a0282e8f3beb</SoftwareSecurityCode>
</ExtensionContent>
</UBLExtension>
<UBLExtension>
<ExtensionContent />
</UBLExtension>
PHP CODE
<?php
$conn = mysqli_connect("localhost", "root", "", "facturacion");
$affectedRow = 0;
libxml_use_internal_errors ( true );
$xml = simplexml_load_file("PI-Pirelli-001-7697004318.xml") or die("Error: Cannot create object");
foreach ($xml->children() as $row) {
$DIN_INVOICE_AUTHORIZATION = $row->InvoiceAuthorization;
$DDA_START_DATE = $row->StartDate;
$DDA_END_DATE = $row->EndDate;
$DVC_PREFIX = $row->Prefix;
$DIN_FROM = $row->From;
$DIN_TO = $row->To;
$DCH_IDENTIFICATION_CODE = $row->IdentificationCode;
$DIN_PROVIDERID = $row->ProviderID;
$DVC_SOFTWAREID = $row->SoftwareID;
$DVC_SOFTWARESECURITYCODE = $row->SoftwareSecurityCode;
$sql = "INSERT INTO fed_ublextensions(DIN_INVOICE_AUTHORIZATION, DDA_START_DATE, DDA_END_DATE, DVC_PREFIX, DIN_FROM, DIN_TO, DCH_IDENTIFICATION_CODE, DIN_PROVIDERID, DVC_SOFTWAREID, DVC_SOFTWARESECURITYCODE) VALUES ('" . $DIN_INVOICE_AUTHORIZATION . "','" . $DDA_START_DATE . "','" . $DDA_END_DATE . "','" . $DVC_PREFIX . "','" . $DIN_FROM . "','" . $DIN_TO . "','" . $DCH_IDENTIFICATION_CODE . "','" . $DIN_PROVIDERID . "','" . $DVC_SOFTWAREID . "','" . $DVC_SOFTWARESECURITYCODE . "')";
$result = mysqli_query($conn, $sql);
if (! empty($result)) {
$affectedRow ++;
} else {
$error_message = mysqli_error($conn) . "n";
}
}
?>
<h2>Insert XML Data to MySql Table Output</h2>
<?php
if ($affectedRow > 0) {
$message = $affectedRow . " records inserted";
} else {
$message = "No records inserted";
}
?>
<style>
body {
max-width: 550px;
font-family: Arial;
}
.affected-row {
background: #cae4ca;
padding: 10px;
margin-bottom: 20px;
border: #bdd6bd 1px solid;
border-radius: 2px;
color: #6e716e;
}
.error-message {
background: #eac0c0;
padding: 10px;
margin-bottom: 20px;
border: #dab2b2 1px solid;
border-radius: 2px;
color: #5d5b5b;
}
</style>
<div class="affected-row">
<?php echo $message; ?>
</div>
<?php if (! empty($error_message)) { ?>
<div class="error-message">
<?php echo nl2br($error_message); ?>
</div>
<?php } ?>