0

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.

3
  • just implode it, but why would need it like that Commented Sep 2, 2016 at 3:25
  • sorry i don't know about implode can you give me an example? Commented Sep 2, 2016 at 3:30
  • @Otachan I add answer, you can see my example of code and let me know if you have any further question Commented Sep 2, 2016 at 11:15

5 Answers 5

1

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']))");

}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Use implode() function. It join array elements with a string.

string implode ( string $glue , array $pieces )
string implode ( array $pieces )

and save the string in database.

3 Comments

do i put that inside loop? or after?
If you are using implode than there is no need of looping, it convert your array to an string
Take care with the glue char. That char should not be the part of array string
1

See answer in this post on how to insert multiple rows with one query in MySQL. Insert multiple rows with one query MySQL

This code will create a single insert query for all the items in your checkbox array.

if(!empty($_POST['check_list'])) {
    $insert_sql = "('".join("'),('",  $_POST['check_list'])."')";
    $query = $handler->query("INSERT INTO images (color_tags) VALUES ". $insert_sql);
}

Comments

1

check if count of posted array is greater than 0 or not, if it has then convert values into comma separated and insert it into table.Otherwise no need of conversion and not need to insert blank values into table

if(count($_POST['check_list']) > 0)
    $list = implode(",",$_POST['check_list']);
else
    $list = '';

$query = $handler->query("INSERT INTO images (color_tags) VALUES('$list')");

Comments

1

I did not read your last line of require to keep it like a separated list. So, modifying my example

<?php

    if(!empty($_POST['check_list']) && is_array($_POST['check_list'])) {

      // use pipe "|" to separate values 
      $color_list = implode('|', $_POST['check_list']);

      // $handler -> mysqli or mysql
      $color_list = $handler->real_escape_string($city);
      $query = $handler->query("INSERT INTO images (color_tags) VALUES ('$color_list')");

    }
?>

Later you can use explode function to make it array again.

Sudo Example:

$DB_STRING_VALUE = "Fetch color_tags value from image table"
$list_of_colors = $DB_STRING_VALUE;

if(!empty($DB_STRING_VALUE)
  $list_of_colors = explode('|', $DB_STRING_VALUE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.