Welcome Guest, Not a member yet? Register   Sign In
Help for php newbie, getting db error 1064 on update
#1

[eluser]Unknown[/eluser]
Could some php/ codeigniter wizard assist a noob with this function? I have only been writing code for a couple of days and i am trying to update data in a database.

The below function loops through each database row getting a folder and filename and loads the corresponding xml file. It then pulls a value from the xml file and inserts it into the database. It is failing at the update with a 1064 db error.

I know it should probably be split up into a model and controller but I dont understand php / codeigniter enough to do that yet.

Code:
function index() {
    // query database for the number of rows, use in the for loop
    $this->db->from('lrhotel');
    $query = $this->db->get();
    $rowcount = $query->num_rows();
    
    for ($i=0; $i<=$rowcount; $i++) {
      $row = $query->row($i);
      
      // Build path to xml file
      $xmlfile = 'http://ukvg/hxml/' . $row->folder . '/' .  $row->file;
      
      //Load xml
      $loadxml = simplexml_load_file($xmlfile);
      
      //Hotel id is also the primary key in the db
      $id = $loadxml->HotelID;
      
      //This is the string to insert into db
      $desc_to_store = $loadxml->Descriptions->Description->Value;
      
      //Insert into db      
      $data = array('desc' => $desc_to_store);
      $this->db->where('id', $id);
      $this->db->update('lrhotel', $data);
      }
    }

If i swap this line:
Code:
$data = array('desc' => $desc_to_store);

With this line:
Code:
$data = array('desc' => "'$desc_to_store'");

It works, but when i query the data it is output enclosed in single quotes?

Thanks!
#2

[eluser]Unknown[/eluser]
Ok, thought i would let you people know i found the answer, it may help someone.

If i swap this line:

Code:
$desc_to_store = $loadxml->Descriptions->Description->Value;


with this

Code:
$desc_to_store = (string) $loadxml->Descriptions->Description->Value;

this now works:
Code:
$data = array('desc' => $desc_to_store);
$this->db->where('id', $id);
$this->db->update('lrhotel', $data);

$desc_to_store needed to be converted from an object to a string.




Theme © iAndrew 2016 - Forum software by © MyBB