Top Banner
Example Codes for Web Server (Service) Using WAMP and PHP as Server Side Script Language Sunnie Chung System Setup (Installation) and Description for WAMP Server 1. Install WAMP server for PHP and MySQL Download and Follow the Setup Procedure in http://www.wampserver.com/en/ 2. Created a new company database in MySQL (You can use the same SQL script written for MS SQL Server) 3. Create a basic web application (Web Server with Webpage) using responsive bootstrap HTML framework 4. Create Stored Procedure in MySQL 5. Manage remaining web application flow in Webserver codes in PHP. Create a Stored Procedure in MySql first
13

Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

Sep 10, 2018

Download

Documents

vuongdan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

Example Codes for Web Server (Service) Using WAMP and PHP as Server Side Script Language

Sunnie Chung

System Setup (Installation) and Description for WAMP Server

1. Install WAMP server for PHP and MySQL Download and Follow the Setup Procedure in

http://www.wampserver.com/en/

2. Created a new company database in MySQL (You can use the same SQL script written for MS

SQL Server)

3. Create a basic web application (Web Server with Webpage) using responsive bootstrap HTML

framework

4. Create Stored Procedure in MySQL

5. Manage remaining web application flow in Webserver codes in PHP.

Create a Stored Procedure in MySql first

Page 2: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

CREATE PROCEDURE `SP_Insert_NewEmployee`(IN `Fname` VARCHAR(20), IN `Minit` VARCHAR(1),

IN `Lname` VARCHAR(20), IN `Ssn` TEXT, IN `Bdate` DATE, IN `Address` TEXT, IN `Sex` VARCHAR(1), IN

`Salary` VARCHAR(10), IN `Mssn` VARCHAR(50), IN `Dno` VARCHAR(50))

BEGIN

INSERT INTO `company`.`employee` (`Fname`, `Minit`, `Lname`, `Ssn`, `Bdate`, `Address`, `Sex`,

`Salary`, `Super_ssn`, `Dno`) VALUES (Fname,Minit, Lname, Ssn, Bdate, Address, Sex, Salary, Mssn,

Dno);

insert into works_on SELECT Ssn "Ssn", Pnumber, 0 FROM `project` WHERE `Dnum`=Dno;

END

Page 3: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server
Page 4: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

index.php

•its landing page, after basic login check it will redirect to employee.php

employee.php

•on click on "ADD" it will open a new page "add_employee.php" .

add_employee.php

• Page shows all fields to create employee.

•After filling all the detail click on submit button, it will call form submit event and page submit POST data to self.

add_employee.php

•POST data can be checked

•if valid then call SP_Insert_NewEmployee with newly entered data

•if employee addes sussfully page will redirect to "employee.php"

Page 5: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

Web Application Code Snappets:

Connect.php : start a session and create a database helper object and connect to database

<?php

session_start() ;

global $db ,$live_site ,$abs_path;

require_once 'db.php';

require_once 'global_functions.php';

$db = new db() ;

date_default_timezone_set("Asia/Kolkata");

?>

Page 6: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

db.php : Helper class db for database operation

<?php

/**

* Description of db

*

*

*/

class db {

public $dbh;

// Create a database connection for use by all functions in this class

function __construct() {

require_once('db_config.php');

if ($this->dbh = mysqli_connect($db_host, $db_user, $db_password, $db_name)) {

} else {

exit('Unable to connect to DB');

}

// Set every possible option to utf-8

mysqli_query($this->dbh, 'SET NAMES "utf8"');

mysqli_query($this->dbh, 'SET CHARACTER SET "utf8"');

mysqli_query($this->dbh, 'SET character_set_results = "utf8",' .

'character_set_client = "utf8", character_set_connection = "utf8",' .

'character_set_database = "utf8", character_set_server = "utf8"');

}

// All text added to the DB should be cleaned with mysqli_real_escape_string

// to block attempted SQL insertion exploits

public function escape($str) {

return mysqli_real_escape_string($this->dbh, $str);

}

// Test to see if a specific field value is already in the DB

// Return false if no, true if yes

public function in_table($table, $where) {

$query = 'SELECT * FROM ' . $table .

' WHERE ' . $where;

$result = mysqli_query($this->dbh, $query);

return mysqli_num_rows($result) > 0;

}

// Perform a generic select and return a pointer to the result

Page 7: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

public function select($query) {

$result = mysqli_query($this->dbh, $query);

return $result;

}

// Add a row to any table

public function insert($table, $field_values) {

$query = 'INSERT INTO ' . $table . ' SET ' . $field_values;

mysqli_query($this->dbh, $query);

}

// Update any row that matches a WHERE clause

public function update($table, $field_values, $where) {

$query = 'UPDATE ' . $table . ' SET ' . $field_values .

' WHERE ' . $where;

return mysqli_query($this->dbh, $query);

}

}

?>

db_config.php : site globals and settings

<?php

global $live_site,$abs_path ;

// Local

$live_site = "http://localhost/Company/" ;

$abs_path = "c://wamp/www/Company/" ;

$db_host = "localhost" ;

$db_name = "company" ;

$db_user = "root" ;

$db_password = "" ;

$project_auth_key = md5("company") ;

error_reporting(E_ALL) ;

ini_set('display_errors', 'on');

?>

employee.php : display employee list

<?php

ob_start();

Page 8: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

require_once 'includes/connect.php';

user_auth();

$SQL = "SELECT * FROM employee ORDER BY Fname,Lname ASC";

$employee = $db->select($SQL);

?>

<div class="container-fluid">

<h1 class="page-header">Employee<a class="btn btn-default col-md-offset-9" href="<?php echo

$live_site . 'add_employee.php'; ?>"><span class="glyphicon glyphicon-plus"></span>Add</a></h1>

<ol class="breadcrumb">

<li class="breadcrumb-item"><a href="<?php echo $live_site . 'dashboard.php'; ?>">Home</a></li>

<li class="breadcrumb-item active">Employee</li>

</ol>

<div class="table-responsive">

<table class="table table-striped">

<thead>

<tr>

<th>#</th>

<th>First Name</th>

<th>Last Name</th>

<th>Ssn</th>

<th>Department</th>

</tr>

</thead>

<tbody>

<?php

$no = 1;

while ($row = mysqli_fetch_object($employee)) {

$deptObj = mysqli_fetch_object($db->select("SELECT Dname FROM DEPARTMENT WHERE

Dnumber='" . $row->Dno . "'"));

$dName = $deptObj->Dname;

?>

<tr>

<td><?php echo $no++; ?></td>

<td><?php echo $row->Fname; ?></td>

<td><?php echo $row->Lname; ?></td>

<td><?php echo $row->Ssn; ?></td>

<td><?php echo $dName; ?></td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

<?php

$content = ob_get_contents();

Page 9: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

$SET = array('menu' => 1);

ob_end_clean();

load_defaultlayout($content, $SET);

?>

add_employee.php: Add employee form and call Stored Prodecure to insert a new employee tuple

<?php

ob_start();

require_once 'includes/connect.php';

user_auth();

if (isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'save') {

$Fname = $_REQUEST['fname'];

$Lname = $_REQUEST['lname'];

$Minit = $_REQUEST['minit'];

$Ssn = $_REQUEST['ssn'];

$Bdate = $_REQUEST['bdate'];

$Address = $_REQUEST['address'];

$Sex = $_REQUEST['sex'];

$Salary = $_REQUEST['salary'];

$Dno = $_REQUEST['dno'];

// find mgr_ssn from Department

$SQL = "SELECT mgr_ssn FROM department where Dnumber='" . $Dno . "'";

$dept = mysqli_fetch_object($db->select($SQL));

$Mgr_ssn = $dept->mgr_ssn;

$SQL = "CALL SP_Insert_NewEmployee('" . $Fname . "','" . $Minit . "','" . $Lname . "','" . $Ssn . "','" .

$Bdate . "','" . $Address . "','" . $Sex . "','" . $Salary . "','" . $Mgr_ssn . "','" . $Dno . "')";

if (!mysqli_query($db->dbh, $SQL)) {

$notify = [

"msg" => "Something want wrong", "code" => "ERROR"

];

} else {

$notify = [

"msg" => "Employee Added Successfully.", "code" => "SUCCESS"

];

}

$_SESSION['notify'] = $notify;

header("Location:" . $live_site . "employee.php");

exit;

}

$SQL = "SELECT * FROM employee";

$employee = $db->select($SQL);

$SQL = "SELECT * FROM department";

$department = $db->select($SQL);

Page 10: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

?>

<div class="container-fluid">

<h1 class="page-header">Add Employee</h1>

<ol class="breadcrumb">

<li class="breadcrumb-item"><a href="<?php echo $live_site . 'dashboard.php'; ?>">Home</a></li>

<li class="breadcrumb-item"><a href="<?php echo $live_site . 'dashboard.php';

?>">Employee</a></li>

<li class="breadcrumb-item active">Add Employee</li>

</ol>

<form action="#" method="post" enctype="multipart/form-data">

<div class="form-group">

<label for="fname">First Name</label>

<input type="text" class="form-control" id="fname" name="fname" placeholder="Enter First

Name. e.g John" required="required" value="">

</div>

<div class="form-group">

<label for="minit">Middle Initial</label>

<input type="text" class="form-control" id="minit" name="minit" placeholder="Enter Middle

Initial. e.g M" required="required" value="">

</div>

<div class="form-group">

<label for="lname">Last Name</label>

<input type="text" class="form-control" id="lname" name="lname" placeholder="Enter Last

Name. e.g Cle" required="required" value="">

</div>

<div class="form-group">

<label for="ssn">Social Security No</label>

<input type="number" class="form-control" id="ssn" name="ssn" placeholder="Social security

number." required="required" value="">

</div>

<div class="form-group">

<label for="bdate">Date of birth</label>

<input type="date" class="form-control" id="bdate" name="bdate" placeholder="YEAR-MM-DD"

required="required" value="">

</div>

<div class="form-group">

<label for="address">Address</label>

<textarea class="form-control" id="address" name="address" placeholder="Enter Address. e.g

2121 , Euclid Ave." required="required"></textarea>

</div>

<div class="form-group">

<label for="sex">Sex</label>

<select class="form-control" id="sex" name="sex">

<option value="M">Male</option>

<option value="F">Female</option>

</select>

</div>

Page 11: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

<div class="form-group">

<label for="salary">Salary</label>

<input type="number" class="form-control" id="salary" name="salary" placeholder="Enter salary

e.g 50000" required="required" value="">

</div>

<div class="form-group">

<label for="dno">Department</label>

<select class="form-control" id="dno" required="required" name="dno">

<?php while ($row = mysqli_fetch_object($department)) { ?>

<option value="<?php echo $row->Dnumber; ?>"><?php echo $row->Dname; ?></option>

<?php } ?>

</select>

</div>

<div class="form-group">

<button class="form-control btn btn-submit btn-primary" id="submit" name="submit"

value="save">Submit</button>

</div>

</form>

</div>

<?php

$content = ob_get_contents();

$SET = array('menu' => 1);

ob_end_clean();

load_defaultlayout($content, $SET);

?>

default_layout.php : template created to manage each page in web application, it includes minimal

jquery and boostrap library use to design in bootstrap

<?php

global $live_site,$abs_path;

// To show which menu is selected

$active[$set['menu']] = "active" ;

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- The above 3 meta tags *must* come first in the head; any other head content must come

*after* these tags -->

<meta name="description" content="">

<meta name="author" content="">

Page 12: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

<link rel="icon" href="<?php echo $live_site.'layout/images/favicon.png'; ?>">

<title>Company Dashboard</title>

<!-- Bootstrap core CSS -->

<link href="<?php echo $live_site.'layout/css/bootstrap.min.css'; ?>" rel="stylesheet">

<!-- Custom styles for this template -->

<link href="<?php echo $live_site.'layout/css/dashboard.css'; ?>" rel="stylesheet">

<link href="<?php echo $live_site.'layout/css/custom.css'; ?>" rel="stylesheet">

</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top">

<div class="container-fluid">

<div class="navbar-header">

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-

target="#navbar" aria-expanded="false" aria-controls="navbar">

<span class="sr-only">Toggle navigation</span>

<span class="icon-bar"></span>

<span class="icon-bar"></span>

<span class="icon-bar"></span>

</button>

<a class="navbar-brand" href="<?php echo $live_site.'dashboard.php';?>">Company</a>

</div>

<div id="navbar" class="navbar-collapse collapse">

<ul class="nav navbar-nav navbar-right">

<li><a href="<?php echo $live_site.'logout.php';?>">Logout</a></li>

</ul>

</div>

</div>

</nav>

<div class="container-fluid">

<div class="row">

<div class="col-sm-3 col-md-2 sidebar">

<ul class="nav nav-sidebar">

<li class="<?php echo $active[1]; ?>"><a href="<?php echo

$live_site.'employee.php';?>">Employee</a></li>

</ul>

</div>

<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

<!-- start of main content of the page -->

<?php echo $content ; ?>

<!-- End of main content of the page -->

Page 13: Example Codes for Web Server (Service) Using WAMP and PHP ...eecs.csuohio.edu/~sschung/CIS408/ExampleCodesWeb... · Example Codes for Web Server (Service) Using WAMP and PHP as Server

</div>

</div>

</div>

<!-- Bootstrap core JavaScript

================================================== -->

<!-- Placed at the end of the document so the pages load faster -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script src="<?php echo $live_site.'layout/js/bootstrap.min.js'; ?>"></script>

<!-- Just to make our placeholder images work. Don't actually copy the next line! -->

<script src="<?php echo $live_site.'layout/js/holder.js' ;?>"></script>

<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->

<script src="<?php echo $live_site.'layout/js/ie10-viewport-bug-workaround.js' ;?> "></script>

</body>

</html>