Page 1 of 1
Some advice needed for code
Posted: Wed May 22, 2002 3:44 pm
by abionifade
A student chooses 4 modules out of 9 every year to undertake as part of his course.
I will create a table in my db which has two fields - student_id and module_title.
I would like to create a form which gives the student 4 drop down boxes which are popluated from a database table (which I can code).
Question is, how can i write a script to automatically insert a new record 4 different times into my database for a particular student?
Many thanks for your advice.
Abi (newbie programmer)
Posted: Wed May 22, 2002 4:49 pm
by jason
I would say simply insert 4 new records into the database with 4 seperate inserts.
Posted: Wed May 22, 2002 5:04 pm
by volka
you may create a complex index by student_id and module_title and mark it to be unique. If a student tries to sign in twice for the same module (?) the insert will fail. I think this is what you want to preserve, isn't it?
Posted: Thu May 23, 2002 6:40 am
by abionifade
hiya guys,
the suggestion on making 4 insert statements sounds good if i could get my head around it.
What need to happen is for the student_id field to be inserted 4 times in the table along with the chosen module_title. So in thoery, when i look at the database, i should see 4 entries for a student, but each having a different module_title.
What im confused with is sending the form variables to the php script to then insert 4 times into the same column name but new record each time...
Any ideas?
Posted: Thu May 23, 2002 11:28 am
by MattF
Just use a few selects or if you wanna be really clever do multiple selects you could do <select name="choice1"><option value="option1">Option1</option></select> etc... and then make sure they haven't selected the same one twice and then do:
Code: Select all
<?php
mysql_query("INSERT INTO tablename ('$studentid','$Choice1')");
mysql_query("INSERT INTO tablename ('$studentid','$Choice2')");
mysql_query("INSERT INTO tablename ('$studentid','$Choice3')");
mysql_query("INSERT INTO tablename ('$studentid','$Choice4')");
?>
Posted: Thu May 23, 2002 12:14 pm
by abionifade
Hi and thanks for getting back to me with a great idea.
Preferably, i would like the module_title and stundent_id field to be the only two within the table.
Your example inserts student_id field with choice_1, choice_2 etc.
Is there anyway i can insert 4 individual entries each uniquely identified by the student_id field?
the table contents would look like this
ch150 Computing
ch150 Maths
ch150 Science
ch150 Physics
Im basically confused as to how to create a script to take 4 possible module_titles and assign them to a student_id like above.
Any ideas?
many thanks
Posted: Thu May 23, 2002 12:41 pm
by volka
that's what the above does. two fields mutliple rows.