Saturday, June 30, 2012

Add Records to multiple tables php mysql



Add Records to multiple tables php mysql

We were discussed adding multiple records to a table in my previous post now we are going to add records in to multiple tables and joining of those table getting out put.Make following 3 tables in your my data base.

CREATE TABLE IF NOT EXISTS `divition` (
  `uid` int(40) NOT NULL AUTO_INCREMENT,
  `uname` varchar(45) NOT NULL,
  `division` varchar(45) NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `institute` (
  `iid` int(30) NOT NULL,
  `institute` varchar(40) NOT NULL,
  PRIMARY KEY (`iid`),
  UNIQUE KEY `iid` (`iid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `job_title` (
  `tid` int(30) NOT NULL,
  `job_title` varchar(40) NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Add records using a form as bellow

Name
Division
Institute
Job Title

Use following php codes to enter data into above 3 tables

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form1" id="form1" >
<table bgcolor=#5f5b0b>
  <tr bgcolor=#ffffff>
    <td >Name</td>
    <td>
      <input type="text" name="uname" size=65/>
    </td>
  </tr>
  <tr bgcolor=#ffffff>
    <td>Division</td>
    <td><input type="text" name="division" size=65 /></td>
  </tr>
  <tr bgcolor=#ffffff>
    <td>Institute</span></div></td>
    <td><input type="text" name="institute" size=65 /></td>
  </tr>
  <tr bgcolor=#ffffff>
    <td>Job Title</span></div></td>
    <td><input type="text" name="job_title" size=65 /></td>
  </tr>

 <tr bgcolor=#ffffff>
    <td colspan=2 align="right">
<input type="submit" name="save" value="Save Record"></td>
  </tr>  </table>
</form>
<?php
 $connection = mysql_connect ("localhost", "root","")
   or die  (mysql_error());

 mysql_select_db ("my_database");

  $uname=$_POST['uname'];
  $division=$_POST['division'];
  $institute=$_POST['institute'];
  $job_title=$_POST['job_title'];

 $query_1="INSERT INTO divition (uid, uname, division) VALUES (NULL, '$uname', '$division')";
  mysql_query($query_1);
 $lastid=mysql_insert_id();

$query_2="INSERT INTO institute (iid, institute) VALUES ($lastid, '$institute')";
  mysql_query($query_2);
  $query_3="INSERT INTO job_title (tid, job_title) VALUES ($lastid, '$job_title')";
  mysql_query($query_3);

  mysql_close($connection);
 
 echo "Records Added";
 ?>

Use following codes to display the records in 3 tables in single table

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_database",$con);
$query='SELECT divition.uname, divition.division, institute.institute,
        job_title.job_title
        FROM divition,institute,job_title
        WHERE divition.uid = institute.iid
        AND divition.uid = job_title.tid' ;
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)){
    echo "name".$row['uname']." <br> ";
      echo "Division :".$row['division']." <br> ";
       echo "Institute:".$row['institute']."<br> ";
        echo  "Job Title:".$row['job_title']."<br>";
      
}

mysql_close($con);
?>

27 comments:

  1. great tutorial...
    I have been searching 7 hours...
    and get the solution from your website...
    thanks bro...

    ReplyDelete
  2. Good One! Thank YOu!

    ReplyDelete
  3. I think this made me fucking clear. Thank you bro.....

    ReplyDelete
  4. You've saved me thousands of labor hours. Thanks!

    ReplyDelete
  5. Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\damn.php:28 Stack trace: #0 {main} thrown in C:\xampp\htdocs\damn.php on line 28
    what can be done for dis errorr

    ReplyDelete
  6. C3063
    ----
    ----
    matadorbet
    ----
    ----
    ----
    ----
    ----
    ----

    ReplyDelete