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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化