【问题标题】:PHP/AJAX importing XML data into MySQL db not workingPHP/AJAX 将 XML 数据导入 MySQL 数据库不起作用
【发布时间】:2017-11-01 04:44:28
【问题描述】:

我正在尝试使用PHP/Ajax 上传XML 文件并将数据导入我的数据库。一切似乎都运行顺利,我有按钮工作和通知我文件是否已上传的功能,但是当我去查看我的数据库时,信息不存在。

(我正在学习一个教程,试图使其适应我自己的需要/XML 文件结构)。

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=hpr_db','root','root');
        $query = "
        INSERT INTO data(userid, firstname, lastname, objectid, productkey, logvolume, loglength)
        VALUES(:userid, :firstname, :lastname, :objectid, :productkey, :logvolume, :loglength);";
        $statement = $connect->prepare($query);

        for($i = 0; $i < count($data); $i++)

        $statement->execute(
            array(
            ':userid'       =>  $data->Machine->MachineUserID,
            ':firstname'    =>  $data->Machine->OperatorDefinition->ContactInformation[$i]->FirstName,
            ':lastname'     =>  $data->Machine->OperatorDefinition->ContactInformation[$i]->LastName,
            ':objectid'     =>  $data->Machine->ObjectDefinition[$i]->ObjectUserID,
            ':productkey'   =>  $data->Machine->SingleTreeProcessedStem->Log[$i]>ProductKey,
            ':logvolume'    =>  $data->Machine->SingleTreeProcessedStem->Log[$i]->LogVolume,
            ':loglength'    =>  $data->Machine->SingleTreeProcessedStem->Log->LogMeasurement[$i]->LogLength,
            )
        );
    }

    $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>';
}

我对此很陌生,所以请善待。

数组中节点后面的[$i]是什么意思?我感觉我的 XML 文件的复杂性使它令人困惑,我把它放错了位置吗?教程 XML 文件只有 2 个节点,所以它的结构是这样的;

':name'     =>  $data->employee[$i]->name,
':position'     =>  $data->employee[$i]->position,

对于更复杂的XML 文件,结构是什么?还是我完全没有抓住重点。

【问题讨论】:

    标签: php mysql ajax xml


    【解决方案1】:

    我也有同样的需求,终于用PHPExcel

    首先你需要使用PHPExcel。之后读取您的 xls 文件并将数据插入到您的数据库中。

    读取您的 xls 文件并将其转换为具有以下结构的 PHP 数组:

    function fileInitializer($file, $needCells){
     $array_data = array();
     require_once 'libraries/PHPExcel/PHPExcel.php';
     $objPHPExcel = new PHPExcel();
     $target_dir = "FILEPATH/".$file;
     $objReader= new PHPExcel_Reader_Excel5();
     $objReader->setReadDataOnly(true);
     $objPHPExcel = $objReader->load( $target_dir);
     $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
     $rowIndex = 0;
     foreach($rowIterator as $row){
         $cellIterator = $row->getCellIterator();
         $cellIterator->setIterateOnlyExistingCells(false); 
         if(1 == $row->getRowIndex ()) continue;
    
        foreach ($cellIterator as $cell) {
            foreach($needCells as $needCell){
                if($needCell['cell_name'] == $cell->getColumn()){
                    $array_data[$rowIndex][$needCell['array_name']] = fai_convert_string_to_persian($cell->getCalculatedValue());
                } 
            }
        }
        $rowIndex++;
    }
    return $array_data;
    }
    $file = 'FILENAME';
    $needCells = array(
         array('cell_name'=>'A', 'array_name'=>'id')
        , array('cell_name'=>'B', 'array_name'=>'full_name')
    );
    $array_data = fileInitializer($file, $needCells);
    

    在上述过程之后,在 $array_data 中等待一段时间并将数据添加到您的数据库中

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      相关资源
      最近更新 更多