无法使用一个表单将多个记录提交到同一数据库中



我有一个表单,填充了数据库中的数据,每条记录中都有一个单选列表按钮。勾选并提交表单后,仅将最后一条记录保存到数据库中,但我希望保存所有记录。这是我的代码:获取/查询数据库的表单代码

  <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'class')->label('Class')->dropDownList(
            Arrayhelper::map(Classes::find()->all(), 'class_name', 
     'class_name'),
            [
                'maxlength' => true,
                'style'=>'width: 300px; height: 50px;',
                'prompt'=>'select class',
                'onchange'=>'
                    $.post( "index.php?
   r=attendance/list&id='.'"+$(this).val(), function(data){
                    $("section#Attend").html(data);
                    });'
                ]); ?>

控制器代码获取结果并将其发送到表单

   public function actionList($id){
        $model = new Attendance;
        $form = new ActiveForm;
        $countclass = Registrationinfo::find()
                ->where(['current_class' => $id])
                ->count();
        $lga = Registrationinfo::find()
                ->where(['current_class' => $id])
                ->all();
        if($countclass > 0){
            foreach($lga as $data){ ?>
              <div >
            <div class="col-md-1"><?= ('1')?></div>
            <div style="display: inline;"><?= $data['first_name'] ?></div>
            <div style="display: inline; margin-left: 20px;"><?= 
$data['surname'] ?></div>
            <div style="display: inline; margin-left: 79px;"><?= 
  $data['gender'] ?></div>
            <div style="display: inline; margin-left: 20px;"><?= $form-
  >field($model, 'student_id')->textInput(['value'=>$data->reg_no, 
  'disabled'=>''])->label(false) ?></div>
            <div style="display: inline; margin-left: 150px;"> <?= $form-
  >field($model, 'status')
     ->radioList(array('PR' => 'Present', 'PM' =>'Permission', 'LT' 
   =>'Late', 'AB' =>'Absent'), array('class' => 'i-checks')); ?>
                <hr style="color: blue;">
        <?php
            }
        }
        else{
            echo "<div class=>'info'>Invalide Class name </div>";
        }
    }

我的操作从同一控制器创建

   public function actionCreate()
    {
        $model = [new Attendance()];
        //select date from datesetup where status is open
        $date = DateSetup::findbysql('SELECT date from date_setup WHERE 
   status = "open"')->all();
        if(count($date) > 0){
            foreach ($date as $dates);
        }else{
            echo "No date As been opened yet";
        }
        //select Term from Termsetup where status is open
        $terms = Termsetup::findbysql('SELECT term from termsetup WHERE 
    status = "open"')->all();
        if(count($terms) > 0){
            foreach ($terms as $term);
        }else{
            echo "No Term As been opened yet";
        }
        if ($model->load(Yii::$app->request->post()) && $model->validate()) 
     {
     //adding the database value retrive to the attandance form field    
            $model->term = $term->term;
            $model->day = date('d', strtotime('$dates'));
            $model->month = date('m', strtotime('$dates'));
            $model->year = date('Y', strtotime('$dates'));
            $model->date = date('Y-m-d h:m:s');
            $model->user_id = "admin";
            if ( isset( $_POST['student_id'] ) ) {
    $model->student_id = $_POST['student_id'];
    // validate, save or more..
    }
     else {
      echo "reg_no not posted"; //return array
     }
            //$model->student_id = $data->student_id;
            //$model->user_id = Yii::$app->user->identity->user_id;
            if($model->save())
       return $this->redirect(['view', 'id' => $model->id]);
        }
        return $this->render('create', [
           'model' => $model,
            ]);
     }

将表单的 name 属性声明为数组,对于更新/插入,请遍历表单的每个元素(用于每个元素(并为每个元素运行插入/更新语句。

最新更新