tp5 implements commitform (document)When you get the id value of the generated data, store the id value in another table, use it as a foreign key, and add a new piece of data to the foreign key table (i.e., the related table).
$labelids=htmlspecialchars($data1['labelids']); // $userid=$db_res['id']; if($labelids){ $lbids=explode(',',$labelids); foreach ($lbids as $k =>$val){ if(!empty($val)){ $lbdata1=[ 'userid'=>$userid, 'labelid'=>$val, 'create_time'=>request()->time(), ]; $db_res1 = Db::name("userlabel")->insert($lbdata1); // Information on the type of user association } } }
Error situation:
$db_res = Db::name("user")->insert($data); //When the entry is not commented, two new entries are added to the main table. $userid = Db::name("user")->insertGetId($data); $labelids=htmlspecialchars($data1['labelids']); // $userid=$db_res['id']; if($labelids){ $lbids=explode(',',$labelids); foreach ($lbids as $k =>$val){ if(!empty($val)){ $lbdata1=[ 'userid'=>$userid, 'labelid'=>$val, 'create_time'=>request()->time(), ]; $db_res1 = Db::name("userlabel")->insert($lbdata1); // Information on the type of user association } } }