Welcome Guest, Not a member yet? Register   Sign In
[Solved] Possible BUG in database "insert" method.
#1

[eluser]Ñuño Martínez[/eluser]
In my current project, I was writing a data insert but I wrote it wrong. It was something like:
Code:
$this->db->insert (array (
  'id' => $IdData,
  'stuff' => $SomeData,
  'other_stuff' => $OtherData
));
If you don’t see it, I forgot to tell where I want to insert the data, so data are not inserted (database doesn’t change at all) but “$this->db->affected_rows ()” returned that one row was affected. Not error, not warning in log file, not exception thrown. I spend some time wondering why data wasn’t inserted until I realised that I forgot to add the table name.

I know that there are other ways to tell the table name, but it should return an error in that case, may be a “You forgot the tablename” or “Wrong datafields” (in my case, only one table has those fields, and as I’ve said it didn’t change). I think there’s a bug somewhere.

I’m using mysqli driver.
#2

[eluser]jonez[/eluser]
I'm not sure why but this is how the insert method is defined:
Code:
public function insert($table = '', $set = NULL, $escape = NULL)
$table being optional may have something to do with active record (guessing) but you can always submit a bug report on Github and see what they say.
#3

[eluser]InsiteFX[/eluser]
Active Record Insert

Code:
// --------------------------------------------------------------------

/**
  * Insert
  *
  * Compiles an insert string and runs the query
  *
  * @param string the table to insert data into
  * @param array an associative array of insert values
  * @return object
  */
function insert($table = '', $set = NULL)
{
  if ( ! is_null($set))
  {
   $this->set($set);
  }

  if (count($this->ar_set) == 0)
  {
   if ($this->db_debug)
   {
    return $this->display_error('db_must_use_set');
   }
   return FALSE;
  }

  if ($table == '')
  {
   if ( ! isset($this->ar_from[0]))
   {
    if ($this->db_debug)
    {
     return $this->display_error('db_must_set_table');
    }
    return FALSE;
   }

   $table = $this->ar_from[0];
  }

  $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));

  $this->_reset_write();
  return $this->query($sql);
}

So as you can see Active Record does check for the table name being set.
#4

[eluser]Ñuño Martínez[/eluser]
I see. Anyway, if the query can't be executed (and it can't because the database didn't changed in my case), why it didn't raises an exception, or it didn't returns an error, or “$this->db->affected_rows ()” didn't returns 0 (zero) to tell me "Hey, man, you did a mistake!"?
#5

[eluser]noideawhattotypehere[/eluser]
Code:
$this->db->insert(...);
returns TRUE/FALSE dependining on querys success. Its up to you how you handle errors.
#6

[eluser]Ñuño Martínez[/eluser]
Unfortunatelly documentation doesn't says it (".../database/active_record.html"). Sad

I'll try it.
#7

[eluser]InsiteFX[/eluser]
This is why it is a good idea to look at the CI source code to see what a method is doing and returning for values, also doing this you will even find some hidden gems.
#8

[eluser]Ñuño Martínez[/eluser]
I see. Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB