如何让 foreach 循环跳过数组中具有空白字段的记录?
我有这个 foreach 循环将此数组插入数据库中。我希望它跳过 tr_name 字段中没有任何内容的记录。最好的方法是什么?
foreach($_POST['tr_name'] as $tr_name) {
$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);
字段 game_name 的数据已正确插入。其余每个字段仅填充 tr_name 字段中输入内容的第一个字符。为什么会发生这种情况?
表格如下:
<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_name[0]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[0]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[0]">
<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_ach_value[0]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[1]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[1]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[1]">
<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_ach_value[1]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[2]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[2]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[2]">
<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_ach_value[2]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[3]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[3]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[3]">
<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_ach_value[3]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[4]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[4]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[4]">
<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_ach_value[4]" type="text" size="4" maxlength="4" /><p></p>
<input name="submit" type="submit" value="submit" />
</form>
I have this foreach loop to insert this array into a database. I want it to skip records that have nothing in the field tr_name. What's the best way to do that?
foreach($_POST['tr_name'] as $tr_name) {
$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);
The data for the field game_name inserted properly. Each of the rest of the fields were filled with only the first character of what was input in the tr_name field. Why would that happen?
Here's the form:
<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_name[0]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[0]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[0]">
<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_ach_value[0]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[1]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[1]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[1]">
<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_ach_value[1]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[2]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[2]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[2]">
<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_ach_value[2]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[3]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[3]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[3]">
<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_ach_value[3]" type="text" size="4" maxlength="4" /><p></p>
Trophy Name: <input name="tr_name[4]" type="text" size="50" maxlength="255" /><br/>
Descripton: <input name="tr_desc[4]" type="text" size="50" maxlength="255" /><br/>
Trophy Color: <select name="tr_color[4]">
<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_ach_value[4]" type="text" size="4" maxlength="4" /><p></p>
<input name="submit" type="submit" value="submit" />
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非 tr_name 作为多个值(并且 HTML 看起来像),否则这是正确的方法。如果它只有一个值 () 这是错误的。
让我们坚持认为您有多个输入字段。那么这就是你所要做的:
Unless tr_name as multiple values (and the HTML is lookling like ) this is the right way. If it only has one value () this is wrong.
Let's stick by thinking you've got multiple input fields. Then this is all you have to do:
这是你想要的吗?
Is this what you want?
您可以使用
continue;
来控制 foreach 循环:Edit
您的表单将生成 4 个独立数组,每个数组包含 4 个项目,如名称、描述、颜色等。它看起来像这样:
您可能想要的是一个包含 4 个内部数组的数组,其中包含 4 个项目,如下所示:
新表单:
您的 PHP 代码现在应该如下所示:
you can use
continue;
to control your foreach loop:Edit
Your form would generate 4 standalone arrays each containing 4 items like name, description, color etc. It would look like this:
What you probably want is one array containing 4 inner arrays with 4 items in it like:
The new Form:
Your PHP code now should look like: