dr.wong Posted June 16, 2006 Share Posted June 16, 2006 Hey everyone.Let say I have two tables in a MySQL database. Once called 'Marks' and another called 'Students'The table 'Students' has two columns being : "Name" and "Student_ID"andThe table 'Marks' has three columns being "ID" (Auto Increment), "Student_ID" and "Mark"Now lets say that we wanted to write a script that kept a record of the students marks.The table 'Students' would be used to generate a form on the page, basically pulling all the names and Student ID's out of its rows and using a loop to show all of the students in the table.So if we had three students, the form would look like this :John |__markfield__| |__ID Field__|Mike |__markfield__| |__ID Field__|Tony|__markfield__| |__ID Field__|The field 'ID FIELD' is hidden and has a value of the students unique ID. The 'markfield' is an input where you can put the students mark. When we press submit, we want all of the marks and their corresponding student ID's to be placed into the table 'Marks'.Later, if we wanted to retrieve any students marks, we could just call 'Select from MARKS where Student_ID = 110115' (or what ever the students ID is) This would show ALL of his marks in a similar loop as we used to display the form.My Question is.. How do I insert all of these new rows at once? Remember that I will not know how many students are in the table 'Students' as new ones would be added all the time.Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/12152-inserting-multiple-rows-at-once/ Share on other sites More sharing options...
nogray Posted June 16, 2006 Share Posted June 16, 2006 You'll need to set the markfield and the id as arrays like this[code]Mike<input type="text" name="markfield[]" /><input type="hidden" name="students[]" value="111" />[/code]In your php, you'll run through the markfield array and set the insert query[code]$markfield_arr = $_POST['markfield'];$students_arr = $POST['students'];$query = "insert into `Marks` (`Student_ID`, `Mark`) values ";for ($i=0; $i<count($markfield_arr); $i++){ $query .= "('".$students_arr[i]."', '".$markfield_arr[i]."'), ";}$query = trim($query, ", ");$result = mysql_query($query);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12152-inserting-multiple-rows-at-once/#findComment-46361 Share on other sites More sharing options...
aebstract Posted June 16, 2006 Share Posted June 16, 2006 Is this for that teacher in the freelance section? Quote Link to comment https://forums.phpfreaks.com/topic/12152-inserting-multiple-rows-at-once/#findComment-46364 Share on other sites More sharing options...
.josh Posted June 16, 2006 Share Posted June 16, 2006 [!--quoteo(post=384665:date=Jun 16 2006, 12:20 PM:name=aebstract)--][div class=\'quotetop\']QUOTE(aebstract @ Jun 16 2006, 12:20 PM) [snapback]384665[/snapback][/div][div class=\'quotemain\'][!--quotec--]Is this for that teacher in the freelance section?[/quote]LMAO wouldn't that be funny. Quote Link to comment https://forums.phpfreaks.com/topic/12152-inserting-multiple-rows-at-once/#findComment-46375 Share on other sites More sharing options...
aebstract Posted June 16, 2006 Share Posted June 16, 2006 Yeah cause I wanted that job :( Quote Link to comment https://forums.phpfreaks.com/topic/12152-inserting-multiple-rows-at-once/#findComment-46378 Share on other sites More sharing options...
.josh Posted June 16, 2006 Share Posted June 16, 2006 well sorry.. better luck next time? i was thinking it was funny cuz he took on a job in the freelance section and is turning around asking for help on it. that is..if that's what the question is about... Quote Link to comment https://forums.phpfreaks.com/topic/12152-inserting-multiple-rows-at-once/#findComment-46385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.