Answer the question
In order to leave comments, you need to log in
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;
<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>
$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;
/*$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 "все получилось";
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question