M
M
Maxim2016-06-10 09:43:24
Yii
Maxim, 2016-06-10 09:43:24

How to update a value in a database?

The database has a table with id and name fields.
On the page, I add new values ​​​​to the database through a form that sends the values ​​\u200b\u200bto the model, and then to the database.
Here is the form:

<?php $form = ActiveForm::begin(); ?>

    <?= $form->field($addform, 'nameEx')->textInput()->label('Exercise name')?>

    <div class="form-group">
        <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
    </div>

<?php ActiveForm::end(); ?>

The addition works.
And how to organize the update of the value in the database?
For example, I added a value, but I want to replace the Name, how can I do this? Without using a Yii generator I
started doing this:
<?php $form = ActiveForm::begin(); ?>

    <?php foreach ($allNameEx as $item){ ?>

        <?= $form->field($addform, 'nameEx')->textInput(array('value'=>$item['name']))->label('');?>

        <a href="<?php echo Url::toRoute(['site/ex-delete', 'id' => $item['id']]) ?>" >Удалить</a>

    <?php } ?>

    <div class="form-group">
        <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>

I add a form with an input that already has a Name value written in it, but what do I do next? After all, the form is one and takes the value of nameEx one, respectively, when changing, everything will change by one value.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VitGun, 2016-06-10
@KidsBout

Display next to the hidden field the Id of the entry, and make the input name unique. In the controller, check the records for changes, and if the name in the model does not match the name in the database, then we update the record.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question