|
Linking student to courses... April 23, 2012 01:14PM | Registered: 1 year ago Posts: 55 |
April 23, 2012 01:45PM | Registered: 1 year ago Posts: 132 |
After that, insert into the course_student table using $lastid.Language: PHP$lastid=mysql_insert_id();
April 23, 2012 01:50PM | Registered: 1 year ago Posts: 132 |
|
Re: Linking student to courses... April 23, 2012 01:58PM | Registered: 1 year ago Posts: 117 |
|
Re: Linking student to courses... April 23, 2012 03:58PM | Registered: 1 year ago Posts: 55 |
Quote
mysql_insert_id() will convert the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT (64 bits) the conversion may result in an incorrect value. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query. For more information about PHP's maximum integer values, please see the integer documentation.
|
Re: Linking student to courses... May 09, 2012 11:14PM | Registered: 1 year ago Posts: 19 |
May 10, 2012 07:26AM | Admin Registered: 7 years ago Posts: 5,225 |
|
Re: Linking student to courses... May 11, 2012 01:52PM | Registered: 1 year ago Posts: 55 |
Language: PHP<?php //inserts an associative array into a table function add_to_table($arrdata, $tablename){ foreach($arrdata as $key=>$value){ $keys[] = '`' . $key . '`'; $values[] = "'" . mysql_real_escape_string($value) . "'"; } $key_list = join(',', $keys); $value_list = join(', ', $values); $SQL = "INSERT INTO `" . $tablename . "` (" . $key_list . ") VALUES (" . $value_list . ")"; $result = mysql_query($SQL); } // adds info into course_student table right after info has been added to student table $snum=mysql_insert_id(); for($i=0; $i <= $numcourses; $i++){ $courses="course".$course_ids[$i]; if (isset($_POST[$courses])){ $link=array('sno'=>$snum,'cid'=> $course_ids[$i],'year'=> date('Y')); add_to_table($link,'course_student'); } } ?>
|
Re: Linking student to courses... May 11, 2012 02:18PM | Registered: 1 year ago Posts: 12 |
> After that, insert into the course_student tableLanguage: PHP> $lastid=mysql_insert_id(); >
|
Re: Linking student to courses... May 11, 2012 03:02PM | Registered: 1 year ago Posts: 88 |
Language: PHP> <?php > > //inserts an associative array into a table > > function add_to_table($arrdata, $tablename){ > > foreach($arrdata as $key=>$value){ > $keys[] = '`' . $key . '`'; > $values[] = "'" . > mysql_real_escape_string($value) . "'"; > } > > $key_list = join(',', $keys); > $value_list = join(', ', $values); > $SQL = "INSERT INTO `" . $tablename . "` (" . > $key_list . ") VALUES (" . $value_list . ")"; > $result = mysql_query($SQL); > } > > // adds info into course_student table right > after info has been added to student table > $snum=mysql_insert_id(); > > for($i=0; $i <= $numcourses; $i++){ > $courses="course".$course_ids[$i]; > > if (isset($_POST[$courses])){ > $link=array('sno'=>$snum,'cid'=> > $course_ids[$i],'year'=> date('Y')); > add_to_table($link,'course_student'); > } > > } > > ?> >
|
Re: Linking student to courses... May 11, 2012 03:56PM | Registered: 1 year ago Posts: 55 |
Quote
Before you execute the for loop for the courses, is there a way you can check how many elements in that before you execute it?
Language: PHP//retrieve data from course table by field function course_data($fieldname){ $result = mysql_query("SELECT ".$fieldname." FROM course"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $arrdata[] = $row[$fieldname]; } return $arrdata; } $course_ids=array(); $course_ids=course_data('cid'); $numcourses=count($course_ids)-1;
|
Re: Linking student to courses... May 11, 2012 04:18PM | Registered: 1 year ago Posts: 88 |