douying2243 2015-06-23 15:49
浏览 46
已采纳

Foreach输入php和mysql

I know there are similar topics out there but haven't been able to find what I'm looking for. So what I need to do is target a specific input name and foreach loop only that input instead of the whole form.

HTML look something like below.

        <form action"<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" name="table1" method="post">
            <input name="something1" type="text" />
            <input name="something2" type="text" /> 
            <input name="something3" type="text" />
            <input name="something4" type="text" />
            <input name="something4" type="text" />
            <input name="something4" type="text" /> 
            <input name="button"  type="submit" value="Add" />    
        </form>

So I wanna loop every "something4" and just ignore the rest. Is this possible?

Just to explain what I want to do with the value is for every "something4" I'm gonna add a field to my DB and the input the respective input value into that field.

something like below...

        $i = 0;
        foreach ($_POST as $something4 => $something4_value) {
            $add = mysqli_query($connect, "ALTER TABLE 'table' ADD something4$i VARCHAR( 255 ) NOT NULL") or die (mysql_error());
            $sql_update = mysqli_query($con, "UPDATE 'table' SET something4$i='$something_value' WHERE id='$id'") or die (mysql_error());
            $i++;
        }

I hope this make sense! Thank you! :)

  • 写回答

1条回答 默认 最新

  • dtjo87679 2015-06-23 15:51
    关注

    Create each of the name="something4" into an array like this:

    <input name="something4[]" type="text" />
    

    Then you can do a

    foreach($_POST['something4'] as $something4) {
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据