0

I am trying to use a form to fill my db. I use a table from the database to fill the options in the form and it works, but the value is not being passed through, I ran through my code and can't find a typo, is there an error I am not seeing?

<?php
require("db.php");
?>
<html>
   <head>
      <title>Insert a Record in MySQL Database</title>
   </head>

   <body>
      <?php
         if(isset($_POST['insert']))
         {            
            $user_added = $_POST['user_added']; 
            $age_added = $_POST['age_added'];

            $insert_query = "INSERT INTO userList ";
            $insert_query .= "(user, Age) ";
            $insert_query .= "VALUES ";
            $insert_query .= "('$user_added', '$age_added')";

            $retval = mysqli_query($connection, $insert_query);

            if(!$retval )
            {
                 die ("The error is: " . mysqli_error($connection));
            }
            else
            {
            echo "Database Updated " . $age_added . " " . $user_added;
            }
         }
         else
         {
            ?>
               <form method="post" action="<?php $_PHP_SELF ?>">
                  <table width="400" border="0" cellspacing="1" cellpadding="2">
                     <tr>
                        <td width="100">User to Add</td>
                        <td><input name="user_added" type="text" id="user_added"></td>
                     </tr>
                     <tr>
                        <td width="100">Age</td>
                        <td><select name="age_added" id="age_added">
                        <?php
                            while ($selection = mysqli_fetch_assoc($age_query))
                            {
                                echo "<option value=\"{$selection['id']}\">{$selection['age']}</option>\n";
                            }
                            ?>
                            </select>
                        </td>
                     </tr>
                     <tr>
                        <td width="100"> </td>
                        <td>
                           <input name="insert" type="submit" id="insert" value="Insert">
                        </td>
                     </tr>                  
                  </table>
               </form>
            <?php
         }
      ?>
   </body>
</html>
<?php
    mysqli_close($connection);
?>

The output if I type Test in the user and select a number in the age, the age is not sent through, and it shows Database Updated Test with no age. What am I missing?

----- ADDED INFORMATION 1 -----

This is the query that creates the $age_query as requested

<?php
//Perfom DB query
$query = "SELECT * FROM PermittedAges ";
$query .= "ORDER BY Age asc";
$age_query = mysqli_query($connection, $query);

//Test for query error
if (!$age_query) {
    die ("Database query failed.");
}
?>

But I am not sure how this could be the problem since this only populates the options in the drop-down field an it works

----- ADDED INFORMATION 2 -----

Here is the var_dump($_POST) output

array(3) { ["user_added"]=> string(4) "Test" ["Age_added"]=> string(0) "" ["insert"]=> string(6) "Insert" }
Tavo
  • 173
  • 1
  • 15
  • why do you use `{...}`? try just `echo "";` – Alex Sep 08 '15 at 18:29
  • @Alex read up on php [`Complex (curly) syntax`](http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex) in the docs or here SO http://stackoverflow.com/questions/2596837/curly-braces-in-string-in-php – Sean Sep 08 '15 at 18:32
  • Where is your `$age_query` that you use in `while ($selection = mysqli_fetch_assoc($age_query))`? Does it contain the `id`? Look at your html source code that is created to see if your `value`s in you `select` are there. – Sean Sep 08 '15 at 18:34
  • so did you do `var_dump($_POST);` ? show us the output? – Alex Sep 08 '15 at 18:40
  • 1
    as you can see you didn't send any value through your post request. check your html generated, what are values set to each option? show us ` – Alex Sep 08 '15 at 18:56
  • @Alex thank you, you made me realize my mistake. The value was not being passed and I changed it to `echo "\n";` . I was using the id for the option but it was not correct. I made the change and it worked. – Tavo Sep 08 '15 at 19:03
  • you are welcome. good luck with your project! :-) – Alex Sep 08 '15 at 19:10
  • `action=""` will probably result in `action=""` as you're not echoing it. If you don't set the `action` it will submit to the current URL anyway. – rybo111 Sep 09 '15 at 13:24

2 Answers2

0

Try this

<?php
      while ($selection = mysqli_fetch_assoc($age_query))
      {
?>
      <option value="<? echo $selection['id']; ?>"> <? echo $selection['age']; ?> </option>
<?
      }
?>
Nanoturka
  • 421
  • 4
  • 15
  • your code is not good. ` echo $selection['age']; ?>` wrong syntax. and it does not change or explain anything in original code. – Alex Sep 08 '15 at 19:00
0

@Alex thank you. I realized my mistake was in my syntax. The value was not being passed and I changed it from

echo "<option value=\"{$selection['id']}\">{$selection['age']}</option>\n";

to

echo "<option value=\"{$selection['age']}\">{$selection['age']}</option>\n";

Tavo
  • 173
  • 1
  • 15