473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multiple partial insert into two related tables

Hi this my second request at bytes,

i am having a db with two tables,
table1 - has the details of student, stud_id as primary key set to auto increment, stud_name and other details

table2 - has the details of courses applied by each individual students, stud_id as foreign key and course_id as primary key

any help, i will be very thankful,

i need to create php mysql quires

a) partial insert data into two tables, for registering student in table1
b) after approval updated table2 with course selected by that particular student.
Sep 22 '10 #1
4 2771
Atli
5,058 Expert 4TB
So your "table2" is a link table? That is: the info on the courses is in another table, but "table2" links the students to by recording the student ID and the course ID for the course he applies to?
Sep 24 '10 #2
Hi Atli,

Yes students_details table is the main table, and course_details is the child table linking stud_id from the main table.

mean while i read about a function, but don't how to use it, any code example wold greatly help me.
Expand|Select|Wrap|Line Numbers
  1. mysql.insert_id();
  2.  
Sep 24 '10 #3
Atli
5,058 Expert 4TB
The function you mention is meant to return the latest AUTO_INCREMENT value generated by MySQL.

Inside a MySQL query you can use LAST_INSERT_ID() to get this.
Expand|Select|Wrap|Line Numbers
  1. SELECT LAST_INSERT_ID()
PHP implements the C version of this function as mysql_insert_id.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(mysql_query("INSERT INTO tbl(col) VALUES('something')") {
  3.     $rowID = mysql_insert_id();
  4.     echo "ID of the row that was just created is: $rowID";
  5. }
  6. ?>
As to the main question...
I'm not entirely sure what you need from us. What are having problems with? Could you show us the code that is causing the problem and explain what it is supposed to be doing?
Sep 24 '10 #4
Hi Atli,

Sorry for my late reply, i was trying the following code,
i have a radio button group, for status, if status==0 student is selected, if status == 1, student is rejected,
Expand|Select|Wrap|Line Numbers
  1. <? 
  2. include('config.php'); 
  3. if (isset($_POST['submitted'])) { 
  4. foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 
  5. $sql = "INSERT INTO `cjet`.`student_details` (`stud_name`,`stud_address`,`stud_father_name`,
  6.                                                 `stud_father_occup`,`stud_mother_name`,`stud_mother_occup`,
  7.                                                 `no_stud_bros`,`no_stud_sis`,`stud_family_income`,
  8.                                                 `sslc_board`,`sslc_my_passing`,`sslc_percent`,
  9.                                                 `puc1_board`,`puc1_my_passing`,`puc1_percent`,
  10.                                                 `puc2_board`,`puc2_my_passing`,`puc2_percent`,
  11.                                                 `grad1_board`,`grad1_my_passing`,`grad1_percent`,
  12.                                                 `grad2_board`,`grad2_my_passing`,`grad2_percent`,
  13.                                                 `grad3_board`,`grad3_my_passing`,`grad3_percent`,
  14.                                                 `any_other_board`,`any_other_my_passing`,`any_other_percent`,
  15.                                                 `course_studying`,`insti_studying`,`status`) 
  16.                                                 VALUES('{$_POST['stud_name']}','{$_POST['stud_address']}',
  17.                                                 '{$_POST['stud_father_name']}','{$_POST['stud_father_occup']}',
  18.                                                 '{$_POST['stud_mother_name']}','{$_POST['stud_mother_occup']}',
  19.                                                 '{$_POST['no_stud_bros']}','{$_POST['no_stud_sis']}',
  20.                                                 '{$_POST['stud_family_income']}',
  21.                                                 '{$_POST['sslc_board']}','{$_POST['sslc_my_passing']}','{$_POST['sslc_percent']}',
  22.                                                 '{$_POST['puc1_board']}','{$_POST['puc1_my_passing']}','{$_POST['puc1_percent']}',
  23.                                                 '{$_POST['puc2_board']}','{$_POST['puc2_my_passing']}','{$_POST['puc2_percent']}',
  24.                                                 '{$_POST['grad1_board']}','{$_POST['grad1_my_passing']}','{$_POST['grad1_percent']}',
  25.                                                 '{$_POST['grad2_board']}','{$_POST['grad2_my_passing']}','{$_POST['grad2_percent']}',
  26.                                                 '{$_POST['grad3_board']}','{$_POST['grad3_my_passing']}','{$_POST['grad3_percent']}',
  27.                                                 '{$_POST['any_other_board']}','{$_POST['any_other_my_passing']}','{$_POST['any_other_percent']}',
  28.                                                 '{$_POST['course_studying']}','{$_POST['insti_studying']}','{$_POST['status']}') "; 
  29.  
  30. mysql_query($sql) or die(mysql_error());
  31.  
  32. $statuschk = $_POST['status'];
  33. echo "$statuschk<br />";
  34. if ($statuschk == "0")
  35. {
  36.     $last_inserted_mysql_id = mysql_insert_id();
  37.  
  38.     $sql = "INSERT INTO `cjet`.`student_don_reg_history` (`dstud_id`,`stud_full_name`) VALUES('$last_inserted_mysql_id','{$_POST['stud_name']}') ";
  39.  
  40.     mysql_query($sql) or die(mysql_error());
  41. }
  42.  
  43. echo "Added row.<br />"; 
  44. printf("Last inserted record has id %d\n", mysql_insert_id());
  45. //echo "<a href='list.php'>Back To Listing</a>"; 
  46. ?>
  47.  
Sep 25 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: William Gill | last post by:
Is there a simple front-end that would let me select records from multiple related tables and edit their field data? Right now, I am opening each table individually in mysql control center,...
4
by: fip | last post by:
Hi, On DB2 7.1.2 on MVS OS 390, when I tried to do an insert with multiple row contents in the values clause: insert into table11 values('aaaa', 'fa'), ('bbbb', 'fb') I got the error: ...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
1
by: Christa Waggett | last post by:
Hi, I'm not a programmer but would appreciate some help with the following. I've been looking at various sites but cannot find the information I require. I have a table of strata plans and if we...
3
by: kashifahmed.bse.mg | last post by:
Hello All, I would like to insert dynamically created related tables by using datatable collections in ado.net. So can anyone figure out which procedure is usefull to do this task. Example will...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
5
by: upwardgazing | last post by:
I'm using Access 2003 (Access 2000 file format) and I have two tables related one-to-many called tblTempEncounter and tblTempEncounterDetails. I need to move a record from the first table with it's...
0
by: PreethiParkavi | last post by:
Hi, I am having two tables : Offer and OfferItems.Both tables having Identity fields as primary keys Offer_ID and OfferItem_ID respectively. the field "OI_OfferID" in OfferItem table relates...
3
by: simple simon | last post by:
How would I insert into multiple related tables using a table valued parameter? Is there any way to do this without using a WHILE loop? I know how to insert from a table valued parameter into one...
3
by: southpawjoe | last post by:
Hi, I would like to insert data into two related tables in a SQL Server 2008 database. I created the following stored procedure but it has a problem. When I add a record, the State table gets...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.