doulizhi1247 2016-05-12 08:58
浏览 41
已采纳

PHP读取XML并在数据库中写入

i have a XML file and need the data in a database after i checked them. so i display the data in a table and after that i try to write the data in a database. but with my code i get only the first set of data from the xml in my database. but there are at least 20 or more in each xml. How can i write all data in the database?

with this code i display the data from the xml

<form action="insert.php" method="post" />
    <table id="Wagen">
        <thead>
          <tr>        
            <th>Typ</th>
            <th>Kennzeichen</th>
            <th>Fahrer</th>
          </tr>
        </thead>

        <tbody>

    <?php
      $url = ('./cars.xml');
      $xml = simplexml_load_file( urlencode($url), null, true);


       foreach ( $xml->car as $cars ) :?>
    <tr>        
            <td><input type="hidden" name="name" value="<?php echo $cars->typ; ?>"><?php echo $cars->typ; ?></td>
            <td><input type="hidden" name="kfz-nr" value="<?php echo $cars->plate; ?>"><?php echo $cars->plate; ?></td>
            <td><input type="hidden" name="fahrer" value="<?php echo $cars->driver; ?>"><?php echo $cars->driver; ?></td>
    </tr>

<?php endforeach; ?>

       </tbody>
    </table> 
    <input type="submit" name="senden" value="XML speichern" />
</form>

and with this code i try to write the data in the database. but i get only the first row from my table. not the others.

<?php

     error_reporting(E_ALL); 
        $MYSQL_HOST = 'localhost'; 
        $MYSQL_USER = 'username'; 
        $MYSQL_PASS = 'password'; 
        $MYSQL_DATA = 'database'; 

        $connid = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS) OR die("Error: ".mysql_error());
        mysql_select_db($MYSQL_DATA) OR die("Error: ".mysql_error()); 

       if (isset($_POST['senden']))
    {
      $sql = 
              "INSERT INTO myTable 
              (fahrzeug,
               kennzeichen,
               fahrer
              )
              VALUES
              ('".mysql_real_escape_string(trim($_POST['name']))."',
               '".mysql_real_escape_string(trim($_POST['kfz-nr']))."',   
               '".mysql_real_escape_string(trim($_POST['fahrer']))."'); 
                mysql_query($sql) OR die("<pre>
".$sql."</pre>
".mysql_error()); 
                mysql_query("SET NAMES 'utf8'");
    }
?>

i hope some one can help me. :)

  • 写回答

2条回答 默认 最新

  • dongse3348 2016-05-12 09:10
    关注

    In your foreach loop, you write multiple sets of hidden inputs with different values, but all of those have the same name so after submitting only one of the inputs gets sent with POST. So in your database code, you just add that single input that gets passed.

    In conclusion, you need to have different names for each hidden input and a loop in your database insertion to loop through all sent results.

    One approach would be to add [] behind input names, making it return an array.

    <input type="hidden" name="name[]" value="<?php echo $cars->typ; ?>">
    <input type="hidden" name="kfz-nr[]" value="<?php echo $cars->plate; ?>">
    <input type="hidden" name="fahrer[]" value="<?php echo $cars->driver; ?>">
    

    will result in something like:

    $_POST[kfz-nr][0]=="ABC123";
    $_POST[kfz-nr][1]=="KLM901";
    $_POST[kfz-nr][2]=="RGB255";
    

    and so on. Then in your insertion method you do:

    if (isset($_POST['senden']))
    {
    $count = count($_POST['name']);
    for($i=0;$i<$count;$i++){
      $sql = 
              "INSERT INTO myTable 
              (fahrzeug,
               kennzeichen,
               fahrer
              )
              VALUES
              ('".mysql_real_escape_string(trim($_POST['name'][$i]))."',
               '".mysql_real_escape_string(trim($_POST['kfz-nr'][$i]))."',   
               '".mysql_real_escape_string(trim($_POST['fahrer'][$i]))."'); 
                mysql_query($sql) OR die("<pre>
    ".$sql."</pre>
    ".mysql_error()); 
                mysql_query("SET NAMES 'utf8'");
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况