Welcome Guest, Not a member yet? Register   Sign In
wired problem when insert data
#1

[eluser]Unknown[/eluser]
function addThing($thing)
{
foreach( $thing as $key => $value )
{
$this->db->set( $key, addslashes($value) );

}
$this->db->insert('things');


$id = $this->db->insert_id();
if(empty($id))
{
log_message('error', "addThing:" . $this->db->last_query());
}
return $id;

}
Sometimes the insert_id is 0, and no data is inerted into database, but when I execute the logged sql, it can be executed successfully. Does anybody have the save problem or have any ideas about it?
Thanks in advance!
#2

[eluser]Randy Casburn[/eluser]
1) How many db connections are you using
2) Are you using this->db->load manually before this code anywhere?
3) Have you verified the resource IDs are consistent?

It's possible there is confusion amongst DB connection IDs. The query would be fine, but the execution of the query could fail because the connection to the resource was 'confused'...not correct, etc.

Just a thought,

Randy
#3

[eluser]Unknown[/eluser]
[quote author="Randy Casburn" date="1217736914"]1) How many db connections are you using
2) Are you using this->db->load manually before this code anywhere?
3) Have you verified the resource IDs are consistent?

It's possible there is confusion amongst DB connection IDs. The query would be fine, but the execution of the query could fail because the connection to the resource was 'confused'...not correct, etc.

Just a thought,

Randy[/quote]

I found the problem. It is because a trigger.

CREATE TRIGGER things_ai_trigger AFTER INSERT ON things
FOR EACH ROW BEGIN

DECLARE flag INT;

SELECT t_id INTO flag FROM t_update WHERE t_id = NEW.t_id;


END;

Please see the bold text. if there are more than one row for the same t_id in t_update table, this trigger will generate an error : Result consisted of more than one row. And the insert sql will return with an empty insert id.

And thanks Randy.
#4

[eluser]Randy Casburn[/eluser]
Ah..I see. That makes sense then. Kinda tough to see those triggers when they're hidden away! It doens't seem this should be a problem in a production system as it is written. Looks like your test data was likely causing the problem.

Glad you got it sort it out.

Have fun.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB