使用foreach循环插入多个字段 [英] insert multiple fields using foreach loop

查看:111
本文介绍了使用foreach循环插入多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的表单:



pre> < h1>新增使用者< / h1>
< form method =postaction =index.php>

< table>
< thead>
< th>名称< / th>
< th>年龄< / th>
< / thead>

< tr>
< td>< input name =name []type =text/>< / td>
< td>< input name =age []type =text/>< / td>
< / tr>

< tr>
< td>< input name =name []type =text/>< / td>
< td>< input name =age []type =text/>< / td>
< / tr>

< tr>
< td>< input name =name []type =text/>< / td>
< td>< input name =age []type =text/>< / td>
< / tr>
< / table>

< input type =submitname =submitvalue =Submit/>
< / form>

以下是提交代码:

<$ p

foreach($ _POST as $ val){
$ name = $ val $ {$ b $ if(isset($ _ POST ['submit'])){ '名称'];
$ age = $ val ['age'];
$ b mysql_query(INSERT INTO users(name,age)VALUES('$ name','$ age'));




$ b $ p
$ b

查询插入到数据库中,而不是数值我已经进入了。



有人可以帮我吗?

解决方案

你在 $ _POST 上做了一个foreach,而不是名字/年龄数组。你应该像这样做名字或年龄数组的foreach:

pre $ if $(
!empty($ _ POST [' ($ _ POST ['age'])&&
is_array($ _ POST ['name'])&& is_array($ _ POST ['age'] )&&
count($ _ POST ['name'])=== count($ _ POST ['age'])
){
$ name_array = $ _POST ['name ];
$ age_array = $ _POST ['age'];
for($ i = 0; $ i
$ name = mysql_real_escape_string($ name_array [$ i]);
$ age = mysql_real_escape_string($ age_array [$ i]);
$ b mysql_query(INSERT INTO users(name,age)VALUES('$ name','$ age'));


$ / code $ / pre

我也会注意到你目前对SQL很敏感所以我添加了转义字符串的名字/年龄的步骤。

我也强烈建议简单地做一个批量插入数据库,而不是插入每个单独记录(我会留给你实施)。从性能的角度来看,这种方法几乎总是可取的。



最后,你真的不应该使用 mysql _ * 函数因为它们已被弃用。考虑更改为mysqli或PDO。


I have a problem when I want to insert multiple fields into one table.

Here's my form:

<h1>Add user</h1>
 <form method="post" action="index.php">

 <table>
    <thead>
        <th>Name</th>
        <th>Age</th>
    </thead>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>
</table>

 <input type="submit" name="submit" value="Submit" />
 </form>

And here's the submit code:

if (isset($_POST['submit'])) {

    foreach ($_POST as $val) {
        $name = $val['name'];
        $age = $val['age'];

        mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
    } 
}

The query inserts into the database, but not the values that I've entered.

Can someone please help me?

解决方案

You are doing a foreach on $_POST rather than on the name/age arrays. You should be doing foreach on name or age array like this:

if (
   !empty($_POST['name']) && !empty($_POST['age']) &&
   is_array($_POST['name']) && is_array($_POST['age']) &&
   count($_POST['name']) === count($_POST['age'])
) {
    $name_array = $_POST['name'];
    $age_array = $_POST['age'];
    for ($i = 0; $i < count($name_array); $i++) {

        $name = mysql_real_escape_string($name_array[$i]);
        $age = mysql_real_escape_string($age_array[$i]);

        mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
    } 
}

I would also note that you are currently susceptible to SQL injection so I added the step of escaping your strings for name/age.

I would also highly suggest simply making a single bulk insert into the DB instead of an insert of each record individually (I will leave that up to you to implement). This approach is almost always preferable from a performance standpoint.

Finally, you REALLY should not be using mysql_* functions as they are deprecated. Consider changing to mysqli or PDO.

这篇关于使用foreach循环插入多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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