How to Write data in an array in Mysql database in php?

Good day , need your help
There are 3 tables
CREATE TABLE `Services` (
 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
 `name` VARCHAR(255) NULL DEFAULT NULL COMMENT 'name ',
 `options` VARCHAR(255) NULL DEFAULT NULL COMMENT 'description of service',
 `price` VARCHAR(255) NULL DEFAULT NULL COMMENT 'price',
 `price_type` ENUM('0','1') NULL DEFAULT NULL COMMENT 'product name price services for example RUB/month or RUB',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `ClientServices` (
 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
 `client_id` INT(11) UNSIGNED NOT NULL,
 `service_id` INT(11) UNSIGNED NOT NULL,
 `price_condition` ENUM('0','1') NULL DEFAULT NULL COMMENT 'off to on',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

 CREATE TABLE `ServicesHistory` (
 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
 `service_id` INT(11) UNSIGNED NOT NULL,
 `client_id` INT(11) UNSIGNED NOT NULL,
 `data_time` DATETIME NULL DEFAULT NULL COMMENT 'date and time of the installation of a new service',
 `service_change` ENUM('0','1') NULL DEFAULT NULL COMMENT 'Service if 0 is disabled, 1 if enabled',
 PRIMARY KEY (`id`),
 INDEX `service_id_key` (`service_id`),
 INDEX `client_id_key` (`client_id`),
 CONSTRAINT `service_idx_const` FOREIGN KEY (`service_id`) REFERENCES `Services` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
 CONSTRAINT `client_idx_const` FOREIGN KEY (`client_id`) REFERENCES `Clients` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

There is a form
<form method="post" action="edit_services.php">
<?php
foreach ($services_list as $row)
{
 if ($row['price_type'] == 0) {$row['price_type'] = 'RUB./month';}
 else {$row['price_type'] = 'RUB./one-time';} 

echo '<li class="list-group-item"-->
<button class="changeCondition '.(!empty($row['price_condition'] == '1') ? "btn btn-primary" : "btn btn-default").'" type="submit">
<i class="'.(!empty($row['price_condition'] == '1') ? "icon-check" : "icon-list-add").'"></i>
</button>'.$row['options'].' '.$row['price'].' '.$row['price_type'].' '.$row['price_condition'].'
';
}
?>

 <input type="hidden" name="save_but">
 <button class="btn btn-success save_services" type="submit">Save</button> 
</form>

I deduce that SQL query
$services_list = DB::query_fetch_all("
 SELECT cs.price_condition, client_id, s.`name`, s.`options`, s.price, s.price_type, s.id
 FROM Services as s JOIN ClientServices as cs ON s.id = cs.service_id WHERE client_id = '%d'
 ",$session);

The question
Is a form it displays all services, you can click 3-8 pieces and they must be recorded by the array in the database
cannot write , develop correct form and PHP script
that is what is at the moment

if (isset($_POST['save_but']))
{
 $on_off = 1;
 $service_id = 2;
 /*$on_off = $_POST["where can I get post"];
 $service_id = $_POST["where can I get post"];*/

 $res = DB::query("UPDATE ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'",$session,$service_id,$on_off);
 if ($res) return "all out";
 }

If you need additional code that will throw off a koment
for all answers thanks in advance
June 8th 19 at 17:14
1 answer
June 8th 19 at 17:16
Solution
Create an external table and write in it each element of the array, and a key that will associate this item with an external entity (for example, ID of the user and the services which he designed).

Or store everything in JSON, or serialize and output decode back to array.

But the first option is better - to edit, search will be much easier in the long run.
CREATE TABLE `ClientServices` (
 `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
 `client_id` INT(11) UNSIGNED NOT NULL,
 `service_id` INT(11) UNSIGNED NOT NULL,
 `price_condition` ENUM('0','1') NULL DEFAULT NULL COMMENT 'off to on',
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

I have such a table , I have a problem with recording data - Roderick_Abshir commented on June 8th 19 at 17:19
the shape certainly leaves something to be desired, so immediately and not to parse what is what.

What is the complexity? You don't know where to get other fields to fill the table? Print a list of them through , ask him a certain name and in PHP catch its value and substitute in the query.

To save data from array, you need to walk through the array with foreach and inside the loop to INSERT as many do, how many array elements will be. - violet_Dibbe commented on June 8th 19 at 17:22
I need to make the handler has already finished forms
I have the beginning but I feel that it's not right
if (isset($_POST['save_but']))
{
 $on_off = 1;
 $service_id = 2;
 /*$on_off = $_POST["where can I get post"];
 $service_id = $_POST["where can I get post"];*/

 $res = DB::query("UPDATE ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'",$session,$service_id,$on_off);
 if ($res) return "all out";
 }

A request I got this , need a way to tell the mask is on PHP - Roderick_Abshir commented on June 8th 19 at 17:25
in the form you don't have any Input tag where could be the data. It is not clear how you actually get the array. - violet_Dibbe commented on June 8th 19 at 17:28
<form method="post" action="edit_services.php">
<?php
foreach ($services_list as $row)
{
 if ($row['price_type'] == 0) {$row['price_type'] = 'RUB./month';}
 else {$row['price_type'] = 'RUB./one-time';} 

echo '<li class="list-group-item"-->
 <input type="hidden" name="condition" value="1">
<button class="changeCondition '.(!empty($row['price_condition'] == '1') ? "btn btn-primary" : "btn btn-default").'" type="submit">
<i class="'.(!empty($row['price_condition'] == '1') ? "icon-check" : "icon-list-add").'"></i>
</button>'.$row['options'].' '.$row['price'].' '.$row['price_type'].' '.$row['price_condition'].'
';
}
?>

 <input type="hidden" name="save_but" value="1">
 <button class="btn btn-success save_services" type="submit">Save</button> 
</form>


Added 2 input , the Save button on the field that is the button inside the loop - Roderick_Abshir commented on June 8th 19 at 17:31
on the "Save" button input superfluous. And what about the first - in PHP script you can get its contents thus
$condition = $_POST['condition'];
that is what the field name will specify this element in the global array $_POST and catch. - violet_Dibbe commented on June 8th 19 at 17:34
if you need an array (for example, some people want to choose), and you add a checkbox, then make it be so
<input type="checkbox" name="condition[]" value="1">

and in php script you have in $_POST['condition'] is already an array with all values that were checked.

And you have here the condition is invalid (empty checks for the string or array on the void)
(!empty($row['price_condition'] == '1') ? "btn btn-primary" : "btn btn-default")

it should look like this
$row['price_condition'] == '1' ? "btn btn-primary" : "btn btn-default"
- violet_Dibbe commented on June 8th 19 at 17:37
And as the handler to make entries in the database , the query in mysql discard write myself , I just need the array that I'm a little confused how to make
Let's start again

if (isset($_POST['save_but']))
{
$condition = $_POST['condition'];
then the array 
{
arrey =[, how to write variables or something ? ]
}
and what's the next thing ?

 $res = DB::query("UPDATE ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'",$session,$service_id,$on_off);
 if ($res) return "all out";
 }
- Roderick_Abshir commented on June 8th 19 at 17:40
,
$condition = $_POST['condition'];

// iterate through all the array elements in turn
foreach($condition as $item) {
 // here is the query, where $item is the array element
}
- violet_Dibbe commented on June 8th 19 at 17:43
Turns out if the user chooses 6 different services , and I have 6 records to be in the database
$condition = $_POST['condition'];
foreach($condition as $item) {
//I need to write 3 variables 
$item['client_id'], $item['service_id'], $condition = $_POST['condition'];
//and then query the database for the change is the addition of the edit
$res = DB::query("UPDATE ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'",$session,$service_id,$on_off);
}

am I doing it right or not ? - Roderick_Abshir commented on June 8th 19 at 17:46
, вместо
$condition = $_POST['condition'];
foreach($condition as $item) {
    //мне нужны для записи 3 переменные 
    $item['client_id'],  $item['service_id'],  $condition = $_POST['condition'];
    //а дальше запрос к базе на изменение добавление редактирование
    $res = DB::query("UPDATE ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'",$session,$service_id,$on_off);
}

надо
$condition = $_POST['condition'];
foreach($condition as $item) {
    //а дальше запрос к базе на изменение добавление редактирование
    $res = DB::query("INSERT INTO ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'", $item['client_id'], $item['service_id'], $condition);
}

но у вас нет в форме соответствующих элементов массива, чтобы их в базу вносить. Или вы удаляете перед показом формы часть кода, или не ту форму колупаете. - violet_Dibbe commented on June 8th 19 at 17:49
it is difficult to say something, seeing only fragments of code which is definitely not a solid. From what you show the full picture not folded. - violet_Dibbe commented on June 8th 19 at 17:52
The whole file
<?php
include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");

if (isset($_SESSION["AUTH_USER"])) {

 $session = $_SESSION["AUTH_USER"];
 $today = date("Y-m-d H:i:s");

 $services_list = DB::query_fetch_all("
 SELECT cs.price_condition, client_id, s.`name`, s.`options`, s.price, s.price_type, s.id
 FROM Services as s JOIN ClientServices as cs ON s.id = cs.service_id WHERE client_id = '%d'
",$session);


 if (isset($_POST['save_but']))
{
 $on_off = 1;
 $service_id = 2;
/* foreach ($_POST as $key=-->$value) {
 echo "<p>".$key." = " . $value . "</p>";
}*/
 /*$name = $_POST["keyName"];
 $phone = $_POST["keyPhone"];*/

 $res = DB::query("UPDATE ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'",$session,$service_id,$on_off);
 if ($res) return "all out";
}
}
?>



<?php include (ABSOLUTE_PATH."/layouts/shared/head.php"); ?>

<?php if ($conf--->is_auth()) { ?>


<!-- Page container -->
<div class="page-container page-container sidebar-fixed can-resize">
 <?php include (ABSOLUTE_PATH."/layouts/shared/sidebar.php"); ?>

 <!-- Main container -->
 <div class="main-container gray-bg">

 <?php include (ABSOLUTE_PATH."/layouts/shared/mheader.php"); ?>
 <?php include (ABSOLUTE_PATH."/layouts/shared/breadcrumbs.php"); ?>

<?php if (isset($_GET["err"])) $err = "<h1-->Error";?> 
<style>
.services_check button {margin-right:19px;}
.services_check {font-size:20px;}
</style>
 <!-- Main content -->
 <div class="main-content">

 <div class="row">
 <div class="col-md-10">
 <div class="panel panel-default">
 <div class="panel-heading clearfix"> 
 <h3 class="panel-title">Services</h3> 
 </div> 

 <!-- panel body --> 
 <div class="panel-body"> 
 <ul class="list-group services_check">
 <form method="post" action="edit_services.php">
<?php
// btn btn-default btn btn-primary// icon-list-add-icon-check
 foreach ($services_list as $row)
{
 if ($row['price_type'] == 0) {$row['price_type'] = 'RUB./month';}
 else {$row['price_type'] = 'RUB./one-time';} 

 echo '<li class="list-group-item"-->
 <button class="changeCondition '.(!empty($row['price_condition'] == '1') ? "btn btn-primary" : "btn btn-default").'" type="submit">
 <i class="'.(!empty($row['price_condition'] == '1') ? "icon-check" : "icon-list-add").'"></i>
 </button>'.$row['options'].' '.$row['price'].' '.$row['price_type'].' '.$row['price_condition'].'
';
}
 /*<i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'">*/ 
?>

 </i></form></ul><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'">
 <input type="hidden" name="save_but">
 <button class="btn btn-success save_services" type="submit">Save</button> 

<p>
 </p><div class="alert alert-success alert-dismissible saved" style="display:none;" role="alert">
 <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
 the <strong>Changes saved!</strong> 
 <br / >Dear customer generated application No. 137484 activation of additional services. In the near future you will contact the Manager on work with clients.
</div>
<p></p>
 </i></div><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'"> 
 </i></div><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'">
 </i></div><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'"> 

 </i></div><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'"> 
 <?php include (ABSOLUTE_PATH."/layouts/shared/footer.php"); ?>


 </i></div><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'">
 <!-- /main content -->

 </i></div><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'">
 <!-- /main container -->

</i></div><i class="'.(empty($row['pole']) ? "icon-list-add" : "icon-check").'">
<!-- /page container -->

<?php 
}
else {
 if (isset($auth) && $auth != "true") {
 $auth_err = '<div class="alert alert-danger alert-login" role="alert"-->'.$auth."; 
}
 include (ABSOLUTE_PATH."/layouts/shared/login_page.php");
}
?>
<script asyncsrc="/assets/js/services.js"></script>
<?php include (ABSOLUTE_PATH."/layouts/shared/loadjs.php"); ?>

</i>
- Roderick_Abshir commented on June 8th 19 at 17:55

Find more questions by tags PHPMySQL