Insert fails, but no error |
[eluser]nwp2005[/eluser]
Hello, I am using Code: $this->db->insert($this->table_name, $data); to insert data into my MySQL database. If I use an incorrect table name or column name, it causes an error. But if I attempt to insert null data into a NOT NULL field, the insert fails silently with no error. It even gives me an insert id despite failing. For example, with this code: Code: $data = array( the log messages are the following: Quote:ERROR - 2011-01-02 01:51:23 --> Array As you can see, there are no values for the keys in my $data array. $this->db->_error_message() is blank and $this->db->insert_id() returns a auto incrementing id, and $this->db->insert($this->table_name, $data) returns "1". This insert is not successful. There is nothing inserted into the database. But codeigniter seems to think that it is a successful insert. What am I missing here?
[eluser]InsiteFX[/eluser]
Try using $this->input->post(); instead and see if it works. Your array is empty, which means you are not inputting any values! If you are using a form, make sure you set your data in the form code. then pass the data to your insert function. InsiteFX
[eluser]cideveloper[/eluser]
Turn on profiling. It will help you debug the situation. Code: $data = array( makes sql like this Code: INSERT INTO `ci177114` (`first_name`, `last_name`) VALUES ('', '') It is inserting blank values so it passes the not null constraint. And if you are getting a value from $this->db->insert_id() I guarantee the database insert is working. The array is NOT empty. You have keys with no values and thus the insert is working.
[eluser]nwp2005[/eluser]
[quote author="progr@mmer" date="1293980205"] It is inserting blank values so it passes the not null constraint. And if you are getting a value from $this->db->insert_id() I guarantee the database insert is working. The array is NOT empty. You have keys with no values and thus the insert is working.[/quote] Yes, you are correct. I was relying on a query with a join rather than a direct look at the DB and I wasn't seeing the results. (The incrementing IDs should have been a give-away, but I guess I assumed MySQL was reserving IDs on failed inserts) Thanks for pointing out the Profiler class too. |
Welcome Guest, Not a member yet? Register Sign In |