我有一个表格,教练可以输入多个投手进行比赛。该表单允许用户根据需要添加行,如下所示:
<tr><td><select name="pitcherteam[]" size="1">
<option>PLEASE SELECT TEAM
<option>Team 1
<option>Team 2
<option>Team 3
</select></td><td>
<input type=text size=50 name="pitcher[]"></td><td>
<select name="pitcherage[]" size="1">
<option>AGE
<option>8
<option>9
</select></td>
<td><input type="text" size=3 name="pitcherpitches[]"></td></tr>
我正在使用的 php 代码循环通过投手,如果它们不是空白,则插入 mysql 数据库:
$size_array = count($_POST['pitcher']);
for ($i=0; $i<$size_array; $i++){
if($_POST['pitcher'][$i] != ''){
$sql2="INSERT INTO pitchcounts (pitcherteam, pitcher, pitcherage, pitches)
VALUES
('$_POST[date]','$_POST[pitcherteam][$i]','$_POST[pitcher][$i]','
$_POST[pitcherage][$i]','$_POST[pitches][$i]')";
if (!mysqli_query($con,$sql2))
{
die('Error: ' . mysqli_error($con));
}
}
}
循环实际上是在更新 db - 但是 - 而不是表单中的值 - 它放入零和数组标记,如下所示:
pitcherteam pitcher pitcherage pitches
Array[0] Array[0] 0 0
Array[1] Array[1] 0 0
Array[3] Array[3] 0 0
关于发生了什么的任何想法?谢谢!