Welcome Guest, Not a member yet? Register   Sign In
Insert fails, but no error
#1

[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(
            'first_name'  => $this->input->get_post('first_name', TRUE),
            'last_name'     => $this->input->get_post('last_name', TRUE)
        );
        $result = $this->db->insert($this->table_name, $data);

        log_message('error', print_r($data,true));
        log_message('error', $this->db->_error_message());
        log_message('error', $this->db->insert_id());
        log_message('error', $result);

the log messages are the following:

Quote:ERROR - 2011-01-02 01:51:23 --> Array
(
[first_name] =>
[last_name] =>
)

ERROR - 2011-01-02 01:51:23 -->
ERROR - 2011-01-02 01:51:23 --> 29
ERROR - 2011-01-02 01:51:23 --> 1

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?
#2

[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
#3

[eluser]cideveloper[/eluser]
Turn on profiling. It will help you debug the situation.

Code:
$data = array(
            'first_name'  => $this->input->get_post('first_name', TRUE),
            'last_name'     => $this->input->get_post('last_name', TRUE)
        );
        $result = $this->db->insert($this->table_name, $data);

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.
#4

[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.




Theme © iAndrew 2016 - Forum software by © MyBB