简体   繁体   English

MYSQL PHP Foreach语句仅提交第一个插入到语句

[英]MYSQL PHP Foreach statement only submitting first insert into statement

I am trying to load an xml file into my MYSQL DB with a foreach statement. 我正在尝试使用foreach语句将xml文件加载到我的MYSQL DB中。 It is successfully looping through the XML file for some reason, but it seems like only the first time through the loop gets actually sent to the DB. 由于某种原因,它成功地循环了XML文件,但是似乎只有第一次通过循环才被实际发送到DB。

<?xml version="1.0" encoding="utf-8" ?>

<chatcoderadmin>
  <!-- Table Alarcos -->
    <Alarcos>
        <Alarcos_ID>12</Alarcos_ID>
        <Device_ID>16cf10d0-5154-444e-96ad-46e913ac9761</Device_ID>
        <Sender>4849941474</Sender>
        <Receiver>2157389113</Receiver>
        <Message>Hi Connor</Message>
        <Service_Center></Service_Center>
        <SMSStatus>0</SMSStatus>
        <Timestamp>2015-09-10 14:09:45</Timestamp>
        <Replace_Bit_Set>0</Replace_Bit_Set>
        <Reply_Path_Exists>0</Reply_Path_Exists>
        <Is_Status_Report>0</Is_Status_Report>
    </Alarcos>
    <Alarcos>
        <Alarcos_ID>13</Alarcos_ID>
        <Device_ID>f0bd524e-2289-47b3-b9c6-65c6af2dce64</Device_ID>
        <Sender>4849941470</Sender>
        <Receiver>266781</Receiver>
        <Message>BstFreeMsg: Select privacy choices for new account or line(s). More info on how to opt in/out of Mobile Ads &amp; Reporting programs at boostmobile.com/privacy. </Message>
        <Service_Center></Service_Center>
        <SMSStatus>0</SMSStatus>
        <Timestamp>2015-09-10 11:30:01</Timestamp>
        <Replace_Bit_Set>0</Replace_Bit_Set>
        <Reply_Path_Exists>0</Reply_Path_Exists>
        <Is_Status_Report>0</Is_Status_Report>
    </Alarcos>
</chatcoderadmin>

This is the PHP code I'm using to process the XML 这是我用来处理XML的PHP​​代码

<?php
    $xml = simplexml_load_file("test.xml") or die("Error: Cannot create object");

    $createAlarcos = $conn->prepare("INSERT INTO Alarcos_Data (alarcos_id, device_id, sender, receiver, message, alarcos_timestamp) VALUES (?,?,?,?,?,?)");

    $createAlarcos->bind_param("isssss",$alarcos_id, $device_id, $sender, $receiver, $message, $alarcos_timestamp);

    foreach($xml->children() as $Alarcos) { 

        $alarcos_id = $Alarcos->Alarcos_ID;
        $device_id = $Alarcos->Device_ID;
        $sender = $Alarcos->Sender;
        $receiver = $Alarcos->Receiver;
        $message = $Alarcos->Message;
        $alarcos_timestamp = $Alarcos->Timestamp;

        $createAlarcos->execute();

    } 

    $createAlarcos->close();
    ?>

You've got the same ID in both XML elements. 您在两个XML元素中都有相同的ID。 Probably alarcos_id is primary key in your database table so it won't overwrite. 可能alarcos_id是数据库表中的主键,因此不会被覆盖。 This is a good reason to check results for errors; 这是检查结果是否有错误的充分理由; execute() is probably returning false. execute()可能返回false。

...
if (!$createAlarcos->execute()) {
    foreach ($createAlarcos->error_list as $err) {
        echo "Error $err[errno]: $err[error]<br/>\n";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM