Otachan Otachan - 3 months ago 10
MySQL Question

How to insert an array into the database without inserting every loop in php

I have this array from the checkbox

<?php
$query = $handler->query("SELECT * FROM colors");
while ($row = $query->fetch()) {?>
<input type="checkbox" name="check_list[]" id="<?php echo $row['id']?>" value="<?php echo $row['hex'];?>">
<?php } ?>


PHP Query

<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$query = $handler->query("INSERT INTO images (color_tags) VALUES ('$check')");
}
}
?>


I want to insert the data's from the array without inserting it on every row
I want it to be just separated in commas like data1,data2,data3 then insert it.

Ish Ish
Answer

Store it in json.

json_encode($array)

<?php
if(!empty($_POST['check_list'])) {

$query = $handler->query("INSERT INTO images (color_tags) VALUES ( json_encode($_POST['check_list']))");

}
?>
Comments