0

我花了很长时间试图弄清楚如何遍历我发布的表单数据并插入到 mysql。我的数据库表包含两列“名称”和“年龄”

我的表格:

<form action="form.php" method="post">

<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>

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

PHP:

<?php
// Create Mysqli object
$db = new mysqli('localhost', 'root', 'root', 'database');

// Create statement object
$stmt = $db->stmt_init();


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

// Create a prepared statement
if($stmt->prepare("INSERT INTO contact (name, age) VALUES (?, ?)")) {

    // Bind your variables to replace the ?s
    $stmt->bind_param('si', $name, $age);


    $returnedData = $_POST['data'];

  foreach($returnedData as $data) {
        $name = $data['name'];
        $age = $data['age'];
        $stmt->execute();
  }


    // Close statement object
    $stmt->close();
}


}

// Close Connection
mysqli_close($link);

?>
4

1 回答 1

2

目前您的表单返回此数组 -

$_POST['data'] = array(
                       0 => array('name'=> 'string'),
                       1 => array('age'=> #),
                       2 => array('name'=> 'string'),
                       3 => array('age'=> #),
                       4 => array('name'=> 'string'),
                       5 => array('age'=> #),
                       6 => array('name'=> 'string'),
                       7 => array('age'=> #),
                       8 => array('name'=> 'string'),
                       9 => array('age'=> #),
                       10 => array('name'=> 'string'),
                       11 => array('age'=> #);

所以你要么想重做你的表格,给名字/年龄相同的键-

<form action="form.php" method="post">

<input type="text" name="data[0][name]" value=""/>
<input type="text" name="data[0][age]" value=""/>
<input type="text" name="data[1][name]" value=""/>
<input type="text" name="data[1][age]" value=""/>
<input type="text" name="data[2][name]" value=""/>
<input type="text" name="data[2][age]" value=""/>
<input type="text" name="data[3][name]" value=""/>
<input type="text" name="data[3][age]" value=""/>
<input type="text" name="data[4][name]" value=""/>
<input type="text" name="data[4][age]" value=""/>
<input type="text" name="data[5][name]" value=""/>
<input type="text" name="data[5][age]" value=""/>

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

-或者-

改变你的 foreach 循环 -

$returnedData = $_POST['data'];

for($i=0;$i<count($returnedData);$i+=2){
    $name = $returnedData[$i]['name'];
    $age = $returnedData[$i+1]['age'];
    $stmt->execute();
}
于 2013-07-09T17:35:03.547 回答