My problem is this: I'm trying to create a simple web-based schools management app using PHP and MySql.
Now I want to assign students to a subject using a select form field like this:
I am able to get the list of available students using this:
- Code: Select all
$sql = "SELECT * FROM students ";
mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$result = mysql_query($sql)or die(mysql_error());
while ($row = mysql_fetch_array( $result)) {
$student_firstname = $row['student_firstname'];
$student_lastname = $row['student_lastname'];
$va = $student_firstname . " " . $student_lastname ;
echo "<option value='$va'>$va\n "; }
echo "</option></select>\n";
The Sql syntax I used to create the subjects and assigned students table is:
- Code: Select all
CREATE TABLE `subjects` (
`subject_id` int(11) NOT NULL,
`subject_name` varchar(50) NULL,
PRIMARY KEY (`subject_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `assigned_students` (
`subject_id` int(11) NOT NULL,
`student_id` int(7) NULL,
PRIMARY KEY (`subject_id`),
KEY `student_id` (`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table assigned_students
add constraint foreign key (subject_id)
references subjects (subject_id) on delete cascade;
alter table assigned_students
add constraint foreign key (student_id)
references students (student_number) on delete cascade;
please I want if I save the form, for the assigned students table to be inserted with ALL the students ids(student_id) and the corresponding subject ids(subject_id). Also, when I want to edit a subject, for the form select to be populated with the students assigned to the subject being edited. Something like this:
Please could someone help me with the sql and PHP code to show it in the form <select></select>.
Thanks.


