PHP从数组插入mysql

时间:2013-07-09 17:04:18

标签: php mysql arrays multidimensional-array

我有一段时间试图弄清楚如何遍历我发布的表单数据并插入到mysql中。我的数据库表包含两列“name”和“age”

我的表单:

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

?>

1 个答案:

答案 0 :(得分: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>

-OR -

改变你的foreach循环 -

$returnedData = $_POST['data'];

for($i=0;$i<count($returnedData);$i+=2){
    $name = $returnedData[$i]['name'];
    $age = $returnedData[$i+1]['age'];
    $stmt->execute();
}