Reputation: 69
Im having a problem with my code:
$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());
}
If i fill just one of my inputs in my form and hit submit it submits that one one but also 16 blank records with just the playlist_id and the other columns blank. If i filled in 5 columns it would submit them but also 11 blanks along with just the fk_id
My table layout is like this:
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>';
and keeps going for about another 10.
Upvotes: 1
Views: 501
Reputation: 941
What you need to do is have an if statement to validate your form wherever you have the php code for the form action. You can use some variant of the code below.
for($i = 0; $i < count($thisVar); $i++) {
if(!empty($thisVar[$i])) {
<insert query>
}
}
$thisVar is the array where you store the inputs from the form and i would be the place in the array where the value is stored.
As a side note, when you have $size
defined multiple times in a row, the last one will be the only value. You could do a 2D array if which case you would need another for loop with $j
and the if statement would be in there with $thisVar[$i][$j]
Upvotes: 1
Reputation: 1866
I don't understand why your are puting all counts in the same var size, but for empty records, you may do the following :
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++; }
Upvotes: 1