Wednesday, February 12, 2020

Insert Records in Two Tables in PHP and MySQL

I wrote this code for my capstone project in my studies that enable the user to save records in two different tables using PHP and MySQL. I try to do some research on the Internet to find this code but fail so I decided to write one for my self and to share with other people who are in need of this code.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.
My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com
My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/
https://www.unlimitedbooksph.com/



Sample Program Output


Program Listing

index.php

<?php
if(count($_POST)>0) {
require_once("db.php");

$emp_id     = strtoupper($_POST['employee_id']);
$emp_name       = strtoupper($_POST['emp_name']);
$emp_position = strtoupper($_POST['position']);

            /* Second Table */
            $emp_telephone  = $_POST['telephone'];
$emp_mobile     = $_POST['mobile']; 
            $emp_email      = strtolower($_POST['email']); 

$sql_one = "INSERT INTO tbl_info(employee_id,name,position) 
    VALUES('$emp_id','$emp_name','$emp_position')";

    /* Second Table Query */

$sql_two = "INSERT INTO tbl_contacts(employee_id,telephone,mobile,email) 
    VALUES('$emp_id','$emp_telephone','$emp_mobile','$emp_email')";


mysqli_query($conn,$sql_one);
mysqli_query($conn,$sql_two);


$current_id = mysqli_insert_id($conn);
$two_id = mysqli_insert_id($conn);
if(!empty($current_id) && !empty($two_id)) {
$message = "New Employee Record Added Successfully";
} else
{
$message = "Error Try Again";
}
}
?>
<html>
<head>
<title>Add New Employee Record</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<form name="frmUser" method="post" action="">
<div style="width:500px;">
<div class="message"><?php if(isset($message)) { echo $message; } ?></div>
<table border="0" cellpadding="10" cellspacing="0" width="500" align="center" class="tblSaveForm">
<tr class="tableheader">
<td colspan="2">Add New Employee Record</td>
</tr>
<tr>
<td><label>Employee ID</label></td>
<td><input type="text" name="employee_id" class="txtField"></td>
</tr>
<tr>
<td><label>Name</label></td>
<td><input type="text" name="emp_name" class="txtField"></td>
</tr>
<tr>
<td><label>Position</label></td>
<td><input type="text" name="position" class="txtField"></td>
</tr>
<tr>
<td><label>Telephone</label></td>
<td><input type="text" name="telephone" class="txtField"></td>
</tr>
<tr>
<td><label>Mobile</label></td>
<td><input type="text" name="mobile" class="txtField"></td>
</tr>
<tr>
<td><label>Email</label></td>
<td><input type="text" name="email" class="txtField"></td>
</tr>

<tr>
<td colspan="2"><input type="submit" name="submit" value="Submit" class="btnSubmit"></td>
</tr>
</table>
</div>
</form>
</body></html>

style.css

body {
font-family:Arial;
}
input {
font-family:Arial;
font-size:14px;
}
label{
font-family:Arial;
font-size:14px;
color:#999999;
}
.tblSaveForm {
border-top:2px #999999 solid;
background-color: #f8f8f8;
}
.tableheader {
background-color: #fedc4d;
}
.tablerow {
background-color: #A7D6F1;
color:white;
}
.btnSubmit {
background-color:#fd9512;
padding:5px;
border-color:#FF6600;
border-radius:4px;
color:white;
}
.message {
color: #FF0000;
text-align: center;
width: 100%;
}
.txtField {
padding: 5px;
border:#fedc4d 1px solid;
border-radius:4px;
}
.evenRow {
background-color: #E2EDF9;
font-size:12px;
color:#101010;
}
.evenRow:hover {
background-color: #ffef46;
}
.oddRow {
background-color: #B3E8FF;
font-size:12px;
color:#101010;
}
.oddRow:hover {
background-color: #ffef46;
}
.tblListForm {
border-top:2px #999999 solid;
}
.listheader {
background-color: #fedc4d;
font-size:12px;
font-weight:bold;
}
.link{
text-decoration:none;
color:#5e8fc7;
font-size:11px;
}

employees.sql

/*
SQLyog Ultimate v10.00 Beta1
MySQL - 5.5.5-10.1.38-MariaDB : Database - employees
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`employees` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `employees`;

/*Table structure for table `tbl_contacts` */

DROP TABLE IF EXISTS `tbl_contacts`;

CREATE TABLE `tbl_contacts` (
  `id_contact` int(15) NOT NULL AUTO_INCREMENT,
  `employee_id` varchar(50) DEFAULT NULL,
  `telephone` varchar(100) DEFAULT NULL,
  `mobile` varchar(100) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id_contact`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

/*Data for the table `tbl_contacts` */

insert  into `tbl_contacts`(`id_contact`,`employee_id`,`telephone`,`mobile`,`email`) values (1,'ABC1878','4335081','09173084360','jakerpomperada@gmail.com'),(2,'ABC1878','4335081','09173084360','jakerpomperada@gmail.com'),(3,'XYZ4545','546565656','56565656565','larry_emol@gmail.com');

/*Table structure for table `tbl_info` */

DROP TABLE IF EXISTS `tbl_info`;

CREATE TABLE `tbl_info` (
  `id_info` int(15) NOT NULL AUTO_INCREMENT,
  `employee_id` varchar(50) DEFAULT NULL,
  `name` varchar(100) DEFAULT NULL,
  `position` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id_info`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

/*Data for the table `tbl_info` */

insert  into `tbl_info`(`id_info`,`employee_id`,`name`,`position`) values (3,'ABC1878','JAKE','PROGRAMMER'),(4,'ABC1878','JAKE POMPERADA','WEB DEVELOPER'),(5,'XYZ4545','LARRY EMOL','VISUAL BASIC PROGRAMMER');

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;


No comments:

Post a Comment