您好,我尝试将 xml 文件中的一些数据插入 mysql,我需要将数据代码插入到引用列中,并且数量列 Xml 中的数据 qta 是:
<dispo>
<codice>CA031BL</codice>
<arrivi>
<arrivo>
<maga>B</maga>
<data>2020-04-01</data>
<qta>0</qta>
</arrivo>
</arrivi>
<codice>CA031BL</codice>
<arrivi>
<arrivo>
<data>2020-04-01</data>
<maga>C</maga>
<qta>30</qta>
</arrivo>
</dispo>
在 php 文件中我插入了这个:连接到 mysql db work simplexml load 如果我在 $quantity 和 $ 引用中插入数字,它似乎工作但我如何插入 xml 文件的右列?
<?php
$conn = mysqli_connect("host", "user", "psw", "db");
$affectedRow = 0;
$xml = simplexml_load_file("http://......./dispo2.xml") or die("Error: Cannot create object");
foreach ($xml->children() as $row) {
$quantity = $row->qta;
$reference = $row->codice;
$sql = "UPDATE ps_stock_available set quantity = '$quantity' where id_product = (SELECT id_product from ps_product where reference in ('$reference')";
$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 } ?>
我还需要在导入数据之前插入这个,因为我必须使用相同的代码在 mysql 的数量中插入我的 xml 的值 qta 的总和:
select `reference`, sum(`quantity`)
from `ps_product_attribute`
group by `reference`