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 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录