XML没有正确插入mysql

时间:2014-03-27 22:53:17

标签: php mysql xml

我正在尝试使用php将XML文件的内容放入数据库中的表中。

这几乎是XML文件的要点。

<Meet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.nspace.co.za/">
  <Name>Test Meet 1</Name>
  <StartDate xsi:nil="true" />
  <EndDate xsi:nil="true" />
  <MeetId>168</MeetId>
  <CustomerId>37</CustomerId>
  <DatabaseId>3</DatabaseId>
  <EventList>
    <Event>
      <CustomerId>37</CustomerId>
      <DatabaseId>3</DatabaseId>
      <EventId>1111</EventId>
      <Name>Formation Skydiving 2-Way - Open 168</Name>
      <EventTeamList>
        <EventTeam>
          <CustomerId>37</CustomerId>
          <DatabaseId>3</DatabaseId>
          <EventTeamId>00bab00</EventTeamId>
          <Name>201 - Test Team 2</Name>
          <NationAbbreviation />
          <Position>1</Position>
          <TotalScore>51.00</TotalScore>
        </EventTeam>
        <EventTeam>
          <CustomerId>37</CustomerId>
          <DatabaseId>3</DatabaseId>
          <EventTeamId>0001</EventTeamId>
          <Name>202 - Test Team 9</Name>
          <Position>2</Position>
          <TotalScore>41.00</TotalScore>
        </EventTeam>
        <EventTeam>
          <CustomerId>37</CustomerId>
          <DatabaseId>3</DatabaseId>
          <EventTeamId>0001</EventTeamId>
          <Name>204 - Test Team 7</Name>
          <Position>2</Position>
          <TotalScore>31.00</TotalScore>
        </EventTeam>
      </EventTeamList>
    </Event>
  </EventList>
</Meet>

这是我的PHP:

$mdstring = file_get_contents('detail' . $meet->MeetId . '.xml');
$meetdetails = new SimpleXMLElement($mdstring);

if($mdstring){

    foreach($meetdetails->EventList->Event as $event)
    {
        mysqli_query($con,"INSERT IGNORE INTO events (event_id, meet_id, event_name)
        VALUES ('{$event->EventId}', '{$meet->MeetId}', '{$event->Name}')");

                foreach($event->EventTeamList->EventTeam as $eventTeam)
                {
                    mysqli_query($con,"INSERT IGNORE INTO event_teams (team_id, event_id, meet_id, team_name)
                    VALUES ('{$eventTeam->EventTeamId}', '{$event->EventId}', '{$meet->MeetId}', '{$eventTeam->Name}')");
                }

    }
}

几乎所有内容都插入正常,但标签EventTeamId的内容除外,出于某种原因,它只是从team_id开始0列并自动递增。我不能为我的生活弄清楚为什么,我以相同方式插入的其他数据$eventTeam->Name,完全正常。

任何想法?
谢谢。

1 个答案:

答案 0 :(得分:1)

team_id是您的表event_teams的主键,似乎它的类型是整数,它也是auto-increment

auto-increment列意味着它的值是通过递增最后一个最大插入值自动创建的,尽管您可以为此字段设置自定义值,但请记住此列的类型为INTEGER并且您只能设置整数值。
由于team_id列是您的表的主键,因此您不能在其中包含两行具有相同值的行

最后在您的XML文件EventTeamId中不是整数