5

当我想将多个字段插入到一个表中时,我遇到了问题。

这是我的表格:

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

查询插入到数据库中,但不是我输入的值。

有人可以帮帮我吗?

4

6 回答 6

16

您正在$_POST对名称/年龄数组而不是在名称/年龄数组上进行 foreach。您应该像这样对 name 或 age 数组进行 foreach :

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 注入的影响,因此我添加了为姓名/年龄转义字符串的步骤。

我还强烈建议简单地将单个批量插入到数据库中,而不是单独插入每条记录(我将由您来实现)。从性能的角度来看,这种方法几乎总是更可取的。

最后,您真的不应该使用mysql_*已弃用的函数。考虑更改为 mysqli 或 PDO。

于 2013-08-09T22:33:13.640 回答
6
if (isset($_POST['submit'])) {

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

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

这将解决您的问题!

于 2013-08-09T22:39:22.520 回答
1
foreach($_POST['firstname'] as $key=>$value) {
    $firstname = $_POST['firstname'][$key];
    $lastname = $_POST['tipo'][$key];
    echo "Parte: $lastname";
    echo "<br>"; 
    echo "Tipo: $firstname";
    echo "<br>";
}
于 2018-01-22T18:28:22.353 回答
0
$education_institute_array = $_POST['education_institute'];
        $education_qualification_array = $_POST['education_qualification'];
        $education_start_date_array = $_POST['education_start_date'];
        $education_end_date_array = $_POST['education_end_date'];
        $education_note_array = $_POST['education_note'];
        for ($i = 0; $i < count($education_institute_array); $i++) {
            $education_institute = mysql_real_escape_string($education_institute_array[$i]);
            $education_qualification = mysql_real_escape_string($education_qualification_array[$i]);
            $education_start_date = mysql_real_escape_string($education_start_date_array[$i]);
            $education_end_date = mysql_real_escape_string($education_end_date_array[$i]);
            $education_note = mysql_real_escape_string($education_note_array[$i]);
            $sql_education_insert = "INSERT INTO `education` (`user_id`, `education_institute`, `education_qualification`, `education_start_date`, `education_end_date`, `education_note`) VALUES ('$user_id', '$education_institute', '$education_qualification', '$education_start_date', '$education_end_date', '$education_note')";
            $sql_result_education = mysql_query($sql_education_insert);
        }
于 2016-07-24T07:56:25.150 回答
0

更简单的代码对我很有效。

if (isset($_POST['submit'])) {
     $i = 0;
     for ((array) $_POST as $val) {
       $sql = "INSERT INTO users (name, age) VALUES (
                   '{$_POST["name"][$i]}','{$_POST["age"][$i]}'
               );
       mysql_query($sql);
       echo (!mysql_affetced_rows()) ? "Query Wrong" : "Query Okay";
       $i++;
     } 
    }
于 2015-10-03T05:36:51.367 回答
0

下面是如何一次插入多行的示例

$query_string = "INSERT INTO YOURTBL_NAME(column_1,column_2)VALUES";
    $data ="";
    for($i=0;$i<count($YOUR FILE ARRAY);$i++)
    {
        $data .='("'.$paramater_1[$i].'","'.$paramater_2.'"),';
    }
     $qry = substr($query_string.$data, 0, -1); 
     $result = mysql_query($qry);
于 2016-02-06T09:20:38.323 回答