I'm having a heck of a time trying to figure out how to loop through my posted form data and inserting to mysql. My database table contains two columns "name" and "age"
我有一点时间试图找出如何循环我发布的表单数据并插入到mysql。我的数据库表包含两列“name”和“age”
My form:
<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 个解决方案
#1
2
Currently your form returns this array -
目前您的表单返回此数组 -
$_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'=> #);
So you either want to redo your form to give the name/age the same key-
所以你要么重做你的表格,要给名字/年龄相同的钥匙 -
<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-
change your foreach loop -
改变你的foreach循环 -
$returnedData = $_POST['data'];
for($i=0;$i<count($returnedData);$i+=2){
$name = $returnedData[$i]['name'];
$age = $returnedData[$i+1]['age'];
$stmt->execute();
}
#1
2
Currently your form returns this array -
目前您的表单返回此数组 -
$_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'=> #);
So you either want to redo your form to give the name/age the same key-
所以你要么重做你的表格,要给名字/年龄相同的钥匙 -
<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-
change your foreach loop -
改变你的foreach循环 -
$returnedData = $_POST['data'];
for($i=0;$i<count($returnedData);$i+=2){
$name = $returnedData[$i]['name'];
$age = $returnedData[$i+1]['age'];
$stmt->execute();
}