V
V
Vladimir Belkov2018-01-31 17:52:39
PHP
Vladimir Belkov, 2018-01-31 17:52:39

How to write array data to mysql database in php?

Good day, I 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 'наименование ',
  `options` VARCHAR(255) NULL DEFAULT NULL COMMENT 'описание услуги',
  `price` VARCHAR(255) NULL DEFAULT NULL COMMENT 'цена услуги',
  `price_type` ENUM('0','1') NULL DEFAULT NULL COMMENT 'наименование цены услуги допустим руб./месяц или руб.',
  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 'выкл вкл',
  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 'Дата и время установки новой услуги',
  `service_change` ENUM('0','1') NULL DEFAULT NULL COMMENT 'Услуга если 0 выключена, если 1 включена',
  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'] = 'Руб./месяц';}
  else {$row['price_type'] = 'Руб./единоразово';}	
                        
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'].'</li>
';
}
?>
</ul>
  <input type="hidden" name="save_but">
  <button class="btn btn-success save_services" type="submit">Сохранить</button>	
</form>

I output this 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);

What is the question
? There is a form on it, all services are displayed, you can click 3-8 pieces and they should be enrolled in an array in the database
.
if (isset($_POST['save_but']))
  {
    $on_off = 1;
    $service_id = 2;
    /*$on_off  = $_POST["где взять пост"];
    $service_id = $_POST["где взять пост"];*/

    $res = DB::query("UPDATE ClientServices SET client_id = '%d', service_id = '%d', price_condition = '%d'",$session,$service_id,$on_off);
    if ($res) return "все получилось";
  }

If you need additional code, then I will throw off the comments
for all the answers, thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OKyJIucT, 2018-01-31
@ibobby1990

Create an external table and write in it each element of the array, and the key that will associate this element with an external entity (for example, the user's ID and the services that he issued).
Or store everything as JSON or serialize and decode it back into an array on output.
But the first option is better - editing, searching will be much easier in the long run.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question