dongzhou1865 2015-10-23 07:45
浏览 96
已采纳

将XML数据插入SQL字段

I wrote a function to insert the <IBAN></IBAN> and from the XML to MySQL Database. I cannot figure out why, I cant insert the data from there to MySQL. This is the code I wrote.

public function test2(){
        $xml = simplexml_load_file('C:\Users\Nihit\Desktop\test.xml') or die ("could not open xml file");
               // performing sql query

            // count xml nodes
        $node = $xml->xpath("//IBAN/text()");

        var_dump($node);
        $num = 0;

         $text = $xml->asXML();
         $sql = "INSERT INTO transfer_packet VALUES(null, null, 0, null, " . $node . " , null, '" . mysql_real_escape_string($text) . "', " . $num . ", 1, 0, NOW(), null);";

            $result = mysql_query($sql) or die(mysql_error());

            if (!$result){

                var_dump($result);
                $result = mysql_query("SHOW ERRORS;");
                var_dump($result);

                }else{

             echo 'SUCCESS';
             echo 'updating id...';

             $last_id = mysql_insert_id();


             $result = mysql_query("update transfer_packet set ORIG_ID = " . $last_id . " where ID = " . $last_id .";");
               }




    }

The code is written in codeigniter.

Update (error received)

Severity: Notice Message : Array to string conversion FIlename : controllers/xmlconverter.php Line Number :368 Unknown column 'Array' in field list' 

This is the xml file I used

fkl.fi/teemasivut/sepa/tekninen_dokumentaatio/Dokumentit/… 

And of course database does not update with a new row.

  • 写回答

1条回答 默认 最新

  • douyi2107 2015-10-23 08:20
    关注

    For this sample XML, you should use this code:

    $xml        =   simplexml_load_file('https://www.fkl.fi/teemasivut/sepa/tekninen_dokumentaatio/Dokumentit/FI_camt_054_sample.xml.xml'); // get XML output
    
    $xmlFile    =   file_get_contents('https://www.fkl.fi/teemasivut/sepa/tekninen_dokumentaatio/Dokumentit/FI_camt_054_sample.xml.xml'); // get XML output as text
    
    $IBAN       =   $xml -> BkToCstmrDbtCdtNtfctn -> Ntfctn -> Acct -> Id -> IBAN; // get IBAN value
    $BIC        =   $xml -> BkToCstmrDbtCdtNtfctn -> Ntfctn -> Acct -> Svcr -> FinInstnId -> BIC; // get BIC value
    
    $transferDateTimestamp  =   strtotime($xml -> BkToCstmrDbtCdtNtfctn -> Ntfctn -> CreDtTm); // Get transfer date as timestamp for transferred_date column
    $data = array
    (
    'IBAN'              => $IBAN,
    'BIC'               => $BIC,
    'INSERT_DT'         =>  time(),
    'xml_file'          => $xmlFile,
    'status'            =>  '1',
    'transferred_date'  =>  $transferDateTimestamp
    );
    $this -> db -> insert('tableName', $data); // inserting a new row to our table
    
    $id = $this -> db -> insert_id(); // get last inserted id
    
    $uptArray   =   array('ORIG_ID' => $id);
    $this -> db -> where('id', $id);
    $this -> db -> update('tableName', $uptArray); // updating ORIG_ID column
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题