【问题标题】:Data is not saving to the database数据未保存到数据库
【发布时间】:2021-10-27 21:54:18
【问题描述】:

我是 Yii2 框架的新手,所以我遇到了数据库问题。我正在做一些项目,并且在提交时将我的数据存储在数据库中时遇到问题。代码如下。有任何想法吗? 当我提交我的值时,它不会存储在数据库中,也不会显示任何类型的错误,它只会刷新我的页面。

Model: Progress.php

    <?php
    namespace app\models;
    use yii\db\activeRecord;
    class Progress extends activeRecord {
        
        private $Emp_id;
        private $date; 
        private $app;
        private $section;
        private $task;
        private $description;
        private $status;
        private $assign_date;
        private $completion_date;
        public function rules(){
            return[
                [['Emp_id','date','app','section','task','description','status','assign_date','completion_date'], 'required']

            ];
        }

    }

Controller:
public function actionCreate(){
        $progress = new Progress();
        $formData= yii::$app->request->post();
        if($progress->load($formData)){
            if($progress->save()){
                yii::$app->getSession()->setFlash('message', 'Progress report created sucessfully');
                return $this->redirect(['index']);
            }
            else
            {
                yii::$app->getSession()->setFlash('message','Report creation failed');


        }
    }
        return $this->render('create', ['progress'=>$progress]);
            

    }
view: create.php

<?php
use yii\helpers\html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */

$this->title = 'Progress report';?>
<div class="site-index">
<h1 class="display-4" style="color: #006666; background-color: #CCFF66;"><b>Progress Report </b></h1>
        <h3 style="background-color: #CCFF99; padding: 10px;">Create employee progress report</h3>
    
<div class="row">
 
  
</div>
    <div class="body-content">
       <?php
       $form = ActiveForm::begin();?>
        
        <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 500px">
            <?php  $items=['1'=>'1','2'=>'2','3'=>'3',] ?>
               <?= $form->field($progress, 'Emp_id')->dropDownlist($items,['promt'=>'Select Employee id']);?> 
            </div>
          </div>

            <br>
        </div>
        <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 500px">
            <?= $form->field($progress, 'date'); ?>    
            </div>

          </div>
      </div>
          <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 500px">
            <?= $form->field($progress, 'app'); ?>   
            </div>
          </div>
      </div>
      <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 500px">
               <?= $form->field($progress, 'section');?> 
            </div>
          </div>
          <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 500px">
               <?= $form->field($progress, 'status');?> 
            </div>
          </div>
        </div>
         <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 1500px">
               <?= $form->field($progress, 'description')->textarea(['rows' => '6']);?> 
            </div>
          </div>
        </div>
        <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 500px">
               <?= $form->field($progress, 'assign_date');?> 
            </div>
          </div>
        </div>
        <div class="row">
          <div class="form-group">
            <div class="col-lg-6" style="width: 500px">
               <?= $form->field($progress, 'completion_date');?> 
            </div>
          </div>
        </div>

            <br>

            <br>
           <div class="row">
          <div class="form-group" >
            <div class="col-lg-3">
            <?= Html::submitButton('Create records', ['class' => 'btn btn-primary']); ?>  
            </div>
            <br>
            <div class="col-lg-2" style="width: 20px">
            <a href=<?php echo yii::$app->homeUrl;?> class="btn btn-primary">Back home</a>
        </div>
    </div>
</div>
</div>

    
<?php ActiveForm::end() ?>
    </div>
Table structure: 
<table class="table table-hover">
  <thead>
    
      <tr class="table-success">
      <th scope="col">ID</th>
      <th scope="col">Employee ID</th>
      <th scope="col">Date</th>
      <th scope="col">App</th>
      <th scope="col">Section</th>
      <th scope="col">Task</th>
      <th scope="col">Description</th>
      <th scope="col">Status</th>
      <th scope="col">Assign Date</th>
      <th scope="col">Completion Date</th>
      <th scope="col">Action</th>
    </tr>
  </thead>
  <tbody>
    <?php
    if(count($progress)> 0):?>
    <?php foreach($progress as $progress):?>
   <tr class="table-info">
    <th scope="row"><?php echo $progres->id;?></th>
      <td><?php echo $progress->Emp_id;?></td>
      <td><?php echo $progress->date;?></td>
      <td><?php echo $progress->app;?></td>
      <td><?php echo $progress->section;?></td>
      <td><?php echo $progress->task;?></td>
      <td><?php echo $progress->description;?></td>
      <td><?php echo $progress->status;?></td>
      <td><?php echo $progress->assign_date;?></td>
      <td><?php echo $progress->completion_date;?></td>
    
      <td>
    <span style="background-color: #CCCCCC"> <?= html::a('View',['view','id'=> $progress->id],['class' => 'label label-primary'])?></span>
 <span style="background-color: #66ff99"> <?= html::a('Update',['update','id'=> $progress->id],['class' => 'label label-sucess'])?></span>
  <span style="background-color: #ff9980"> <?= html::a('Delete',['delete','id'=> $progress->id],['class' => 'label label-danger'])?></span>
      </td>
    </tr>
   
   
   
  </td>
    </tr>
<?php endforeach; ?>
    <?php else: ?>
        <tr>
            <td>
                No records found!
            </td>
        </tr>     
    <?php endif; ?>
  </tbody>
</table>
        </div>
    </div>
</div>

【问题讨论】:

  • 分享你的表结构
  • 我在帖子中分享了我的表结构
  • 最终的 html 看起来有效吗?提交时浏览器中是否有任何javascript错误?提交时是否正确填充了 $formData 数组?
  • 尝试在表单中的某处添加&lt;?= Html::errorSummary($progress) ?&gt; - 对于未在表单中呈现的字段,您可能会遇到验证错误,因此在这种情况下也不会显示错误。
  • 另外,您应该从 Progress 模型中删除所有属性。

标签: php mysql forms yii2-basic-app


【解决方案1】:

您可以使用 GII 预览来确保您的模型规则

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 2021-02-02
  • 2014-08-21
  • 2019-07-12
  • 2021-10-07
  • 2016-11-22
相关资源
最近更新 更多