Foreach输入php和mysql [英] Foreach input php and mysql

查看:210
本文介绍了Foreach输入php和mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道那里有类似的话题,但一直没有找到我要找的东西。所以我需要做的是针对一个特定的输入名称和foreach循环只有输入,而不是整个形式。



HTML看起来像下面。

 < form action<?php echo $ _SERVER ['PHP_SELF'];?> enctype =multipart / form-dataname =table1method =post> 
< input name =something1type =text/>
< input name =something2type =text/>
< input name =something3type =text/>
< input name =something4type =text/>
< input name =something4type =text/>
< input name =something4type =text/>
< input name =buttontype =submitvalue =Add/>
< / form>

所以我想循环每个something4,而忽略其余部分。这是可能的吗?



只是为了解释我想要做的值是每个something4我要添加一个字段到我的数据库和输入各自的输入值到这个领域。

类似下面...

  $ 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 ++;
}

我希望这是有道理的!谢谢! :)

解决方案

创建每个 name =something4一个像这样的数组:

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

然后你可以做一个

<$ p $ foreach($ _ POST ['something4'] as $ something4){
}


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! :)

解决方案

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) {
}

这篇关于Foreach输入php和mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆