doujingke4981 2014-03-06 12:45
浏览 25
已采纳

通过查询结果问题在db中插入多个文本框

here i am again with an amateur question about executing a query statement for multiple textboxes.

I made the following code, standard there are 4 textboxes but with javascript the user has a option to add more textboxes, which will be having continued naming following productg[4] so productg[5] etc. I need this to work with these 4 for now, thus the reason i have not posted the javascript part.

When executing this code, i do get a result as echo with all the textboxesvalues that were filled in, but in my database only 1 row is added, when i expect there to be 4.

If more information is needed to give me sufficient advise, i´ll provide it as best as i can.

HTML

<div id="productgroepen">
    <div>
        <label>Product groep 1</label>
        <input type="text" name="productg[1]" />
    </div>
    <div>
        <label>Product groep 2</label>
        <input type="text" name="productg[2]" />
    </div>
    <div>
        <label>Product groep 3</label>
        <input type="text" name="productg[3]" />
    </div>
    <div>
        <label>Product groep 4</label>
        <input type="text" name="productg[4]" />
    </div>
</div>

PHP

    $total = 4;
    for($i=1; $i<=$total; $i++)
    {
        if(isset($_POST['productg']) && isset($_POST['productg'][$i]))
        {
            $product = $_POST['productg'][$i];
            $productquery = mysqli_query($conn, "INSERT INTO productgroepen SET id = 'LAST_INSERT_ID()' , levid ='$hidresult' , productgroep = '$product'");
            if($productquery === false)
            {
                throw new Exception('Query failed: ' . mysqli_error($conn) );
            }
        }
    }
  • 写回答

2条回答 默认 最新

  • doudizhu2222 2014-03-06 13:41
    关注

    Try this:

    $total = 4;
    for($i=1; $i<=$total; $i++){
        if(isset($_POST['productg']) && isset($_POST['productg'][$i])){
            //still needs some check whether $_POST['productg'][$i] contains an acceptable value
            $result = mysqli_query("INSERT INTO productgroepen SET levid='".mysqli_real_escape_string($conn, $hidresult)."', productgroep='".mysqli_real_escape_string($conn, $_POST['productg'][$i])."'");
            if($result === false)
                throw new Exception('Query failed: '.mysqli_error($conn) );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法