drz49609
2016-09-02 03:23
浏览 100
已采纳

如何在不插入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.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • douju6850 2016-09-02 03:41
    已采纳

    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']))");
    
    }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • donglu1472 2016-09-02 03:27

    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.

    评论
    解决 无用
    打赏 举报
  • douchuilai2355 2016-09-02 04:25

    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);
    }
    
    评论
    解决 无用
    打赏 举报
  • douge3113 2016-09-02 05:43

    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')");
    
    评论
    解决 无用
    打赏 举报
  • dongyao8698 2016-09-02 08:17

    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);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题