Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Wa0013 - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

SQL DATABASE

Source Code

-- Create a new database


CREATE DATABASE myDB;

mysql> USE myDB;


Database changed

mysql>-CREATE TABLE for tblEmployee


mysql> CREATE TABLE tblEmployee
-> (
-> EmpID INT PRIMARY KEY,
-> EmpName VARCHAR(30) NOT NULL,
-> Designation VARCHAR(20),
-> Department VARCHAR(20),
-> Salary INT
-> );
Query OK, 0 rows affected (0.17 sec)

mysql> -- Insert data into tblEmployee table


mysql> INSERT INTO tblEmployee VALUES
-> (1, 'Alex', 'Manager', 'Sales', 45000),
-> (2, 'Balan', 'Asst Manager', 'Sales', 38000),
-> (3, 'Chandru', 'Programmer', 'IT', 40000),
-> (4, 'Dev', 'Manager', 'Production', 45000),
-> (5, 'Eshwar', 'Asst Manager', 'Production', 38000),
-> (6, 'Felix', 'HR Manager', 'HR', 42000);
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> -- Retrieving data from tblEmployee table

mysql> SELECT * FROM tblEmployee ;


+ + + + + +
| EmpID | EmpName | Designation | Department | Salary |
+ + + + + +
| 1 | Alex | Manager | Sales | 45000 |
| 2 | Balan | Asst Manager | Sales | 38000 |
| 3 | Chandru | Programmer | IT | 40000 |
| 4 | Dev | Manager | Production | 45000 |
| 5 | Eshwar | Asst Manager | Production | 38000 |
| 6 | Felix | HR Manager | HR | 42000 |
+ + + + + +
6 rows in set (0.01 sec)

mysql> -- Retrieve all employees in a particular department (e.g., 'IT')


mysql> SELECT * FROM tblEmployee WHERE Department = 'IT';
+ + + + + +
| EmpID | EmpName | Designation | Department | Salary |
+ + + + + +
| 3 | Chandru | Programmer | IT | 40000 |
+ + + + + +
1 row in set (0.01 sec)

mysql> -- Retrieve employees with a salary above a certain threshold (e.g., 42000)
mysql> SELECT * FROM tblEmployee WHERE Salary >= 42000;
+ + + + -+ +
| EmpID | EmpName | Designation | Department | Salary |
+ + + + -+ +
| 1 | Alex | Manager | Sales | 45000 |
| 4 | Dev | Manager | Production | 45000 |
| 6 | Felix | HR Manager | HR | 42000 |
+ + + + -+ +
3 rows in set (0.00 sec)
mysql> -- Update the salary of employee with ID 6
mysql> UPDATE tblEmployee SET Salary = 65000.00 WHERE EmpID = 6;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM tblEmployee ;


+ + + + + +
| EmpID | EmpName | Designation | Department | Salary |
+ + + + + +
| 1 | Alex | Manager | Sales | 45000 |

| 2 | Balan | Asst Manager| Sales | 38000 |


| 3 | Chandru | Programmer | IT | 40000 |
| 4 | Dev | Manager | Production| 45000 |
| 5 | Eshwar | Asst Manager | Production| 38000 |
| 6 | Felix | HR Manager | HR | 65000 |
+ + + + -+ +
6 rows in set (0.00 sec)

mysql> -- Delete records where the EmpID is 2

mysql> DELETE FROM tblEmployee WHERE EmpID = 2;


Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM tblEmployee ;
+ + + + + +
| EmpID | EmpName | Designation | Department | Salary |
+ + + + + +

| 1 | Alex | Manager | Sales | 45000 |


| 3 | Chandru | Programmer| IT | 40000 |
| 4 | Dev | Manager | Production | 45000 |
| 5 | Eshwar | AsstManager| Production | 38000 |
| 6 | Felix | HR Manager | HR | 65000 |
+ + + + + +5
rows in set (0.00 sec)
WEB APPLICATION WITH DATABASE USING PHP AND MYSQL

Source Code

Sql

CREATE TABLE `users` (

`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,

`username` varchar(80) NOT NULL,

`name` varchar(80) NOT NULL,

`password` varchar(80) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `users` (`id`, `username`, `name`,

`password`) VALUESINSERT INTO `users` (`id`,

`username`, `name`, `password`) VALUES (1, 'yssyogesh', 'Yogesh Singh', '12345'),

(2, 'bsonarika', 'Sonarika Bhadoria', '12345'),

(3, 'vishal', 'Vishal Sahu', '12345'),

(4, 'vijay', 'Vijay mourya', '12345');

config.php

<?php session_start();

$host = "localhost"; /* Host name */

$user = "root"; /* User */

$password = ""; /* Password */

$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user,

$password,$dbname);

// Check connection if (!$con) {

die("Connection failed: " . mysqli_connect_error());}


INDEX.PHP

<?php

include "config.php";

if(isset($_POST['but_submit'])){

$uname = mysqli_real_escape_string($con,$_POST['txt_uname']);

$password = mysqli_real_escape_string($con,$_POST['txt_pwd']);

if ($uname != "" && $password != ""){

$sql_query = "select count(*) as cntUser from users where username='".$uname."' and


password='".$password."'";

$result = mysqli_query($con,$sql_query);

$row = mysqli_fetch_array($result);

$count = $row['cntUser']; if($count > 0){

$_SESSION['uname'] = $uname; header('Location: home.php');

}else{

echo "Invalid username and password";

?>

<html>

<head>

<title>Create simple login page with PHP and MySQL</title>

<link href="style.css" rel="stylesheet" type="text/css">

</head>

<body>
<div class="container">

<form method="post" action="">

<div id="div_login">

<h1>Login</h1>

<div>

<input type="text" class="textbox" id="txt_uname" name="txt_uname"


placeholder="Username" />

</div>

<div>

<input type="password" class="textbox" id="txt_uname" name="txt_pwd"


placeholder="Password"/>

</div>

<div>

<input type="submit" value="Submit" name="but_submit" id="but_submit" />

</div>

</div>

</form>

</div>

</body>

</html>
style.css

/* Container */

.container{ width:40%; margin:0 auto;

/* Login */ #div_login{

border: 1px solid gray; border-radius: 3px; width: 470px;

height: 270px;

box-shadow: 0px 2px 2px 0px gray; margin: 0 auto;

#div_login h1{ margin-top: 0px;

font-weight: normal; padding: 10px;

background-color: cornflowerblue; color: white;

font-family: sans-serif;

#div_login div{ clear: both; margin-top: 10px; padding: 5px;

#div_login .textbox{ width: 96%;

padding: 7px;

#div_login input[type=submit]{ padding: 7px;

width: 100px;

background-color: lightseagreen; border: 0px;

color: white;

}
Home.php

<?php

include "config.php";

// Check user login or not if(!isset($_SESSION['uname'])){

header('Location: index.php');

// logout if(isset($_POST['but_logout'])){

session_destroy();

header('Location: index.php');

?>

<!doctype html>

<html>

<head></head>

<body>

<h1>Homepage</h1>

<form method='post' action="">

<input type="submit" value="Logout" name="but_logout">

</form>

</body>

</html>
CONFIG.PHP

<?php

include "config.php";

// Check user login or not if(!isset($_SESSION['uname'])){

header('Location: index.php');

// logout if(isset($_POST['but_logout'])){

session_destroy();

header('Location: index.php');

?>

<!doctype html>

<html>

<head></head>

<body>

<h1>Homepage</h1>

<form method='post' action="">

<input type="submit" value="Logout" name="but_logout">

</form>

</body>

</html>
Output:
ADDITIONAL PROGRAMS

1. Develop a program to design a simple calculator using PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
</head>
<body>
<h2>Simple Calculator</h2>
<form method="post" action="">
<label for="num1">Number 1:</label>
<input type="text" name="num1" required></br></br>
<label for="num2">Number 2:</label>
<input type="text" name="num2" required></br></br>
<label for="operation">Operation:</label>
<select name="operation" required>
<option value="add">Addition</option>
<option value="subtract">Subtraction</option>
<option value="multiply">Multiplication</option>
<option value="divide">Division</option>
</select>
<button type="submit">Calculate</button>
</form>
</body>
</html>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get user inputs
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operation = $_POST['operation'];
switch ($operation) {
case 'add':
$result = $num1 + $num2;
break;
case 'subtract':
$result = $num1 - $num2;
break;
case 'multiply':
$result = $num1 * $num2;
break;
case 'divide':
if ($num2 != 0) {

$result = $num1 / $num2;


} else {

$result = 'Cannot divide by zero';


}
break;
default:

$result = 'Invalid operation';


}

echo "Result: $result";


}
?>
Output:
2. Develop a web page for Cinema Ticket Booking System and validate Login page

Sql

CREATE TABLE users (

`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,

`username` varchar(80) NOT NULL,

`name` varchar(80) NOT NULL,

`password` varchar(80) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO users (`id`, `username`, `name`,

`password`) VALUESINSERT INTO `users` (`id`,

`username`, `name`, `password`) VALUES (1, 'yssyogesh', 'Yogesh Singh', '12345'),

(2, 'bsonarika', 'Sonarika Bhadoria', '12345'),

(3, 'vishal', 'Vishal Sahu', '12345'),

(4, 'vijay', 'Vijay mourya', '12345');

config.php

<?php session_start();

$host = "localhost"; /* Host name */

$user = "root"; /* User */

$password = ""; /* Password */

$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user,

$password,$dbname);

// Check connection if (!$con) {

die("Connection failed: " . mysqli_connect_error());}


INDEX.PHP

<?php

include "config.php";

if(isset($_POST['but_submit'])){

$uname = mysqli_real_escape_string($con,$_POST['txt_uname']);

$password = mysqli_real_escape_string($con,$_POST['txt_pwd']);

if ($uname != "" && $password != ""){

$sql_query = "select count(*) as cntUser from users where username='".$uname."' and


password=".$password."'";

$result = mysqli_query($con,$sql_query);

$row = mysqli_fetch_array($result);

$count = $row['cntUser']; if($count > 0){

$_SESSION['uname'] = $uname; header('Location: home.php');

}else{

echo "Invalid username and password";

?>
<html>

<head>

<title>Create simple login page with PHP and MySQL</title>

<link href="style.css" rel="stylesheet" type="text/css">

</head>

<body>

<div class="container">

<form method="post" action="">

<div id="div_login">

<h1>Login</h1>

<div>

<input type="text" class="textbox" id="txt_uname" name="txt_uname"


placeholder="Username" />

</div>

<div>

<input type="password" class="textbox" id="txt_uname" name="txt_pwd"


placeholder="Password"/>

</div>

<div>

<input type="submit" value="Submit" name="but_submit" id="but_submit" />

</div>

</div>

</form>

</div>

</body>

</html>
style.css

/* Container */

.container{ width:40%;

margin:0 auto;

/* Login */ #div_login{

border: 1px solid gray; border-radius: 3px; width: 470px;

height: 270px;

box-shadow: 0px 2px 2px 0px gray;

margin: 0 auto;

#div_login h1{ margin-top: 0px;

font-weight: normal; padding: 10px;

background-color: cornflowerblue; color: white;

font-family: sans-serif;

#div_login div{ clear: both; margin-top: 10px;

padding: 5px;

#div_login .textbox{ width: 96%;

padding: 7px;

#div_login input[type=submit]{ padding: 7px;

width: 100px;

background-color: lightseagreen; border: 0px;


color: white;

HOME.PHP

<?php

include "config.php";

// Check user login or not if(!isset($_SESSION['uname'])){

header('Location: index.php');

// logout if(isset($_POST['but_logout'])){

session_destroy();

header('Location: index.php');

?>

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

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

<title>Cinema Ticket Booking</title>

<style>

body {

font-family: Arial, sans-serif;

margin: 20px;

}
h1 {

text-align: center;

.movie-listings { display: flex;

flex-wrap: wrap;

justify-content: space-around;

.movie {

border: 1px solid #ccc; padding: 10px; margin: 10px;

width: 200px;

text-align: center;} #nav{

color: white;

background:#1100ff; margin:0;

display:flex;

align-items: center; justify-content: center;

body{

margin:0;

padding:0;

form{

display:flex; height:1.1em;

align-items: center; justify-content: center;

}
</style>

</head>

<body>

<div id="nav">

<h1>Cinema Ticket Booking</h1>

<form method='post' action="">

<input type="submit" value="Logout" name="but_logout">

</form>

</div>

<div class="movie-listings">

<div class="movie">

<h2>Movie 1</h2>

<p>Showtimes: 12:00 PM, 3:00 PM, 7:00 PM</p>

<a href="#">Book Now</a>

</div>

<div class="movie">

<h2>Movie 2</h2>

<p>Showtimes: 1:00 PM, 4:00 PM, 8:00 PM</p>

<a href="#">Book Now</a>

</div>

<!-- Add more movie listings as needed -->

</div>

</body>

</html>
Output:

You might also like