Welcome! Log In Create A New Profile

Advanced

Linking student to courses...

Posted by 77528204 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Linking student to courses...
April 23, 2012 01:14PM
Ok, I managed to complete the student registration part and displaying the available courses, but now I'm struggling to link the student with a course, any advice?
I have my student submit info and course info on the same page, the problem I have is that the student number gets created when the info is submitted so how can I get the number at the same time when it is being created and use it in the course_student table? Any advice?
avatar Re: Linking student to courses...
April 23, 2012 01:45PM
Try this...
After insertion into the student table, use this code to retrieve the sno which is the primary key
Language: PHP
$lastid=mysql_insert_id();
After that, insert into the course_student table using $lastid.
avatar Re: Linking student to courses...
April 23, 2012 01:50PM
If that isnt clear enough, try to check this link out and read more about it http://php.net/manual/en/function.mysql-insert-id.php cheers!
sorry needed to edit... check this out also http://www.w3schools.com/php/func_mysql_insert_id.asp
Re: Linking student to courses...
April 23, 2012 01:58PM
Another way is to SELECT sno FROM student WHERE idno = $idnum

An ID number will always be unique...so it's another way to get the sno value.
Re: Linking student to courses...
April 23, 2012 03:58PM
Ok, so it seems I'll have to retrieve the number as soon as it is created, I'll try some methods out, thanks.
I think I'll use mysql_insert_id()

However, if you use Bigint for the id field, it might cause problems:

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
How do you create the third linking table???? So lost>>>help!!!!confused smiley
avatar
Mac
Re: Linking student to courses...
May 10, 2012 07:26AM
Re: Linking student to courses...
May 11, 2012 01:52PM
@Steve 77593448

The table that links courses and students already exists and it should get updated each time a student registers:

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
i didnt even know about this function i did it differently, but it works

77553055 Wrote:
-------------------------------------------------------
> Try this...
> After insertion into the student table, use this
> code to retrieve the sno which is the primary key
>
>
Language: PHP
> $lastid=mysql_insert_id(); >
> After that, insert into the course_student table
> using $lastid.
Re: Linking student to courses...
May 11, 2012 03:02PM
77528204 Wrote:
-------------------------------------------------------
> The table that links courses and students already
> exists and it should get updated each time a
> student registers:
>
>
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';); > } > > } > > ?> >


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?
Re: Linking student to courses...
May 11, 2012 03:56PM
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?

I'm not sure what you mean but I'm guessing its, "check how many courses are there"?

For that I have this function:

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;


Then I use $numcourses to do the loop
Re: Linking student to courses...
May 11, 2012 04:18PM
Nice one, now i see where $numcourses come from.
Sorry, you do not have permission to post/reply in this forum.