0

我正在尝试解析 xml 文件并将其内容添加到数据库中。

这是代码..

function generateDeck($userid, $db) {

    if (!$xml = simplexml_load_file('deckList.xml')) {
        trigger_error('Error reading XML file', E_USER_ERROR);
    }

    foreach ($xml as $card) {

        $id = $userid;
        $card_id = $card->card_id;
        $box_num = $card->box_num;
        $order = $card->order_num;

        $insert = "INSERT INTO decks (id, card_id, box_num, order_num) VALUES ('$id','$card_id','$box_num','$order')";
        $select = "SELECT * FROM decks ORDER BY order_num";

        mysqli_query($db, $insert) or die("SQL Error 1: " . mysqli_error($db));
        mysqli_query($db, $select) or die("SQL Error 1: " . mysqli_error($db));

    }

    mysqli_close($db);
}

这是xml

<deck>
   <card>
      <card_id>baby</card_id>
      <box_num>1</box_num>
      <order>1</order>
   </card>
   <card>
      <card_id>selectBaby</card_id>
      <box_num>1</box_num>
      <order>2</order>
   </card>
   <card>
      <card_id>bed</card_id>
      <box_num>1</box_num>
      <order>3</order>
   </card>
   <card>
      <card_id>selectBed</card_id>
      <box_num>1</box_num>
      <order>4</order>
   </card>
   <card>
      <card_id>book</card_id>
      <box_num>1</box_num>
      <order>5</order>
   </card>
   <card>
      <card_id>selectBook</card_id>
      <box_num>1</box_num>
      <order>6</order>
   </card>
   <card>
      <card_id>cup</card_id>
      <box_num>1</box_num>
      <order>7</order>
   </card>
   <card>
      <card_id>selectCup</card_id>
      <box_num>1</box_num>
      <order>8</order>
   </card>
   <card>
      <card_id>cupboard</card_id>
      <box_num>1</box_num>
      <order>9</order>
   </card>
   <card>
      <card_id>selectCupboard</card_id>
      <box_num>1</box_num>
      <order>10</order>
   </card>
   <card>
      <card_id>daddy</card_id>
      <box_num>1</box_num>
      <order>11</order>
   </card>
   <card>
      <card_id>selectDaddy</card_id>
      <box_num>1</box_num>
      <order>12</order>
   </card>
   <card>
      <card_id>eating</card_id>
      <box_num>1</box_num>
      <order>13</order>
   </card> 
   <card>
      <card_id>mummy</card_id>
      <box_num>1</box_num>
      <order>14</order>
   </card>
   <card>
      <card_id>selectMummy</card_id>
      <box_num>1</box_num>
      <order>15</order>
   </card>
   <card>
      <card_id>plate</card_id>
      <box_num>1</box_num>
      <order>16</order>
   </card>
   <card>
      <card_id>selectPlate</card_id>
      <box_num>1</box_num>
      <order>17</order>
   </card>

.....


</deck>

但是,XML 的每一行都会打印到屏幕上。:-/ 我使用两行从另一个 php 文件调用此函数..

//Generate new deck for new user
include('generateDeck.php');
generateDeck($user, $db);

有谁知道为什么将 xml 内容输出到屏幕上?我的输出是..

baby 1 1 selectBaby 1 2 bed 1 3 selectBed 1 4 book 1 5 selectBook 1 6 cup 1 7 selectCup 1 8 碗柜1 9 selectCupboard 1 10 daddy 1 11 selectDaddy 1 12 吃东西 1 13 mummy 1 14 selectMummy 1 15 盘子1 16 selectPlate 1 17 shoe 1 18 selectShoe 1 19 table 1 20 selectTable 1 21 walk 1 22 selectWalking 1 23 dogNose 1 24 selectDogNose 1 25 teddyEars 1 26 selectTeddyEars 1 27 ActivityOne 1 28 ActivityTwo 1 29 ActivityThree 1 30 ActivityFour 3 1 31 ix 1 ActivityFive3 1

浏览器正在渲染

<deck>
   <card>

   </card>
   <card>

   </card>
   <card>

   </card>

.....

</deck>

在 Firebug 中确认

谢谢。

(编辑以反映对保留字使用的更改(订单现在是order_num)

4

0 回答 0