Announcements | Last Post | |
---|---|---|
SoC Curricula | 09/30/2017 01:08PM | |
Demarcation or scoping of examinations and assessment | 02/13/2017 07:59AM | |
School of Computing Short Learning Programmes | 11/24/2014 08:37AM | |
Unisa contact information | 07/28/2011 01:28PM |
Linking student to courses... April 23, 2012 01:14PM |
Registered: 12 years ago Posts: 45 Rating: 0 |
Re: Linking student to courses... April 23, 2012 01:45PM |
Registered: 12 years ago Posts: 116 Rating: 0 |
After that, insert into the course_student table using $lastid.Language: PHP$lastid=mysql_insert_id();
Re: Linking student to courses... April 23, 2012 01:50PM |
Registered: 12 years ago Posts: 116 Rating: 0 |
Re: Linking student to courses... April 23, 2012 01:58PM |
Registered: 12 years ago Posts: 96 Rating: 0 |
Re: Linking student to courses... April 23, 2012 03:58PM |
Registered: 12 years ago Posts: 45 Rating: 0 |
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: 12 years ago Posts: 17 Rating: 0 |
Re: Linking student to courses... May 10, 2012 07:26AM |
Admin Registered: 18 years ago Posts: 10,001 Rating: 353 |
Re: Linking student to courses... May 11, 2012 01:52PM |
Registered: 12 years ago Posts: 45 Rating: 0 |
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: 12 years ago Posts: 9 Rating: 0 |
> After that, insert into the course_student tableLanguage: PHP> $lastid=mysql_insert_id(); >
Re: Linking student to courses... May 11, 2012 03:02PM |
Registered: 12 years ago Posts: 77 Rating: 0 |
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: 12 years ago Posts: 45 Rating: 0 |
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: 12 years ago Posts: 77 Rating: 0 |