Some advice needed for code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Some advice needed for code

Post 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)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

I would say simply insert 4 new records into the database with 4 seperate inserts.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Post 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?
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post 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

&lt;?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')");
?&gt;
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

that's what the above does. two fields mutliple rows.
Post Reply