PHP - 使用新列将XML插入MySQL数据库

时间:2014-03-18 15:41:53

标签: php mysql xml

我有一个导入xml文件行的phpx文件。 我的问题是我必须在我的MySQL数据库中插入一列。 我添加了一个新列并编辑了phpx文件(正确执行),但新列的行保持为空。

这是一个很小的XML示例:

<recordset>
    <job created_on="2013-01-22 11:07:12">
        <id>123456</id>
        <title>Name of the job (m/w)</title>
        <land>Swiss</land>
        <date>18.03.2014</date>
    </job>
</recordset>

这是我的phpx插入功能:

    // insert into database
private function insertDataIntoDb()
{
    // truncate table
    $sql = 'TRUNCATE TABLE '.$this->dbTable.';';
    $dbStatement = $this->db->prepare($sql);
    $dbStatement->execute();

    // insert data into table
    $sql = 'INSERT INTO '.$this->dbTable.' (id, title, land, date)
    VALUES (?, ?, ?, ?)';
    $dbStatement = $this->db->prepare($sql);
    $dbStatement->execute();
    $dbStatement->bind_param('issi', $id, $title, $land, $date);

    // do this for every 'job'
    for($i=0, $l=count($this->strXml->job); $i<$l; $i++)
    {
        // xml handle
        $h = $this->strXml->stellenangebot[$i];
        // bind data for insert sql statement
        $id = utf8_decode(filter_var(html_entity_decode($h->id, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_NUMBER_INT)); 
        $title= utf8_decode(filter_var(html_entity_decode($h->title, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_MAGIC_QUOTES));
        $land= utf8_decode(filter_var(html_entity_decode($h->land, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_MAGIC_QUOTES)); 
                    $date= utf8_decode(filter_var(html_entity_decode($h->date, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_MAGIC_QUOTES)); 

        // execute sql statement
        $dbStatement->execute();
        $this->dbNumOfRows += $dbStatement->affected_rows;
    }
}

正如我之前所说,除了新列land之外,还会插入新作业!

最好的问候

请帮助我!

1 个答案:

答案 0 :(得分:0)

问题是2倍,首先你没有检查数据库访问代码中的任何错误,其次,bind_param要求变量在调用时存在。它将变量绑定到语句,而不是值。因为在你执行可能是你的问题的bind_param之前你的变量不存在。

你似乎也有错误 - &gt; execute()。

我没有添加错误处理代码,但请尝试查看它是否运行得更好。

private function CheckForErrors()
{
    // add error processing code here
}


// insert into database
private function insertDataIntoDb()
{
    // truncate table
    $sql = 'TRUNCATE TABLE '.$this->dbTable.';';
    $dbStatement = $this->db->prepare($sql);
    if ( ! $dbStatement->execute() ) {
       $this->CheckForErrors();
    }

    // insert data into table

    $sql = 'INSERT INTO '.$this->dbTable.' (id, title, land, date)
                                            VALUES (?, ?, ?, ?)';
    if ( ! $dbStatement = $this->db->prepare($sql) ) {
       $this->CheckForErrors();
    }

    //$dbStatement->execute();

    // Create the variables to be used in the bind_param()
    $id = '';
    $title = '';
    $land = '';
    $date = '';
    if ( ! $dbStatement->bind_param('issi', $id, $title, $land, $date) ) {
       $this->CheckForErrors();
    }

    // do this for every 'job'
    for($i=0, $l=count($this->strXml->job); $i<$l; $i++)
    {
        // xml handle
        $h = $this->strXml->stellenangebot[$i];
        // bind data for insert sql statement
        $id    = utf8_decode(filter_var(html_entity_decode($h->id, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_NUMBER_INT)); 
        $title = utf8_decode(filter_var(html_entity_decode($h->title, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_MAGIC_QUOTES));
        $land  = utf8_decode(filter_var(html_entity_decode($h->land, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_MAGIC_QUOTES)); 
        $date  = utf8_decode(filter_var(html_entity_decode($h->date, ENT_COMPAT, 'UTF-8'), FILTER_SANITIZE_MAGIC_QUOTES)); 

        // execute sql statement
        if ( ! $dbStatement->execute() ) {
           $this->CheckForErrors();
        }

        // this also seems a little unnecessary as an insert will only ever create one row
        // unless it fails and creates NO rows.
        // $this->dbNumOfRows += $dbStatement->affected_rows;
        $this->dbNumOfRows++;
    }