$min_year = 1;
$max_year = 10;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
$yearHTML = '';
$yearHTML .= '<select name="year" id="yearDrop">'.PHP_EOL;
$yearHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($years as $year) {
if (!$validSubmission && isset($_POST['year']) && $year == $_POST['year'])
{
$yearHTML .= "<option value='".$year."' selected='selected'>$year</option>".PHP_EOL;
}
else
{
$yearHTML .= "<option value='".$year."'>$year</option>".PHP_EOL;
}
}
$yearHTML .= '</select>';
Above I have a drop down menu which consists of options 1 – 10 in the drop down menu. The problem I am having is that no matter which number I select from the drop down menu, it keeps inserting the number 1 in the database. Can anybody see why in the code below why it is doing this:
$getyear = in_array($_POST['year'], $years);
$insertsql = "
INSERT INTO Student
(Year)
VALUES
(?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("i", $getyear);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
returns TRUE. True is cast to INT, so becomes 1;
Replace by: