我将此表单提交到mySQL db中的表中。字段max
对于所有行都相同,但每行有2个唯一值。
<form action="file.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<label>Max: </label><input type="number" name="max" value="" autocomplete="off">
<label>Times (hh:mm): </label>
<input type="time" name="time1_1" value="" autocomplete="off"><span> & </span><input type="time" name="time2_1" value="" autocomplete="off">
<input type="time" name="time1_2" value="" autocomplete="off"><span> & </span><input type="time" name="time2_2" value="" autocomplete="off">
<input type="time" name="time1_3" value="" autocomplete="off"><span> & </span><input type="time" name="time2_3" value="" autocomplete="off">
<input type="time" name="time1_4" value="" autocomplete="off"><span> & </span><input type="time" name="time2_4" value="" autocomplete="off">
<input type="time" name="time1_5" value="" autocomplete="off"><span> & </span><input type="time" name="time2_5" value="" autocomplete="off">
<input type="time" name="time1_6" value="" autocomplete="off"><span> & </span><input type="time" name="time2_6" value="" autocomplete="off">
<input type="time" name="time1_7" value="" autocomplete="off"><span> & </span><input type="time" name="time2_7" value="" autocomplete="off">
<input type="time" name="time1_8" value="" autocomplete="off"><span> & </span><input type="time" name="time2_8" value="" autocomplete="off">
<input type="time" name="time1_9" value="" autocomplete="off"><span> & </span><input type="time" name="time2_9" value="" autocomplete="off">
<input type="time" name="time1_10" value="" autocomplete="off"><span> & </span><input type="time" name="time2_10" value="" autocomplete="off">
<button name="submit" type="submit">Submit</button>
</form>
该表格包含表格中的所有时间值和最大数量。
------------------------------------
| id max time1 time2 |
| 1 25 13:30 19:30 |
| 2 25 14:00 20:00 |
------------------------------------
有没有办法提交所有20个时间值(如果不是空的)而不是为每个值执行以下操作?
$sql = "INSERT INTO tbl_name (max, time1, time2) VALUES (:max, :time1, :time2)";
$stmt = $db->prepare($sql);
$stmt->execute(array(
":max" => $_POST['max'],
":time1" => $_POST['time1_1'],
":time2" => $_POST['time2_1']
));
答案 0 :(得分:1)
只需绑定所有值。
$sql = "INSERT INTO tbl_name (max, time1, time2) VALUES (?,?,?)".str_repeat(',(?,?,?)', 9);
$stmt = $db->prepare($sql);
$params = array();
for ($i = 1; $i <= 10; $i++) {
$params[] = $_POST['max'];
$params[] = $_POST["time1_$i"];
$params[] = $_POST["time2_$i"];
}
$stmt->execute($params);