1

Here's the target table:

**trophies**
game_name
tr_name
tr_description
tr_color
tr_ach_value

Here's the form:

    <form action="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>
<p>&nbsp;</p>
<p>Trophies:</p>
Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_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_ach_value" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_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_ach_value" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_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_ach_value" type="text" size="4" maxlength="4" /><p></p>


Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_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_ach_value" type="text" size="4" maxlength="4" /><p></p>

<input name="submit" type="button" value="submit" />
</form>

I'm trying to get the multiple trophy records each with the same game_name inserted into the table trophies. Would I use an array for this? If so could you show me an example of how?

Thanks in advance.

2 Answers 2

1

Name your input fields like this

Trophy Name:    <input name="trophy[0][name]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="trophy[0][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="trophy[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="trophy[0][ach_value]" type="text" size="4" maxlength="4" /><p></p>

Just increase the number by one for each new set of data. Hope that helped.

Edit:

The PHP code would be like this:

<?php
    foreach($_POST['trophy'] as $trophy) {
        $sql = "INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES ('".$_POST['game_name']."', '".$trophy['name']."', '".$trophy['desc']."', '".$trophy['color']."', '".$trophy['ach_value']."')";
        mysql_query($sql);
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Guys. What would the code be to then insert the array data into the table?
Would it be: code sql = 'INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES (game_name, tr_name, tr_desc, tr_color, tr_ach_value)'; code
No problem :) And also don't forget to sanitize the values before inserting it into the database ;-) See here for details w3schools.com/PHP/php_filter.asp
1

use name[] as field name

  <imput name="tr_name[]" ...

in php all fields using a [] final in the name is an array at server side, so in your php you can iterate your multiples tr_name inside $_POST['tr_name'] variable, etc.

1 Comment

Yeah but the resulting $_POST array is kind of ungrouped. Just a personal preference I like my values to appear together in one array ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.