Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

insert 2 drop down list options as new rows in a table

General discussions related to php

Moderators: macek, egami, gesf

insert 2 drop down list options as new rows in a table

Postby AnnuN » Thu Jun 20, 2013 9:00 pm

How to insert more than one drop down list option values in mysql table as new row having

Code: Select all
$last_student_id = $sql_op->insert_id;


The thing is I am recording student information, student previous education and student courses using one form. I am storing/inserting the information into 3 tables at back end in mysql. The problem is in Student Previous Education and student courses. I am using 2 text boxes to store the information of previousDegree results in StudentPrevEducation table and 3 drop down lists for the student courses so that It can be stored in StudentCourses table as 3 rows with the last insert id i.e.
Code: Select all
 `$last_student_id = $sql_op->insert_id.`


So please help me in this regard the table structure is as below

Image

Using one Text box and one drop down list to save the above data i.e. Student Info

Image

Using two text boxes to save the above data as StudentPrevEdu of each student

Image

Using 3 drop down list to save the StudentCourses data of each student

I need the final out put as *StudentCourses Table mentioned above

***The Information of StudentInfo, PreviousEducation and StudentCourses are inserted using one form.***

Please help in this regard
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby johnj » Thu Jun 20, 2013 9:53 pm

This is confusing, please be specific with your question.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: insert 2 drop down list options as new rows in a table

Postby AnnuN » Sun Jun 23, 2013 9:29 pm

I want to record student information using one input form but the data will be stored in 3 different tables i.e. linked with StudentId (Autonumber - PK) in main Studentinfo table.

I need to record student biographical information in StudentInfo and Student Courses in other table. For student courses I am using 2 or 3 dropdown list depending up on the program selected. The requirement is to store the option values i.e. courses as new row for each selected course for the student.

Please check the attached form and the sections that needs to be recorded in separate tables are highlighted i.e. Student Previous Education and Student Courses
Attachments
Student.jpg
Student Form
Student.jpg (127.37 KiB) Viewed 1020 times
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby AnnuN » Mon Jun 24, 2013 1:47 am

Is it Ok and is it clear from the Attached Pic
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby johnj » Mon Jun 24, 2013 5:05 am

Student ID is the primary key in one table and a foreign key in other two tables. So, go ahead and insert it in all the three tables. What is the issue that you are facing?
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: insert 2 drop down list options as new rows in a table

Postby AnnuN » Mon Jun 24, 2013 9:49 pm

StudentId is Auotnumber Primary Key which I am getting on submission of form through this code

Code: Select all
    $last_student_id = $sql_op->insert_id;


I want to record two courses one from each StudentCourse Dropdownlist and store it in StudentCourses table as two individual rows e.g.

Physics is the course with value 302
Maths is the course with value 303

Both should be store for example the StudentId is 21 (See the attachment)
Attachments
stdCourses.jpg
stdCourses.jpg (6.98 KiB) Viewed 980 times
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby AnnuN » Wed Jun 26, 2013 12:21 am

What is this?
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby AnnuN » Wed Jun 26, 2013 9:02 pm

Please help me I am waiting for the response.......
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby johnj » Wed Jun 26, 2013 9:49 pm

You have the last_student_id and you have the courses selected. Use these two and insert them to the table.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: insert 2 drop down list options as new rows in a table

Postby AnnuN » Thu Jun 27, 2013 12:13 am

johnj wrote:You have the last_student_id and you have the courses selected. Use these two and insert them to the table.

Here I am getting values from form using the following code

Code: Select all
      
$BACourseA = trim($dbs->escape_string(strip_tags($_POST['BACourse1'])));         
$BACourseB = trim($dbs->escape_string(strip_tags($_POST['BACourse2'])));
$custom_data['CourseSelected'][] = array($BACourseA, $BACourseB);
$custom_data['Medium'] = array('E', 'E');
$arrlength = 2;


And I used this to insert the values into Table Student Course

Code: Select all
$custom_data['StudentId'] = $last_student_id;
for ($i=1;$i<=$arrlength;$i++){
   @$sql_op->insert('student_courses', $custom_data);
   }


But I am unable to Insert anything to DB
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby johnj » Thu Jun 27, 2013 8:33 pm

1).remove the "@" sign from @sql_op->insert so that we can see the error messages thrown

2).The problem I think is with your $custom_data array. You are mixing the dimensions. You are storing
the courses as a 2 dim array while for student id it is a 1 dim array. And I cannot see if you properly
looping while picking up the courseA and courseB

It is better that you properly create the sql for the insert and then use an array to do the insert.
Properly create the iinsert means, write the insert sql properly like:
insert into student_courses (id, course1, course2) values ($last_student_id,$BACourseA,$BACourseB)..something like
this.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: insert 2 drop down list options as new rows in a table

Postby AnnuN » Sun Jun 30, 2013 8:28 pm

I want to add two rows for $Course1 and $Course2 not one row please help.
AnnuN
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Jun 20, 2013 8:36 pm

Re: insert 2 drop down list options as new rows in a table

Postby johnj » Sun Jun 30, 2013 8:30 pm

See - you already have the two rows. You have the primary key. Use a loop to add the 2 rows or write two separate sqls.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: insert 2 drop down list options as new rows in a table

Postby jaysismyson » Fri Jul 05, 2013 1:51 am

This is confusing, please be specific with Rift Platinum your question.
jaysismyson
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Jul 05, 2013 1:49 am


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.