PHP -在MySQL数据库中插入一个新列


PHP - Insert XML into MySQL db with a new column

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

下面是一个很小的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 !

问好

请帮帮我!

问题是2倍,首先您没有检查数据库访问代码中的任何错误,其次,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++;
    }