There is a table

idv dekad_podvid 1 красный 5 зеленый 3 синий 

I make a request

 $sql = "SELECT * FROM dekad_podvid ORDER BY idv ASC"; $query = mysql_query($sql) or die("Ошибка: " . mysql_error()); while($row = mysql_fetch_array($query)) { $idv=$row[0]; $podvid=$row[1]; echo "<input type='checkbox' name='podvid[]' id='podvid' value='$idv' >"; echo $podvid; 

Displays correctly, but I do not know how to handle this array further.

It is necessary: ​​to accept ticked through POST and execute the request of the form

 SELECT * FROM table WHERE "полученные галочки" 

    1 answer 1

    In order to solve the problem of saving flags, a single table dekad_podvid not enough. It must either be expanded by the values column to store the checked flags, or an additional table dekad_podvid_values with the following structure should be created

     CREATE TABLE dekad_podvid_values ( id int(11) NOT NULL AUTO_INCREMENT, idv int(11) NOT NULL, PRIMARY KEY (id) ); 

    A separate table can be convenient, in case you need to store different states of checkboxes for different users (when several sets and additional fields may be required to indicate belonging to individual users).

    An HTML form for checkboxes might look like this.

     <?php require_once('handler.php'); ?> <!DOCTYPE html> <html lang="ru"> <head> <title>Редактирование состояния флагов</title> <meta charset='utf-8'> </head> <body> <form method='POST'> <?php foreach($podivs as $podiv) { $checked = in_array($podiv['idv'], $values) ? 'checked' : ''; echo "<input type='checkbox' name='podvid[]' id='podvid' ". "value='{$podiv['idv']}' $checked>{$podiv['dekad_podvid']}<br />"; } ?> <input type='submit' value='Добавить'><br /> </form> </body> </html> 

    The handler handler.php performs two functions; it creates $podivs to form flags and $values with saved flags. In addition, it processes the POST data coming from the form.

    In the question you are using outdated mysql extension. At the moment it is considered obsolete, moreover, it is excluded from PHP 7. Therefore, the answer further focuses on the extension of PDO. The file handler.php might look like this.

     <?php require_once('connect.php'); try { // Обработчик формы if(!empty($_POST)) { // Удаляем старые данные $query = 'DELETE FROM dekad_podvid_values'; $pdo->exec($query); // Вставляем новые данные if(count($_POST['podvid'])) { $values = []; foreach($_POST['podvid'] as $value) { $values[] = "(NULL, ".intval($value).")"; } $query = 'INSERT INTO dekad_podvid_values VALUES '.implode(',', $values); $pdo->exec($query); } header("Location: {$_SERVER['PHP_SELF']}"); exit(); } // Данные для флажков формы $query = 'SELECT * FROM dekad_podvid ORDER BY idv ASC'; $pdv = $pdo->query($query); $podivs = $pdv->fetchAll(); // Идентификаторы отмеченных флажков $query = 'SELECT idv FROM dekad_podvid_values'; $val = $pdo->query($query); $values = $val->fetchAll(PDO::FETCH_NUM | PDO::FETCH_COLUMN); } catch (PDOException $e) { echo "Ошибка выполнения запроса: {$e->getMessage()}"; } 

    To establish a connection to the database, the connect.php file is used, which can be made out as follows.

     <?php try { $pdo = new PDO( 'mysql:host=localhost;dbname=test', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => true]); } catch (PDOException $e) { echo "Невозможно установить соединение с базой данных"; } ?>