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" }