我有一个phpx文件,可导入xml文件的行。
我的问题是我必须在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除外!

最好的祝福

请帮我 !

最佳答案

问题有两方面,首先,您没有检查数据库访问代码中是否有任何错误,其次,bind_param要求变量在调用时存在。它将变量绑定到语句,而不是值。因为直到执行完bind_param之后,变量才存在,所以可能是您的问题。

另外,您似乎也有一个错误的-> 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++;
    }

关于php - PHP-用新列将XML插入MySQL数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22484169/

10-16 23:09