Update data in the database?

The whole point of this story is that Using a GridView pulling data from a database. In column deduce checkboxes(CheckboxColumn). And deduce the dropDownList using a list of categories.
Html::dropDownList('records','id',
 ArrayHelper::map($listData,'id', 'name'),
 ['prompt' => '-- Select category --']);

The selected checkbox using books, you need to change the category.
Damn something I tuplyu)) still Need to pass the id of the book which will change the category_id.
In $records obtained:
Array
(
 [0] => 1
 [1] => 3
 [2] => 5
)

And $selection gets the category value you want to change. There was just (int), not an array. Sorry for force majeure

And in the handler do this:
There is such code. In $records get id of category you want to edit (from 1 to N, i.e. is an array) in the $selection which gets an id in the same table (here a single value). Like all simple. I read the documentation, looked at examples, but to catch up and can't.
if ($records = Yii::$app->getRequest()->post('records')
 && $selection = Yii::$app->request->post('selection')) {
 foreach($selection as $id){
 $e = Book::findOne((int)$id);
$e->save();
}
 }

In both variables the data arrives, but it's not updated
hkar.ru/Mi7k
July 2nd 19 at 14:18
2 answers
July 2nd 19 at 14:20
Solution
$records = Yii::$app->getRequest()->post('records');
$selection = Yii::$app->request->post('selection');
if (!is_null($records) && !is_null($selection)) {
 Book::updateAll(['category_id' => $selection], ['id' => $records]);
}
I have not correctly interpreted the problem, updated question. Sorry - immanuel_West84 commented on July 2nd 19 at 14:23
: I understand what You mean, so my answer is relevant for the current version of the question - bryana.Renn commented on July 2nd 19 at 14:26
: Thank you very much)) You're my Savior))) - immanuel_West84 commented on July 2nd 19 at 14:29
July 2nd 19 at 14:22
if
for $selection which gets an id in the same table (here a single value)

then why are You sorted? If it is a string

If I understand correctly, should be:
$id = $records[$selection];
if($e = Book::findOne($id)){
$e->save();
}

give an example of $_POST
Different lines changed the question. Added screenshot database - immanuel_West84 commented on July 2nd 19 at 14:25
You need to change the field category_id - bryana.Renn commented on July 2nd 19 at 14:28
I have not correctly interpreted the problem, updated question. Sorry - immanuel_West84 commented on July 2nd 19 at 14:31

Find more questions by tags YiiSQL