【发布时间】:2014-06-28 23:59:30
【问题描述】:
花费了无数个小时试图弄清楚这一点,但无济于事。我得到了一大堆 XML 文件,理想情况下正在寻找将事务直接导入数据库的方法,因此我不必手动执行。
我已经设法得到以下结果,但不知道如何分解产品并使用相同的剩余详细信息将它们作为单独的交易分配给客户。
这是我迄今为止尝试和管理的:
////Daily XML I get sent in this format
<trans>
<custtrans>
<cust>564</cust>
<cust_name>John David</cust_name>
<product>P1,P2,P3,P4</product>
<internal>Yes</internal>
</custtrans>
<custtrans>
<cust>877</cust>
<cust_name>James Harris</cust_name>
<product>P2</product>
<internal>No</internal>
</custtrans>
</trans>
////I'd Like the transactions to be recorded in mysql like this
cust |cust_name |product |internal
564 |John David |P1 |Yes
564 |John David |P2 |Yes
564 |John David |P3 |Yes
564 |John David |P4 |Yes
877 |James Harris |P2 |No
////This is how it is being inserted which I do NOT WANT
cust |cust_name |product |internal
564 |John David |P1,P2,P3,P4|Yes
877 |James Harris |P2 |No
////my PHP insert statement into database
$db = new PDO($dsn, $username, $password, $options);
$xml = simplexml_load_file('http://xml.com');
foreach ($xml as $insert)
{
try {
$stmt = $db->prepare('INSERT INTO customers (cust,cust_name,product,internal)
VALUES (:cust,:cust_name,:product,:internal)');
$stmt->execute(array(
':cust' => $insert ->cust,
':cust_name' => $insert ->cust_name,
':product' => $insert ->product,
':internal' => $insert ->internal,
));
//else catch the exception and show the error.
} catch(PDOException $e) {
$error[] = $e->getMessage();
}
}
【问题讨论】: