Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point why this Codeigniter's data insertion is causing an error?

Hi Experts

Could you point why this Codeigniter's data insertion is causing an error?

if ($this->db->insert("server_routes", $params)) {
	return $this->db->insert_id();
} else {
	return false;
}

Open in new window


Thanks in advance!

The $params array:
array(9) { ["id"]=> string(0) "" ["_name"]=> string(3) "xxx" ["_hostname"]=> string(14) "192.168.100.76" ["_port"]=> string(4) "5060" ["_providerId"]=> string(2) "13" ["_prefix"]=> string(5) "65019" ["_active"]=> string(1) "1" ["_channel"]=> string(3) "120" ["_description"]=> string(0) "" }

Open in new window


The MySQL table structure where the data must to be inserted:

CREATE TABLE server_routes (
  id int(11) NOT NULL DEFAULT 0,
  _name varchar(150) DEFAULT NULL,
  _hostname varchar(45) DEFAULT NULL,
  _port int(11) DEFAULT NULL,
  _providerId int(11) DEFAULT NULL,
  _prefix varchar(15) DEFAULT NULL,
  _active int(11) DEFAULT NULL,
  _channel int(11) DEFAULT 30,
  _description text DEFAULT NULL
)
ENGINE = INNODB
AVG_ROW_LENGTH = 327
CHARACTER SET latin1
COLLATE latin1_swedish_ci;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why is the "id" part of the $params array?  It would seem that this would be generated by the INSERT, not passed to the INSERT, right?  And in any case, the field is defined INT, but gets passed an empty string.

Full disclosure: I don't know much about CodeIgniter, and these frameworks have their own conventions, so maybe this is acceptable in the CodeIgniter world.  It just seems odd to me, based on my experience with PHP and MySQL.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try omitting (removing) the "id" from the $params array.  See what happens and let us know, thanks.
Avatar of Eduardo Fuerte

ASKER

@Ray

Yes, this way of sending data for insertions is accepted to Codeigniter.
Thank you for the help!
Just curious - what fixed the issue?
Of course.

Just to apply PK to id column and make it Auto Increment.

After that the initial code perfectly runs under Codeigniter.