简体   繁体   English

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

[英]PHP - Insert XML into MySQL db with a new column

I've a phpx file which imports rows of a xml file. 我有一个phpx文件,可导入xml文件的行。 My problem is that I have to insert one column more in my MySQL db. 我的问题是我必须在MySQL数据库中再插入一列。 I added a new column and edited the phpx file (which executes correctly), but the rows of the new column stays empty. 我添加了一个新列并编辑了phpx文件(可以正确执行),但是新列的行保持为空。

Here is a tiny XML-example: 这是一个很小的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>

Here is my phpx insert function: 这是我的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;
    }
}

As I said before, the new jobs will inserted, except the new column land ! 正如我之前所说,将插入新的工作,除了新的列land

Best Regards 最好的祝福

Please help me ! 请帮我 !

The problem is 2 fold, first you are not checking for any errors in your database access code and second, the bind_param requires the variables to exists at the time it is called. 问题有两方面,首先,您没有检查数据库访问代码中是否有任何错误,其次,bind_param要求变量在调用时存在。 It binds the variable to the statement, not the value. 它将变量绑定到语句,而不是值。 Since your variables do not exis until after you do the bind_param that is probably your problem. 由于您的变量直到执行完bind_param之后才存在,所以可能是您的问题。

Also you seem to have an erroneous ->execute() as well. 另外,您似乎也有一个错误的-> execute()。

I have not added error processing code but try this see if it works a little better. 我没有添加错误处理代码,但是尝试一下看看它是否工作得更好。

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++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM