在 Foreach 中插入 PHP
Insert in Foreach PHP
我有 PHP:
if($post['for']){
foreach($post['for'] as $value){
$saveMsgObjFor->message_id = $save;
$saveMsgObjFor->object_type_id = 4;
$saveMsgObjFor->object_ref_id = $value;
$saveMsgObjFor->object_email = null;
$saveMsgObjFor->save($update);
}
}
但它只是保存了第一个循环。两次,它显示错误:
Statement could not be executed (23000 - 1062 - Duplicate entry
'33' for key 'PRIMARY'
33
是字段 message_object_id
,它是 auto increment
。请帮帮我..
我认为您应该从代码中删除以下行:
$saveMsgObjFor->message_id = $save;
我不太熟悉 Zend 框架,但看起来您正试图将相同的值分配给 auto_increment,这是行不通的,因为它是行的标识符,必须是独特。希望这对您有所帮助!
基于 hint in this answer,您似乎需要将 message_object_id
显式设置为 null
。第一次它默认为 null
,但在 save()
之后它被设置为 auto increment
值,因此您需要明确地重置它,例如:
foreach($post['for'] as $value){
$saveMsgObjFor->message_object_id = null;
$saveMsgObjFor->message_id = $save;
我有 PHP:
if($post['for']){
foreach($post['for'] as $value){
$saveMsgObjFor->message_id = $save;
$saveMsgObjFor->object_type_id = 4;
$saveMsgObjFor->object_ref_id = $value;
$saveMsgObjFor->object_email = null;
$saveMsgObjFor->save($update);
}
}
但它只是保存了第一个循环。两次,它显示错误:
Statement could not be executed (23000 - 1062 - Duplicate entry
'33' for key 'PRIMARY'
33
是字段 message_object_id
,它是 auto increment
。请帮帮我..
我认为您应该从代码中删除以下行:
$saveMsgObjFor->message_id = $save;
我不太熟悉 Zend 框架,但看起来您正试图将相同的值分配给 auto_increment,这是行不通的,因为它是行的标识符,必须是独特。希望这对您有所帮助!
基于 hint in this answer,您似乎需要将 message_object_id
显式设置为 null
。第一次它默认为 null
,但在 save()
之后它被设置为 auto increment
值,因此您需要明确地重置它,例如:
foreach($post['for'] as $value){
$saveMsgObjFor->message_object_id = null;
$saveMsgObjFor->message_id = $save;