1. <tfoot id='yJgeW'></tfoot>

    <small id='yJgeW'></small><noframes id='yJgeW'>

      <i id='yJgeW'><tr id='yJgeW'><dt id='yJgeW'><q id='yJgeW'><span id='yJgeW'><b id='yJgeW'><form id='yJgeW'><ins id='yJgeW'></ins><ul id='yJgeW'></ul><sub id='yJgeW'></sub></form><legend id='yJgeW'></legend><bdo id='yJgeW'><pre id='yJgeW'><center id='yJgeW'></center></pre></bdo></b><th id='yJgeW'></th></span></q></dt></tr></i><div id='yJgeW'><tfoot id='yJgeW'></tfoot><dl id='yJgeW'><fieldset id='yJgeW'></fieldset></dl></div>
        <bdo id='yJgeW'></bdo><ul id='yJgeW'></ul>

    1. <legend id='yJgeW'><style id='yJgeW'><dir id='yJgeW'><q id='yJgeW'></q></dir></style></legend>
    2. 使用 foreach 循环插入多个字段

      时间:2023-09-23
            <tbody id='HXuAU'></tbody>

          • <tfoot id='HXuAU'></tfoot><legend id='HXuAU'><style id='HXuAU'><dir id='HXuAU'><q id='HXuAU'></q></dir></style></legend>

            <small id='HXuAU'></small><noframes id='HXuAU'>

          • <i id='HXuAU'><tr id='HXuAU'><dt id='HXuAU'><q id='HXuAU'><span id='HXuAU'><b id='HXuAU'><form id='HXuAU'><ins id='HXuAU'></ins><ul id='HXuAU'></ul><sub id='HXuAU'></sub></form><legend id='HXuAU'></legend><bdo id='HXuAU'><pre id='HXuAU'><center id='HXuAU'></center></pre></bdo></b><th id='HXuAU'></th></span></q></dt></tr></i><div id='HXuAU'><tfoot id='HXuAU'></tfoot><dl id='HXuAU'><fieldset id='HXuAU'></fieldset></dl></div>
                <bdo id='HXuAU'></bdo><ul id='HXuAU'></ul>
                本文介绍了使用 foreach 循环插入多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                当我想在一个表中插入多个字段时遇到问题.

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

                这是我的表格:

                <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>
                

                这是提交代码:

                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.

                有人可以帮我吗?

                推荐答案

                您正在对 $_POST 而不是 name/age 数组执行 foreach.您应该像这样对 name 或 age 数组执行 foreach:

                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')");
                    } 
                }
                

                我还注意到您目前很容易受到 SQL 注入的影响,因此我添加了为名称/年龄转义字符串的步骤.

                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.

                最后,您真的不应该使用 mysql_* 函数,因为它们已被弃用.考虑改用mysqli或PDO.

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

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

                上一篇:在 foreach 中,isLastItem() 存在吗? 下一篇:foreach 是否保证在 php 中按数组顺序迭代?

                相关文章

                最新文章

                1. <legend id='uFSyx'><style id='uFSyx'><dir id='uFSyx'><q id='uFSyx'></q></dir></style></legend>
                2. <small id='uFSyx'></small><noframes id='uFSyx'>

                    <bdo id='uFSyx'></bdo><ul id='uFSyx'></ul>
                  <tfoot id='uFSyx'></tfoot>
                  <i id='uFSyx'><tr id='uFSyx'><dt id='uFSyx'><q id='uFSyx'><span id='uFSyx'><b id='uFSyx'><form id='uFSyx'><ins id='uFSyx'></ins><ul id='uFSyx'></ul><sub id='uFSyx'></sub></form><legend id='uFSyx'></legend><bdo id='uFSyx'><pre id='uFSyx'><center id='uFSyx'></center></pre></bdo></b><th id='uFSyx'></th></span></q></dt></tr></i><div id='uFSyx'><tfoot id='uFSyx'></tfoot><dl id='uFSyx'><fieldset id='uFSyx'></fieldset></dl></div>