duanmibei1929 2012-02-05 03:06
浏览 14
已采纳

将两个数组元素逐个插入到DB表中

I have some html form like that:

...
<input type="text" id="color"  name="color" value="" placeholder="Color" />
<input type="text" name="size" id="size" value="" placeholder="Size" />
... 

During form submissions, I'm checking if above inputs' values are separated by commas.

If yes, then doing something like that:

  • Lets say user entered, 1 color: black and multiple sizes: 42, 43, 44. The code will insert 3x time with different sizes but all other values will be same

  • Lets say user entered, multiple colors: black, yellow and 1 size: 42. The code will insert 2x time with different colors but all other values will be same

Question

What if user entered multiple colors and multiple sizes. Something like, colors: black, orange, blue and sizes:45,46,48. The code must insert 9 x time: black - 45, black - 46... blue-48. How can I do it?

Here is PHP code:

public function add() {
        if ($_POST['formID'] == 'add_form') {
            $stmt = $this->db->prepare("INSERT INTO `items` 
          (`refno`, `color`, `size`, `qt`, `stackno`, `notes`, `price`, `add_date`)
          VALUES (?, ?, ?, ?, ?, ?, ?, NOW())") or die($db->error);
            $count = array();
            $size_arr = explode(",", $_POST['size']);
            $color_arr = explode(",", $_POST['color']);
            $count['size'] = count($size_arr);
            $count['color'] = count($color_arr);
            if ($count['color'] > 1) {
                foreach ($color_arr as $color) {
                    $stmt->bind_param("ssiiisi", $_POST['refno'], $color, $_POST['size'], $_POST['qt'], $_POST['stackno'], $_POST['notes'], $_POST['price']) or die($stmt->error);
                    $stmt->execute() or die($stmt->error);
                }
            }
            if ($count['size'] > 1) {
                foreach ($size_arr as $size) {
                    $stmt->bind_param("ssiiisi", $_POST['refno'], $_POST['color'], $size, $_POST['qt'], $_POST['stackno'], $_POST['notes'], $_POST['price']) or die($stmt->error);
                    $stmt->execute() or die($stmt->error);
                }
            } else {
                $stmt->bind_param("ssiiisi", $_POST['refno'], $_POST['color'], $_POST['size'], $_POST['qt'], $_POST['stackno'], $_POST['notes'], $_POST['price']) or die($stmt->error);
                $stmt->execute() or die($stmt->error);
            }
            $stmt->close();
            $this->ajax->respond("nb", "Successfuly added", 1);
        }
    }
  • 写回答

1条回答 默认 最新

  • dpm91915 2012-02-05 03:31
    关注

    The brute force method of having two for loops should work

    foreach ($color_arr as $color) {
         foreach($size_arr as $size) {
              // insert into the database
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入