PHP - Insert XML into MySQL db with a new column -


i've phpx file imports rows of xml file. problem have insert 1 column more in mysql db. added new column , edited phpx file (which executes correctly), rows of new column stays empty.

here tiny xml-example:

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

here phpx insert function:

    // insert database private function insertdataintodb() {     // truncate table     $sql = 'truncate table '.$this->dbtable.';';     $dbstatement = $this->db->prepare($sql);     $dbstatement->execute();      // insert data table     $sql = 'insert '.$this->dbtable.' (id, title, land, date)     values (?, ?, ?, ?)';     $dbstatement = $this->db->prepare($sql);     $dbstatement->execute();     $dbstatement->bind_param('issi', $id, $title, $land, $date);      // every 'job'     for($i=0, $l=count($this->strxml->job); $i<$l; $i++)     {         // xml handle         $h = $this->strxml->stellenangebot[$i];         // bind data 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 said before, new jobs inserted, except new column land!

best regards

please me !

the problem 2 fold, first not checking errors in database access code , second, bind_param requires variables exists @ time called. binds variable statement, not value. since variables not exis until after bind_param problem.

also seem have erroneous ->execute() well.

i have not added error processing code try see if works little better.

private function checkforerrors() {     // add error processing code here }   // insert database private function insertdataintodb() {     // truncate table     $sql = 'truncate table '.$this->dbtable.';';     $dbstatement = $this->db->prepare($sql);     if ( ! $dbstatement->execute() ) {        $this->checkforerrors();     }      // insert data table      $sql = 'insert '.$this->dbtable.' (id, title, land, date)                                             values (?, ?, ?, ?)';     if ( ! $dbstatement = $this->db->prepare($sql) ) {        $this->checkforerrors();     }      //$dbstatement->execute();      // create variables used in bind_param()     $id = '';     $title = '';     $land = '';     $date = '';     if ( ! $dbstatement->bind_param('issi', $id, $title, $land, $date) ) {        $this->checkforerrors();     }      // every 'job'     for($i=0, $l=count($this->strxml->job); $i<$l; $i++)     {         // xml handle         $h = $this->strxml->stellenangebot[$i];         // bind data 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();         }          // seems little unnecessary insert ever create 1 row         // unless fails , creates no rows.         // $this->dbnumofrows += $dbstatement->affected_rows;         $this->dbnumofrows++;     } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -