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

如何在不插入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']))");
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序