A
A
Alexander Ivanov2018-03-18 22:39:46
Yii
Alexander Ivanov, 2018-03-18 22:39:46

Why is table data not updating in Yii2?

there is a form

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$baseUrl = Yii::$app->request->baseUrl; 
?>
<div>
<? 
$username = $user -> username;
$mail = $user -> email;
$phone = $user -> phone;
$ava = $user -> ava;
$status = $user -> status;
$pass = $user -> password;
$id = $user -> id;
?>
<?php $form = ActiveForm::begin([ 'method' => 'POST', 'options' => ['enctype' => 'multipart/form-data']]) ?>
          <?= $form->field($EditUserAdmin, 'username')->textInput(['value' => $username]) ?>
          <?= $form->field($EditUserAdmin, 'email')->textInput(['value' => $mail]) ?>
          <?= $form->field($EditUserAdmin, 'phone')->textInput(['value' => $phone]) ?>
          <?= $form->field($EditUserAdmin, 'password') ?>
          <?= $form->field($EditUserAdmin, 'status') ?>
          <?= $form->field($EditUserAdmin, 'id')->textInput(['value' => $id]) ?>
          <?= $form->field($EditUserAdmin, 'ava')->fileinput() ?>
          <img src='<?='../../web/'.$ava?>'> <br>
          <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
          <?php ActiveForm::end() ?>

</div>

Controller
public function actionUsers()
    {

                if(Yii::$app->request->get()){
                  $EditUserAdmin = new EditUserAdmin();
                  $user = $_GET['user'];
                  $user_result = User::find()->where(['username' => $user]) -> one();					
                  return $this->render('Edituseradmin',[
                  'EditUserAdmin' => $EditUserAdmin,
                  'user' => $user_result ]);
                }
 								if(Yii::$app->request->post()){	    
                  $id = $_POST['EditUserAdmin']['id'];			
                  $user = User::find()->where(['id' => $id]) -> one();
                  
                      $user->username=$_POST['EditUserAdmin']['username'];
                    $user->email=$_POST['EditUserAdmin']['email'];
                    $user->phone=$_POST['EditUserAdmin']['phone'];
                    $user->password=md5($_POST['EditUserAdmin']['password']);
                    $user->ava=$_POST['EditUserAdmin']['ava'];
                    $user->status=$_POST['EditUserAdmin']['status'];
                    $user->update();
                                    
                    return Yii::$app->response->redirect(Url::to('index'));
                } 
    $users = user::find()->all();	
    return $this->render('users',[
            'users' => $users,
        ]);  
    }

the data is passed to the POST array, but as I understand it, the if(Yii::$app->request->post()) condition in the controller does not work, I can’t understand why. Please tell me what I missed, there are no errors in the Yui debugger and there are no errors on the form, the data is simply sent and that's it, but they should be sent, go to the condition and work out the code after the condition.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis, 2018-03-18
@sidni

1) Wildness .... Why do you directly access $ _POST when there is a load method
2) Instead Yii::$app->request->post()
, use
Yii::$app->request->isPost

R
Ruslan Absalyamov, 2018-03-19
@rusline18

1 Use instead of such constructions $_POST['EditUserAdmin']['username']on such

Yii::$app->request->post('EditUserAdmin')['username']
Well, if you use Yii2
Usually they use it differently Yii::$app->response->redirect(Url::to('index')
And like this this->redirect(['controller/index'])
The second point is usually not update() but save(), at least that's how I use it in my projects.
Include first errors something like this
if($user->save()){
return $user->getErrrors();
}

If it doesn't save it will throw an error.
If the post does not reach, then use approximately var_dump () And check whatever you want there to understand if the post is going through for you or not. Let's sayvar_dump(Yii::$app->response->post());

V
vnpp, 2018-03-20
@vnpp

It is necessary to check the result of the call
which, unlike save() returns FALSE or the number of records changed, and save() only TRUE/FALSE.
Data may be rejected during validation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question