Insert Data in multiple tables from multiple Tabs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Insert Data in multiple tables from multiple Tabs

Postby jawahar » Mon Jul 02, 2012 2:39 am

My table structure is

Table Name: Personnel(Parent)

student_id(int) Primary Key Auto Increment
firstname(varchar)
middlename(varchar)
lastname(varchar)
dob(date)
gender(varchar)
blood(varchar)

Table Name: contact(child)

contact_id(int) primary key auto increment
email(varchar)
phone(varchar)
mobile(varchar)
address(varchar)
state(varchar)
city(varchar)
zip(varchar)
student_id(int) Assigned Foreign key with reference of 'personnel' table


Table Name: family_detail(child)

family_detail_id(int) primary key auto increment
father_occupation(varchar)
father_designation(varchar)
father_qualification_category(varchar)
father_qualification(varchar)
father_income(varchar)
mother_occupation(varchar)
mother_designation(varchar)
mother_qualification_category(varchar)
mother_qualification(varchar)
mother_income(varchar)
student_id(int) Assigned Foreign key with reference of 'personnel' table


Table Name: previous_school(child)

previous_school_id(int) primary key auto increment
previous_school(varchar)
previous_class(varchar)
previous_percentage(varchar)
previous_rank(varchar)
board(varchar)
from_date(date)
to_date(date)
student_id(int) Assigned Foreign key with reference of 'personnel' table



Table Name: student_detail(child)

student_detail_id(int) primary key auto increment
admission_status(varchar)
admission_date(date)
class(varchar)
stream(varchar)
section(varchar)
birth_place(varchar)
nationality(varchar)
religion(varchar)
community(varchar)
hobbies(varchar)
medical_detail(varchar)
special_category(varchar)
emergency_note(varchar)
student_id(int) Assigned Foreign key with reference of 'personnel' table


I need all these details to be stored in to the database from a form.(Note: All these details are separated into more than one form)

I designed my webpage into tabs. ie.,

Tab 1 -- Basic Details
Tab 2 -- Student Details
Tab 3 -- Family Details
Tab 4 -- Previous School Details

separate php file is maintained for each tabs and it is included in a single file using the "include()" function.


My question is here.....

First I submit the form details of "Tab 1" ie., Basic Details. Then click "Save" button to store the values in the database.
Assume that the student_id is '10' here.
Then the form details of "Tab 2" ie., Student Details.
Now I want to store these details to the corresponding 'student_id'
Then "Tab 3".
Now I want to store these details to the corresponding 'student_id'
Then "Tab 4"
Now I want to store these details to the corresponding 'student_id'

My coding files are follows:

add-student.php
Syntax: [ Download ] [ Hide ]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>School Management</title>


<style type="text/css">
<!--
body {
        margin-left: 0px;
        margin-top: 0px;
}
.style2 {
        font-size: 12px;
        color: #002175;
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-weight: bold;
}
.style3 {
        color: #FF0000;
        font-size: 14;
        font-weight: bold;
}
.style4 {
        color: #999999;
        font-family: Geneva;
        font-size: 18px;
}
.style5 {
        font-size: 16px;
        font-weight: bold;
}
.style11 {font-size: 12px; font-weight: bold; color: #000099; font-family:verdana; }


-->
</style>



<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" />
</head>

<body>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="780" align="right" valign="middle"><img src="images/user-icon.png" width="17" height="22" border="0" /></td>
        <td width="98" height="30" align="right" valign="middle">&nbsp;Welcome <span class="style2">User</span></td>
        <td width="90" align="right" valign="middle"><span class="style3">Logout</span></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td colspan="3"><?php include('menu.php'); ?>       </td>
  </tr>
  <tr>
        <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
        <td width="35" height="40" align="left" valign="middle" style="background:url(images/heading-bar.png) repeat-x">&nbsp;&nbsp;&nbsp;<img src="images/student-list-icon.jpg" width="21" height="24" border="0" /></td>
    <td width="624" align="left" valign="middle" style="background:url(images/heading-bar.png) repeat-x"><span class="style4">&nbsp;<span class="style5">Student Admission Details</span></span></td>
    <td width="32%" align="right" style="background:url(images/heading-bar.png) repeat-x"><a href="add-student.php"><img src="images/add.png" width="96" height="35" /></a></td>
  </tr>
  <tr>
        <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td height="425" colspan="3" valign="top" style="background:url(images/background.png) no-repeat"><div id="TabbedPanels1" class="TabbedPanels">
      <ul class="TabbedPanelsTabGroup">
       
        <li class="TabbedPanelsTab" tabindex="0">Basic Details</li>
        <li class="TabbedPanelsTab" tabindex="0">Student Details</li>
        <li class="TabbedPanelsTab" tabindex="0">Family Details</li>
        <li class="TabbedPanelsTab" tabindex="0">Previous School Details</li>
        <li class="TabbedPanelsTab" tabindex="0">Documents</li>
      </ul>
      <div class="TabbedPanelsContentGroup">
        <div class="TabbedPanelsContent"><?php include('basic-details.php'); ?></div>
        <div class="TabbedPanelsContent"><?php include('student-detail.php'); ?></div>
        <div class="TabbedPanelsContent"><?php include('family-details.php'); ?></div>
        <div class="TabbedPanelsContent"><?php include('previous-school-details.php'); ?></div>
        <div class="TabbedPanelsContent">Under Maintanence!. We get back soon...</div>
      </div>
    </div></td>
  </tr>
  <tr>
    <td colspan="3" background="images/footer.png"><?php include('footer.php'); ?></td>
  </tr>
</table>
<script type="text/javascript">
<!--
var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
//-->
</script>
</body>
</html>
 



basic-details.php

Syntax: [ Download ] [ Hide ]
<?php
include("../includes/connection.php");

if(isset($_POST['save']))
{
        //Assign Variables//
        $FirstName      =       $_POST['FirstName'];
        $Midname        =       $_POST['Midname'];
        $LastName       =       $_POST['LastName'];
        $dob            =       $_POST['dob'];
        $gender         =       $_POST['gender'];
        $BloodGroup     =       $_POST['BloodGroup'];
       
        $email          =       $_POST['email'];
        $phone          =       $_POST['phone'];
        $mobile         =       $_POST['mobile'];
       
        $address1       =       $_POST['address1'];
        $state1         =       $_POST['state1'];
        $city1          =       $_POST['city1'];
        $zip1           =       $_POST['zip1'];
       
        $qry = "INSERT INTO personnel(firstname, middlename, lastname, dob, gender, blood) VALUES('$FirstName', '$Midname', '$LastName', '$dob', '$gender', '$BloodGroup')";
        $ins = mysql_query($qry)or die("Insert Error".mysql_error());
       
        $last_id = mysql_insert_id();
       
        $qry2 = "INSERT INTO contact(email, phone, mobile, address, state, city, zip, student_id) VALUES('$email', '$phone', '$mobile', '$address1', '$state1', '$city1','$zip1', '$last_id')";
        $ins2 = mysql_query($qry2) or die("Insert Error".mysql_error());
        $ins2_value = mysql_fetch_array($ins2);
}
?>
<form name="basicdetails" method="post" action="">
<table width="1024" border="0" cellspacing="0" cellpadding="0">
  <tr>
   <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td width="547" valign="top">
          <fieldset><legend><strong>Personal Details</strong></legend>
        <table width="62%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="95" align="right">First Name</td>
        <td width="9" align="center" valign="middle"><strong>:</strong></td>
    <td width="234"><input type="text" name="FirstName" /></td>
  </tr>
  <tr>
    <td width="95" align="right">Middle Name</td>
        <td width="9" align="center" valign="middle"><strong>:</strong></td>
    <td width="234"><input type="text" name="Midname" /></td>
  </tr>
  <tr>
    <td width="95" align="right">Last Name</td>
        <td width="9" align="center" valign="middle"><strong>:</strong></td>
    <td width="234"><input type="text" name="LastName" /></td>
  </tr>
  <tr>
    <td width="95" align="right">Date of Birth</td>
        <td width="9" align="center" valign="middle"><strong>:</strong></td>
    <td width="234"><input type="text" name="dob" /></td>
  </tr>
  <tr>
    <td width="95" align="right">Gender</td>
        <td width="9" align="center" valign="middle"><strong>:</strong></td>
    <td width="234"><select name="gender">
                <option>--select--</option>
                <option>Male</option>
                <option>Female</option>
                </select></td>
  </tr>
  <tr>
    <td width="95" align="right">Blood Group </td>
        <td width="9" align="center" valign="middle"><strong>:</strong></td>
    <td width="234"><select name="BloodGroup">
                <option>--select--</option>
                <option>A-</option>
                <option>A+</option>
                <option>AB-</option>
                <option>AB+</option>
                <option>B-</option>
                <option>B+</option>
                <option>O-</option>
                <option>O+</option>
                </select>       </td>
  </tr>
  <tr>
    <td align="right">&nbsp;</td>
    <td align="center" valign="middle">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>                                               
</fieldset>
    </td>
    <td width="28">&nbsp;</td>
    <td width="449" valign="top">
          <fieldset>
<legend><strong>Present Address</strong> </legend>
<table width="63%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="21%" align="right">Address</td>
        <td width="6%" align="center" valign="middle"><strong>:</strong></td>
    <td width="73%"><input type="text" name="address1" /></td>
  </tr>
  <tr>
    <td align="right">State</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="state1" />
       
    </td>
  </tr>
  <tr>
    <td align="right">City</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="city1" /></td>
  </tr>
  <tr>
    <td align="right">Zip</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="zip1" /></td>
  </tr>
</table>                                               
</fieldset>
    </td>
  </tr>
  <tr>
  <tr>
        <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
    <td valign="top">
        <fieldset>
<legend><strong>Contact Details</strong></legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="12%" align="right">Email</td>
        <td width="2%" align="center" valign="middle"><strong>:</strong></td>
    <td width="86%"><input type="text" name="email" /></td>
  </tr>
  <tr>
    <td align="right">Phone</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="phone" /></td>
  </tr>
  <tr>
    <td align="right">Mobile</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="mobile" /></td>
  </tr>
</table>                                               
</fieldset>
    </td>
    <td>&nbsp;</td>
    <td valign="top">
        <fieldset>
<legend><strong>Permanent Address</strong> </legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
        <td width="49%" align="right">Copy from Present</td>
        <td width="3%" align="center" valign="middle"><strong>:</strong></td>
    <td width="48%"><input type="checkbox" name="CopyPresent" /></td>
  </tr>
  <tr>
    <td width="49%" align="right">Address</td>
        <td width="3%" align="center" valign="middle"><strong>:</strong></td>
    <td width="48%"><textarea name="address" rows="5" cols="20"></textarea></td>
  </tr>
  <tr>
    <td align="right">State</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="state" /></td>
  </tr>
  <tr>
    <td align="right">City</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="city" /></td>
  </tr>
  <tr>
    <td align="right">Zip</td>
        <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="zip" /></td>
  </tr>
</table>                                               
</fieldset>
    </td>
  </tr>
  <tr>
        <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
   <td colspan="3" align="center"><input type="submit" name="save" value="Save" /></td>
  </tr>
</table>
</form>
 



student-detail.php


Syntax: [ Download ] [ Hide ]
<?php
include("../includes/connection.php");

if(isset($_REQUEST['save']))
{
        //Assign variable//
        $AdmissionStatus        =       $_REQUEST['AdmissionStatus'];
        $AdmissionDate          =       $_REQUEST['AdmissionDate'];
        $class                          =       $_REQUEST['class'];
        $stream                         =       $_REQUEST['stream'];
        $section                        =       $_REQUEST['section'];
        $BirthPlace                     =       $_REQUEST['BirthPlace'];
        $nationality            =       $_REQUEST['nationality'];
        $religion                       =       $_REQUEST['religion'];
       
        $community                      =       $_REQUEST['community'];
        $hobbies                        =       $_REQUEST['hobbies'];
        $MedicalCondition       =       $_REQUEST['MedicalCondition'];
        $category                       =       $_REQUEST['category'];
        $EmergencyNote          =       $_REQUEST['EmergencyNote'];
       
        $query = "INSERT INTO student_detail(admission_status, admission_date, class, stream, section, birth_place, nationality, religion, community, hobbies, medical_detail, special_category, emergency_note) VALUES('$AdmissionStatus', '$AdmissionDate', '$class', '$stream', '$section', '$BirthPlace', '$nationality', '$religion', '$community', '$hobbies', '$MedicalCondition', '$category', '$EmergencyNote')";
        $res = mysql_query($query) or die("Insert Error ".mysql_error());
       
}
?>
<form name="studentdetail" method="post" action="#">
<table width="1024" border="0" cellspacing="0" cellpadding="0">
  <tr>
   <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td width="547" valign="top">
         
      <fieldset>
        <legend><strong>Student Detail</strong></legend>
        <table width="62%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="163" align="right">Admission Status</td>
        <td width="10" align="center" valign="middle"><strong>:</strong></td>
    <td width="227"><select name="AdmissionStatus">
        <option>--select--</option><option>Admitted</option><option>Applied</option><option>On Hold</option><option>Rejected</option><option>Selected</option><option>Waiting List</option></select>
    </td>
    <td width="221" align="right">Community</td>
    <td width="27" align="center" valign="middle"><strong>:</strong></td>
    <td width="162"><input type="text" name="community" /></td>
  </tr>
  <tr>
    <td width="163" align="right">Admission Date</td>
        <td width="10" align="center" valign="middle"><strong>:</strong></td>
    <td width="227"><input type="text" name="AdmissionDate" /></td>
    <td width="221" align="right">Hobbies</td>
    <td width="27" align="center" valign="middle"><strong>:</strong></td>
    <td width="162"><textarea name="hobbies" rows="5" cols="18"></textarea></td>
  </tr>
  <tr>
    <td width="163" align="right">Class</td>
        <td width="10" align="center" valign="middle"><strong>:</strong></td>
    <td width="227"><input type="text" name="class" /></td>
    <td width="221" align="right">Medical Conditions</td>
    <td width="27" align="center" valign="middle"><strong>:</strong></td>
    <td width="162"><textarea name="MedicalCondition" rows="5" cols="18"></textarea></td>
  </tr>
  <tr>
    <td width="163" align="right">Stream</td>
        <td width="10" align="center" valign="middle"><strong>:</strong></td>
    <td width="227"><select name="stream">
        <option>--select--</option><option>English Medium</option><option>Tamil Medium</option></select>
    </td>
    <td width="221" align="right">Special Category</td>
    <td width="27" align="center" valign="middle"><strong>:</strong></td>
<td width="162"><select name="category">
                                <option>--Special Category--</option><option>Scheduled Caste</option><option>Backward Class</option><option>Scheduled Tribe</option>
                   </select>    </td>
  </tr>
  <tr>
    <td width="163" align="right">Section</td>
        <td width="10" align="center" valign="middle"><strong>:</strong></td>
    <td width="227"><input type="text" name="section" /></td>
    <td width="221" align="right">Emergency Note</td>
    <td width="27" align="center" valign="middle"><strong>:</strong></td>
    <td width="162"><textarea name="EmergencyNote" rows="5" cols="18"></textarea></td>
  </tr>
  <tr>
    <td width="163" align="right">Place of Birth</td>
        <td width="10" align="center" valign="middle"><strong>:</strong></td>
    <td width="227"><input type="text" name="BirthPlace" /></td>
  </tr>
  <tr>
    <td align="right">Nationality</td>
    <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="nationality"></td>
  </tr>
  <tr>
    <td align="right">Religion</td>
    <td align="center" valign="middle"><strong>:</strong></td>
    <td><input type="text" name="religion"></td>
  </tr>
  <tr>
    <td align="right">&nbsp;</td>
    <td align="center" valign="middle">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
   <td colspan="6" align="center"><input type="submit" name="save" value="Save" /></td>
  </tr>
</table>
</fieldset></td></tr></table>
</form>
 
jawahar
Forum Newbie
 
Posts: 2
Joined: Fri Jun 15, 2012 2:27 am

Re: Insert Data in multiple tables from multiple Tabs

Postby Christopher » Mon Jul 02, 2012 10:44 am

You can use hidden values or the session to hold previous values through a series of form. Also, you need to validate the values you get from the user. Show an error message if a value is not valid. Use PDO and prepared statements to escape the values. Finally, use $_POST instead of $_REQUEST.
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 12681
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US


Return to PHP - Code

Who is online

Users browsing this forum: No registered users and 14 guests