1

I'm running a codeigniter application, and i can't get this simple insert to work well.

if(!$this->db->insert('myTable',$entry)){
    $p3 = 0;
}else{
    echo $this->db->last_query();
    var_dump($this->db->_error_message());
}

the debug in the "else" clause always show me an empty string for error message, and if i test the last_query in phpmyadmin, the insert work well.

But the insert isn't happening when i normally run the app, even if the "else" is launched, implying the request succeed.

any clue ?

UPDATE : exemple of $entry array :

$entry = array(
    'id' => '',
    'id_devis' => $current[0]->id,
    'metier' => $_POST[$i+1 . '_create_metier'],
    'days' => $_POST[$i+1 . '_create_daycount'],
    'price' => $_POST[$i+1 . '_create_price'],
    'cr_date' => date('Y-m-d H:i:s'),
    'user' => ''
);

$current is an extract of another table (using $this->db->get), and all the datas are fine, since i can insert them through the echo of last_query()

3
  • Can you print the $entry array in your app, before executing the query and post it here? Commented Jan 30, 2013 at 12:03
  • Insert echo $this->db->last_query(); before }else{ and see what will be the output when inserting, there may be some strange situation when silently failing to insert there were no obvious errors from MySQL driver. Commented Jan 30, 2013 at 12:15
  • id is the primary key of the table. I just tried to put the echo in the 'if' statement, only "else" is running Commented Jan 30, 2013 at 13:34

2 Answers 2

1

Put

error_reporting(E_ALL);
ini_set('display_errors', '1');

before the line with the insert. You should see an error message.

My guess is that you try to insert an illegal value for id. Possible reasons: - It should not be empty. - It can not be a string (ie. if the type is an integer)

Most of the time an id in the database is of type int and is an auto-increment field. If so, you should not try to insert an id. It will be created for you.

Sign up to request clarification or add additional context in comments.

1 Comment

I still don't get an error message with error reporting enabled. I removed the 'id' field from the array since it seems to be useless, and the query returned by last_query still works. You're right about the auto_increment int field, but none of those points are causing the bug, that's why i don't understand -my php logs are cleans, and the query returned by last_query works fine in phpmyadmin, so logically it's not a php bug, nor a mysql bug. Weird !
1

Just found out the issue, there wasn't. a script was deleting the line i just inserted further in the code, the insert was successful. Thanks for your answers all

1 Comment

omg! thank you! this was exactly what was happening to me too! spent 2 hours debugging, and luckily came across this comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.