我的代码出了问题:
$size = count($_POST['tv_id']);
$size = count($_POST['Season']);
$size = count($_POST['EpisodeNumber']);
$size = count($_POST['EpisodeName']);
$i = 0;
while ($i < $size) {
$Season = mysql_real_escape_string($_POST['tv_id'][$i]);
$Season = mysql_real_escape_string($_POST['Season'][$i]);
$EpisodeNumber= mysql_real_escape_string($_POST['EpisodeNumber'][$i]);
$EpisodeName= mysql_real_escape_string($_POST['EpisodeName'][$i]);
$sql="INSERT INTO tvshows SET fk_id = '".$fk_id."', tv_id ='".$tv_id."', Season ='".$Season."', EpisodeNumber='".$EpisodeNumber."', EpisodeName='".$EpisodeName."'";
$query = mysql_query($sql) or die(mysql_error());
}
如果我只填写表单中的一个输入并点击提交,则提交一个,但只提交16个空白记录,只有playlist_id,其他列空白。如果我填写5列,它将提交它们,但也提交11个空格以及fk_id
我的桌面布局是这样的:
echo '<tr>';
echo "<td><input type='hidden' name='tv_id[]' id='tv_id' value='' /></td>";
echo "<td><input type='text' name='Season[]' id='Season[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeNumber[]' id='EpisodeNumber[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeName[]' id='EpisodeName[]' value='' /></td>";
echo '</tr>';
echo '<tr>';
echo "<td><input type='hidden' name='tv_id[]' id='tv_id' value='' /></td>";
echo "<td><input type='text' name='Season[]' id='Season[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeNumber[]' id='EpisodeNumber[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeName[]' id='EpisodeName[]' value='' /></td>";
echo '</tr>';
echo '<tr>';
echo "<td><input type='hidden' name='tv_id[]' id='tv_id' value='' /></td>";
echo "<td><input type='text' name='Season[]' id='Season[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeNumber[]' id='EpisodeNumber[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeName[]' id='EpisodeName[]' value='' /></td>";
echo '</tr>';
echo '<tr>';
echo "<td><input type='hidden' name='tv_id[]' id='tv_id' value='' /></td>";
echo "<td><input type='text' name='Season[]' id='Season[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeNumber[]' id='EpisodeNumber[]' value='' /></td>";
echo "<td><input type='text' name='EpisodeName[]' id='EpisodeName[]' value='' /></td>";
echo '</tr>';
并继续约10个。
答案 0 :(得分:1)
你需要做的是有一个if语句来验证你的表单,只要你有表单操作的php代码。您可以使用以下代码的某些变体。
for($i = 0; $i < count($thisVar); $i++) {
if(!empty($thisVar[$i])) {
<insert query>
}
}
$ thisVar是存储表单输入的数组,我将是存储值的数组中的位置。
作为旁注,当您连续多次定义$size
时,最后一个将是唯一的值。你可以做一个2D数组,如果你需要另一个用于循环$j
的情况,那么if语句就在那里$thisVar[$i][$j]
答案 1 :(得分:1)
我不明白为什么要将所有计数放在相同的var大小中,但对于空记录,您可以执行以下操作:
while ($i < $size) {
$Season = mysql_real_escape_string($_POST['tv_id'][$i]);
$Season = mysql_real_escape_string($_POST['Season'][$i]);
$EpisodeNumber= mysql_real_escape_string($_POST['EpisodeNumber'][$i]);
$EpisodeName= mysql_real_escape_string($_POST['EpisodeName'][$i]);
//....
if(!empty($Season) and !empty($EpisodeNumber) and ...)
{
$sql="INSERT INTO tvshows SET fk_id = '".$fk_id."', tv_id ='".$tv_id."', Season ='".$Season."', EpisodeNumber='".$EpisodeNumber."', EpisodeName='".$EpisodeName."'";
$query = mysql_query($sql) or die(mysql_error());
}
$i++; }