Hi,
I have created a drop down list using data from an existing mysql table and I would like that data to be inserted into a new table. The drop down list data is a primary key in the student table and I would like it to be a foreign key in the new table.
Unfortunately nothing is being inserted when the trainer submits that form into the new table using the insert query.
So I basically would like to enter some eating information for the each student in my student table in a seperate eating table.
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to
MySQL: ' . mysqli_connect_error());
if (isset($_POST['submit'])) {
// Grab the profile data from the POST
$student_id = mysqli_real_escape_string($dbc, trim($_POST['student_id']));
$break = mysqli_real_escape_string($dbc, trim($_POST['break']));
}
$query = "INSERT INTO eating (student_id, break)".
"VALUES ('$student_id', '$break')";
mysqli_close($dbc);
?>
<div id = "newstudentform">
<?php
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<fieldset><legend>Eating Information</legend>';
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to
MySQL: ' . mysqli_connect_error());
$query = "SELECT student_id, first_name, last_name FROM student WHERE trainer_id = '" . $_SESSION['trainer_id'] . "'";
$data = mysqli_query($dbc, $query);
echo '<select id = "student_id" name="student_id">';
// printing the list box select command
while($nt=mysqli_fetch_array($data)){//Array or records stored in $nt
echo '<option value=" . $nt[student_id] . ">' . $nt[first_name] . '</option>';
/ Option values are added by looping through the array /
}
echo '</select><br />';// Closing of list box
echo '<label for="break">Breakfast:</label>';
echo '<select id="break" name="break">';
echo '<option value="W">Ate Well';
echo '</option>';
echo '<option value="L">Ate Little';
echo '</option>';
echo '</select><br />';
echo '</fieldset>';
echo '<input type="submit" value="Save Eating Data" name="submit" />';
echo '</form>';
?>
</div>