вы можете использовать continue;
для управления циклом foreach:
foreach($_POST['tr_name'] as $tr_name) {
if(empty($tr_name)) { continue; }
$sql3 = "INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES ('".$_POST['game_name']."', '".$tr_name['tr_name']."', '".$tr_name['tr_desc']."', '".$tr_name['tr_color']."', '".$tr_name['tr_ach_value']."')";
mysql_query($sql3);
}
Редактировать
Ваша форма будет генерировать 4 автономных массива, каждый из которых будет содержать 4 элемента, таких как имя, описание, цвет и т. Д. Это будет выглядеть так:
tr_name[0]
tr_desc[0]
tr_name[1]
tr_name[2]
...
Что вам, вероятно, нужно, так это один массив, содержащий 4 внутренних массива с 4 элементами в нем, например:
tr[0][name]
tr[0][desc]
tr[1][name]
tr[1][desc]
...
Новая форма:
<form action="http://www.yeahthatrocks.com/update.php" method="post">
Game Name: <input name="game_name" type="text" size="25" maxlength="255" /><br></br>
Release Date: <input name="release_date" type="text" size="25" /><p></p>
<p>Console:
<select name="game_console">
<option value="PS3">PS3</option>
<option value="Xbox 360">Xbox 360</option>
<option value="Both">Both</option>
</select>
Game Category:
<select name="game_category">
<option value="Retail">Retail</option>
<option value="PSN">PSN</option>
<option value="Arcade">Arcade</option>
<option value="Arcade">DLC</option>
</select>
Game Type:
<select name="game_type">
<option value="Action">Action</option>
<option value="Action RPG">Action RPG</option>
<option value="Adventure">Adventure</option>
<option value="Board">Board</option>
<option value="Card">Card</option>
<option value="Casino">Casino</option>
<option value="Educational">Educational</option>
<option value="Fighting">Fighting</option>
<option value="Flight">Flight</option>
<option value="Game Show">Game Show</option>
<option value="Hunting">Hunting</option>
<option value="Music">Music</option>
<option value="Other">Other</option>
<option value="Pinball">Pinball</option>
<option value="Platformer">Platformer</option>
<option value="Puzzle">Puzzle</option>
<option value="Racing">Racing</option>
<option value="RPG">RPG</option>
<option value="Shooter">Shooter</option>
<option value="Sports">Sports</option>
<option value="Strategy">Strategy</option>
<option value="Virtual Pet">Virtual Pet</option>
</select>
Trophy Totals:</p>
Bronze: <input name="bronze_ttl" type="text" size="3" maxlength="3" />
<br/>
Silver: <input name="Silver Total" type="text" size="3" maxlength="3" /><br/>
Gold: <input name="Gold Total" type="text" size="3" maxlength="3" /><br/>
Platinum: <input name="Platinum Total" type="text" size="3" maxlength="3" /><br/>
Hidden: <input name="Hidden Total" type="text" size="3" maxlength="3" /><br/>
Xbox Pts.: <input name="Xbox Pts Total" type="text" size="5" maxlength="5" /><br/>
</p>
<p> </p>
<p>Trophies:</p>
Trophy Name: <input name="tr[0][name]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr[0][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr[0][color]">
<option value="Bronze">Bronze</option>
<option value="Silver">Silver</option>
<option value="Gold">Gold</option>
<option value="Platinum">Platinum</option>
<option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[0][ach_value]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr[1][name]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr[1][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr[1][color]">
<option value="Bronze">Bronze</option>
<option value="Silver">Silver</option>
<option value="Gold">Gold</option>
<option value="Platinum">Platinum</option>
<option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[1][ach_value]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr[2][name]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr[2][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr[2][color]">
<option value="Bronze">Bronze</option>
<option value="Silver">Silver</option>
<option value="Gold">Gold</option>
<option value="Platinum">Platinum</option>
<option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[2][ach_value]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr[3][name]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr[3][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr[3][color]">
<option value="Bronze">Bronze</option>
<option value="Silver">Silver</option>
<option value="Gold">Gold</option>
<option value="Platinum">Platinum</option>
<option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[3][ach_value]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr[4][name]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr[4][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr[4][color]">
<option value="Bronze">Bronze</option>
<option value="Silver">Silver</option>
<option value="Gold">Gold</option>
<option value="Platinum">Platinum</option>
<option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[4][ach_value]" type="text" size="4" maxlength="4" /><p></p>
<input name="submit" type="submit" value="submit" />
</form>
Ваш код PHP теперь должен выглядеть следующим образом:
// $_POST['tr'] holds an array with trophies and the trophies' attributes
foreach($_POST['tr'] as $tr) {
/*
$tr is now a hash like:
array(
"name" => "a name you entered",
"desc" => "the description",
"color" => "black",
"ach_value" => "a value you entered"
)
*/
if(empty($tr['name'])) { continue; }
$sql3 = "INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES ('".$_POST['game_name']."', '".$tr['name']."', '".$tr['desc']."', '".$tr['color']."', '".$tr['ach_value']."')";
mysql_query($sql3);
}