doucheng2053 2018-02-03 08:28
浏览 89
已采纳

使用PHP和Ajax将XML数据插入到Mysql表中

I am using the below PHP file to upload XML to MySQL database, but the issue is my table name is Con-1 and cannot use it. I have error in the below line:

array(
     ':name'   => $data->Con-1[$i]->name,
     ':address'  => $data->Con-1[$i]->address,
     ':gender'  => $data->Con-1[$i]->gender,
     ':designation' => $data->Con-1[$i]->designation,
     ':age'   => $data->Con-1[$i]->age
    )

please help me to solve it

    <?php
//import.php
sleep(3);
$output = '';
if(isset($_FILES['file']['name']) &&  $_FILES['file']['name'] != '')
{
 $valid_extension = array('xml');
 $file_data = explode('.', $_FILES['file']['name']);
 $file_extension = end($file_data);
 if(in_array($file_extension, $valid_extension))
 {
  $data = simplexml_load_file($_FILES['file']['tmp_name']);
  $connect = new PDO('mysql:host=localhost;dbname=testing','root', '');
  $query = "
  INSERT INTO `Con-1`
   (name, address, gender, designation, age) 
   VALUES(:name, :address, :gender, :designation, :age);
  ";
  $statement = $connect->prepare($query);
  for($i = 0; $i < count($data); $i++)
  {
   $statement->execute(
    array(
     ':name'   => $data->Con-1[$i]->name,
     ':address'  => $data->Con-1[$i]->address,
     ':gender'  => $data->Con-1[$i]->gender,
     ':designation' => $data->Con-1[$i]->designation,
     ':age'   => $data->Con-1[$i]->age
    )
   );

  }
  $result = $statement->fetchAll();
  if(isset($result))
  {
   $output = '<div class="alert alert-success">Import Data Done</div>';
  }
 }
 else
 {
  $output = '<div class="alert alert-warning">Invalid File</div>';
 }
}
else
{
 $output = '<div class="alert alert-warning">Please Select XML File</div>';
}

echo $output;

    ?>
  • 写回答

1条回答 默认 最新

  • doqdcks3948 2018-02-03 11:02
    关注

    You should be able to do it using variable variables, so first assign Con-1 to a variable and then substitute this variable for the name in the assignment...

       $var = 'Con-1';
       $statement->execute(
        array(
         ':name'   => $data->$var[$i]->name,
         ':address'  => $data->$var[$i]->address,
         ':gender'  => $data->$var[$i]->gender,
         ':designation' => $data->$var[$i]->designation,
         ':age'   => $data->$var[$i]->age
        )
       );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的