This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') | |
{ | |
$pdo = new PDO("mysql:host=localhost;dbname=demo", "root", ""); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$stmt = $pdo->prepare("INSERT INTO mytable (field1, field2) VALUES (?,?)"); | |
for ($i = 0; $i < count($_POST['field1']); $i++) | |
{ | |
$stmt->execute(array( | |
$_POST['field1'][$i], | |
$_POST['field2'][$i] | |
)); | |
} | |
} | |
?> | |
<form method="post"> | |
<b>field1 1</b><br> | |
<label>field1 <input type="text" name="field1[]"></label> | |
<br> | |
<label>field2 <input type="text" name="field2[]"></label> | |
<br> | |
<b>field1 2</b><br> | |
<label>field1 <input type="text" name="field1[]"></label> | |
<br> | |
<label>field2 <input type="text" name="field2[]"></label> | |
<input name="" type="submit" value="Submit"> | |
</form> | |
CREATE TABLE `mytable` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`field1` varchar(255) DEFAULT NULL, | |
`field2` varchar(255) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |