将多个同名输入插入数据库

时间:2018-04-03 21:08:55

标签: php arrays mysqli input store

我知道这个问题已经在这里被问过了,但是我已经经历了20多岁而无法获得以下代码才能工作

<form action="" method="post" >
 <td><input name="field1[]" value="" ></td>
 <td><input name="field2[]" value="" ></td>
 <td><input name="field3[]" value="" ></td>
 <td><input name="field1[]" value=""></td> 
 <td><input name="field2[]" value=""></td>
 <td><input name="field3[]" value=""></td>
 <td><input name="field1[]" value=""></td>
 <td><input name="field2[]" value=""></td>
 <td><input name="field3[]" value=""></td>
 <button type="submit" id="submit" name="submit" > Go </button>
</form>





<?php
if(isset($_POST["submit"])){

    foreach($_POST['field1'] as $key => $value) {

        /* Attempt MySQL server connection. Assuming you are running MySQL
        server with default setting (user 'root' with no password) */
        $link = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");

        // Check connection
        if($link === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }

        // Prepare an insert statement
        $sql = "INSERT INTO test (field1, field2, field3) VALUES (?, ?, ?)";

        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "sss", $field1, $field2, $field3);

            // Set parameters
            $field1 = $_REQUEST['field1'];
            $field2 = $_REQUEST['field2'];
            $field3 = $_REQUEST['field3'];

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                echo "Records inserted successfully.";
            } else{
                echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
            }
        } else{
            echo "ERROR: Could not prepare query: $sql. " . mysqli_error($link);
        }

        // Close statement
        mysqli_stmt_close($stmt);

        // Close connection
        mysqli_close($link);    
    }       
}
?>

我的问题很清楚 - 如何将此数组存储到数据库中。我做错了,因为我的DB存储了Array字而不是值。任何帮助都将受到大力赞赏

1 个答案:

答案 0 :(得分:2)

您正在引用$_POST['field1']这是一个数组,因此是您的结果。

您需要引用数组的项目,即$_POST['field1'][0],依此类推。

您也不需要在foreach内准备查询。在循环外执行一次并保存n *到服务器的往返行程和n *查询编译。

<?php
if(isset($_POST["submit"])){

    /* Attempt MySQL server connection. Assuming you are running MySQL
        server with default setting (user 'root' with no password) */
    $link = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");

    // Check connection
    if($link === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    // Prepare an insert statement
    $sql = "INSERT INTO test (field1, field2, field3) VALUES (?, ?, ?)";
    $stmt = mysqli_prepare($link, $sql);

    // Bind variables to the prepared statement as parameters
    mysqli_stmt_bind_param($stmt, "sss", $field1, $field2, $field3);

    foreach($_POST['field1'] as $key => $value) {

        // Set parameters
        $field1 = $_POST['field1'][$key];
        $field2 = $_POST['field2'][$key];
        $field3 = $_POST['field3'][$key];

        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            echo "Records inserted successfully.";
        } else{
            echo "ERROR: Could not execute query: $sql. " . mysqli_error($link);
        }
    }        
}
?>