Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5842647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:02:41+00:00 2026-05-22T12:02:41+00:00

I have this foreach loop to insert this array into a database. I want

  • 0

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>&nbsp;</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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:02:42+00:00Added an answer on May 22, 2026 at 12:02 pm

    you can use continue; to control your foreach loop:

    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);
    }
    

    Edit

    Your form would generate 4 standalone arrays each containing 4 items like name, description, color etc. It would look like this:

    tr_name[0]
    tr_desc[0]
    
    tr_name[1]
    tr_name[2]
    
    ...
    

    What you probably want is one array containing 4 inner arrays with 4 items in it like:

    tr[0][name]
    tr[0][desc] 
    
    tr[1][name]
    tr[1][desc]
    ...
    

    The new 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>&nbsp;</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>
    

    Your PHP code now should look like:

    // $_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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that looks something like this: //iteration over scales foreach ($surveyScales
I have this code in jQuery, that I want to reimplement with the prototype
I have this code while($row = mysql_fetch_row($result)) { echo '<tr>'; $pk = $row[0]['ARTICLE_NO']; foreach($row
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have a MySQL query that looks like this: UPDATE `Table` SET `Column` =
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.