Professional Documents
Culture Documents
PHP FILE (65) FINAL
PHP FILE (65) FINAL
Practical 1
(i) Install and configure PHP, Web Server and MySQL database using
XAMPP/WAMP/LAMP/MAMP.
(ii) Create a web page that displays “Hello World.”
(i)
Step 1 : Downloading XAMPP on Windows
Click on Next
Step 3 : After Installation, Create a new folder in the htdocs folder in XAMPP
Step 4 : Open XAMPP Control Panel and Start the Apache Server
Start the
Apache
Server
Krina Amar Parikh 3 226040307065
IWD 4340704
(ii)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=devicewidth,initialscale=1.0">
<title>First php Page</title>
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Input
Output
Hello World
Practical 2
Create a web page that collects user information using a form and displays it
when the user clicks the submit button
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Web Page</title>
<style>
.myclass{
border-color: black;
max-width: 600px;
margin: 0 auto;
padding: 50px;
box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px ;
}
.form-group{
margin-block: 30px;
}
</style>
</head>
<body>
<div class="myclass">
<form action="" method="post">
<label for="fname"><b>FIRST NAME:</b></label>
<input type="text" name="name">
<br><label for="address"><b>ADDRESS:</b></label>
<input type="text" name="address">
<br><label for="gender"><b>GENDER:</b></label>
<input type="radio" id="Male" name="gender" value="Male">
<label for="Male"><b>Male</b></label>
</b>
</div>
</form>
</body>
</html>
Output
Practical 3
(i) Write a script to implement a simple calculator for mathematical operations.
Input
<!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.0">
<title>Calculator</title>
<?php $ans
= "";
if(isset($_POST['sub'])) {
$num1 = $_POST['fnum'];
$num2 = $_POST['snum'];
$op = $_POST['sub']; if($op
== '+'){
$ans = $num1 + $num2;
} else if($op == '-') {
$ans = $num1 - $num2;
} else if($op == 'x') {
$ans = $num1 * $num2;
} else if($op == '/') {
$ans = $num1 / $num2;
}
} ?>
</head>
<body>
<form action="" method="post">
<label for="firstNum">First Number: </label>
<input type="text" name="fnum"> <br><br>
<label for="secNum">Second Number: </label>
<input type="text" name="snum"> <br><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
<input type="submit" name="sub" value="x">
<input type="submit" name="sub" value="/">
<br><br>
Result: <input type="text" value="<?php echo
$ans; ?>">
</form>
</body>
</html>
Output
Input
<!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.0">
<title>Salary Calculate</title>
<?php
$bs = $da = $hra = $ma = $gs = $ins = $pf = $de =
$ns = '';
if(isset($_POST['sub'])) {
$bs = $_POST['bs'];
$da = $bs * 0.50;
$hra = $bs * 0.10;
$ma = $bs * 0.04;
$gs = $bs + $da + $hra + $ma;
Output
Practical 4
(i)Write a script that reads the name of the car and displays the name
of the company the car belongs to as per the below table:
Car Company
Safari,Nexon,Tigor,Tiago Tata
XUV700,XUV300,Bolero Mahindra
i20,Verna,Venue,Creta Hyundai
Swift,Alto,Baleno,Brezza Suzuki
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="" method="POST">
enter car name
<input type="text" name="car">
<input type="submit" name="submit">
<br>
<br>
<?php
break;
case ($car=="Swift" || $car=="Alto" || $car=="Baleno" ||
$car=="Brezza") :
echo "Suzuki";
break;
default: echo
"invalid car";
break;
}
?>
</form>
</body>
</html>
Output
(ii) Write a script to read the marks of 4 subjects and display the result as
per the below instructions
(b) If a student gets less than 35 marks in any subject, then he/she will be
marked as FAIL, otherwise he/she will be marked as PASS.
The result contains the grade of each individual subject in tabular format as
per the above table.
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$sub1=$_POST['sub1'];
$sub2=$_POST['sub2'];
$sub3=$_POST['sub3'];
$sub4=$_POST['sub4'];
switch (true) {
case($sub1>"100"):
$g1="invalid";
$r1="invalid";
break;
case ($sub1>="90"):
$g1="AA";
$r1="Pass";
break; case
($sub1>="80"):
$g1="AB";
$r1="Pass";
break; case
($sub1>="75"):
$g1="BB";
$r1="Pass";
break; case
($sub1>="70"):
$g1="BC";
$r1="Pass";
break; case
($sub1>="65"):
$g1="CC";
$r1="Pass";
break; case
($sub1>="60"):
$g1="CD";
$r1="Pass";
break; case
($sub1>="40"):
$g1="DD";
$r1="Pass";
break;
default:
$g1="FF";
$r1="FAIL";
break;
}
switch (true) {
case($sub2>"100"):
$g2="invalid";
$r2="invalid";
break; case
($sub2>="90"):
$g2="AA";
$r2="Pass";
break; case
($sub2>="80"):
$g2="AB";
$r2="Pass";
break; case
($sub2>="75"):
$g2="BB";
$r2="Pass";
break;
case ($sub2>="70"):
$g2="BC";
$r2="Pass";
break;
case ($sub2>="65"):
$g2="CC";
$r2="Pass";
break; case
($sub2>="60"):
$g2="CD";
$r2="Pass";
break; case
($sub2>="40"):
$g2="DD";
$r2="Pass";
break;
default:
$g2="FF";
$r2="FAIL";
break;
}
switch (true) {
case($sub3>"100"):
$g3="invalid";
$r3="invalid";
break; case
($sub3>="90"):
$g3="AA";
$r3="Pass";
break; case
($sub3>="80"):
$g3="AB";
$r3="Pass";
break; case
($sub3>="75"):
$g3="BB";
$r3="Pass";
break; case
($sub3>="70"):
$g3="BC";
$r3="Pass";
break; case
($sub3>="65"):
$g3="CC";
$r3="Pass";
break; case
($sub3>="60"):
$g3="CD";
$r3="Pass";
break; case
($sub3>="40"):
$g3="DD";
$r3="Pass";
break;
default:
$g3="FF";
$r3="FAIL";
break;
}
switch (true) {
case($sub4>"100"):
$g4="invalid";
$r4="invalid";
break; case
($sub4>="90"):
$g4="AA";
$r4="Pass";
break; case
($sub4>="80"):
$g4="AB";
$r4="Pass";
break; case
($sub4>="75"):
$g4="BB";
$r4="Pass";
break; case
($sub4>="70"):
$g4="BC";
$r4="Pass";
break; case
($sub4>="65"):
$g4="CC";
$r4="Pass";
break; case
($sub4>="60"):
$g4="CD";
$r4="Pass";
break; case
($sub4>="40"):
$g4="DD";
$r4="Pass";
break;
default:
$g4="FF";
$r4="FAIL";
break;
}
}
?>
<table border="5">
<th>Subject</th>
<th>grade</th>
<th>Result</th>
<tr>
<td>sub1</td>
<td><?php echo $g1;?></td>
<td><?php echo $r1;?></td>
</tr>
<tr> <td>sub2</td>
<tr>
<td>sub3</td>
<td><?php echo $g3;?></td>
Output
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="" method="POST">
ENTER NUMBER :<input type="number" name="no">
<input type="submit" name="submit">
<br>
<?php
if($_POST)
{
$f=$_POST['no'];
$f1=0;
$f2=1;
Output
(iv) Write a script to display a multiplication table for the given number.
Input
<!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.0">
<title>Multiplication Table</title>
<style>
table, th, td{
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
}
</style>
</head>
<body>
<form action="" method="post">
Enter a Number: <input type="text" name="num">
<input type="submit" name="sub">
</form> <?php
if(isset($_POST['sub'])) {
$n = $_POST['num'];
echo "<table>";
for($i = 1; $i <= 10; $i++) {
echo "<tr>"; echo
"<td>$n x $i = "; echo
($n * $i) . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body></html>
Output
Practical 5
(i) Write a script to calculate the length of a string and count the
number of words in the given string without using string functions. Input
<!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.0">
<title>String Length and Word Count</title>
</head>
<body>
<h1>Enter a string:</h1>
<form action="" method="post">
<input type="text" name="string" required>
<input type="submit" value="Calculate">
</form>
<?php if
(isset($_POST['string'])) {
$str = $_POST['string'];
$length = 0; $words
= 1; while
(isset($str[$length])) { if
($str[$length] == ' ') {
$words++;
}
$length++;
}
echo "<h2>Results</h2>";
echo "String length: $length<br>";
echo "Number of words: $words";
}
?>
</body>
</html>
Output
Input
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SORTING OF ARRAY</title>
<?php
$result = " ";
$array = array();
if (isset($_POST['SUB'])) {
$array = array(10, 30, 20, 50, 40, 70, 60, 90, 80, 100);
sort($array);
$result = implode(", ", $array);
}
?>
</head>
<body>
<form action = "" method = "POST">
<div class="container">
<label for="org">ORIGINAL ARRAY</label>
<textarea name = "org"><?php echo "10, 30, 20, 50, 40, 70, 60, 90, 80,
100";?></textarea>
<button type="submit" name="SUB">SUBMIT</button>
</div>
<div class="container">
<label for = "result">RESULTANT ARRAY</label>
<textarea name = "result"><?php echo $result;?></textarea>
</div>
</form>
IWD 4340704
</body>
</html>
Output
$res = json_encode($result);
}
?>
</head>
<tr>
<td><input type="text" name="arr1[1][0]" placeholder="10"
required></td>
<td><input type="text" name="arr1[1][1]" placeholder="20"
required></td>
<td><input type="text" name="arr1[1][2]" placeholder="30"
required></td>
</tr>
<tr>
<td><input type="text" name="arr1[2][0]" placeholder="10"
required></td>
<td><input type="text" name="arr1[2][1]" placeholder="20"
required></td>
<td><input type="text" name="arr1[2][2]" placeholder="30"
required></td>
</tr>
<tr>
<td>
<br>
<button type="submit" name="SUB1">SUBMIT</button>
</td>
</tr>
<th>MATRIX - 02</th>
<tr>
<td><input type="text" name="arr2[0][0]" placeholder="10"
Krina Amar Parikh 40 226040307065
IWD 4340704
required></td>
<td><input type="text" name="arr2[0][1]" placeholder="20"
required></td>
<td><input type="text" name="arr2[0][2]" placeholder="30"
required></td>
</tr>
<tr>
<td><input type="text" name="arr2[1][0]" placeholder="10"
required></td>
<td><input type="text" name="arr2[1][1]" placeholder="20"
required></td>
<td><input type="text" name="arr2[1][2]" placeholder="30"
required></td>
</tr>
<tr>
<td><input type="text" name="arr2[2][0]" placeholder="10"
required></td>
<td><input type="text" name="arr2[2][1]" placeholder="20"
required></td>
<td><input type="text" name="arr2[2][2]" placeholder="30"
required></td>
</tr>
<tr>
<td>
<br>
<button type="submit" name="SUB">SUBMIT</button>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
(iv) Write a script to encode a given message into equivalent Morse code.
</body>
</html>
Output
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$amount = $_POST["amount"];
function calculateNotes($amount) {
$denominations = [100, 50, 20, 10, 5, 2, 1];
$noteCount</li>";
}
}
}
echo "</ul>";
}
calculateNotes($amount);
}
?>
</body>
</html>
Output
?>
</form>
</body>
</html>
Output
Output
(iv) Write a script to display the current date and time in different
formats.
Krina Amar Parikh 50 226040307065
IWD 4340704
Input
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=devicewidth, initial-
scale=1">
<title>Date Formats</title>
</head>
<body>
<?php
echo "Today's date is
".date("d/m/Y"); echo "<br>"; echo
date("l"); echo "<br>";
date_default_timezone_set("Asia/Calcutta");
echo "The time is " . date("h:i:sa");
?>
</body>
</html>
Output
Practical 7
Output
(b). Create an object of a class and access its public properties and methods.
Input
(ii) Write a script that uses the set attribute and get attribute
methods to access a class’s private attributes of a class.
Output
}
$obj = new B();
$obj->Data();
$obj->Add();
?>
</body>
</html>
Output
trait iPhone{
public $model = "A15 Bionic";
}
function show(){
echo "<h4>Apple</h4> " . $this->name;
}
function display(){
echo "<h4>Apple Chip</h4> " . $this->model;
}
}
function getdata()
{
echo "This is method of class A <br>";
}
Krina Amar Parikh 58 226040307065
IWD 4340704
}
class B extends A{
function getdata1()
{
echo "This is method of class B which inherits class
A <br>";
}
}
class C extends B{
function getdata2()
{
echo "This is method of class C which inherits class
B <br>"; }
}
$ob = new C();
$ob->getdata();
$ob->getdata1();
$ob->getdata2();
?>
</body>
</html>
Output
</body>
</html>
interface Second {
function two();
}
Output
class MyClass {
public $name;
Output
PRACTICAL – 08
Aim: - A] Create a web page using a form to collect employee
information.
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Employee Information Form</title>
</head>
<body>
<h2>Employee Information Form</h2>
<form action="" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"
required><br><br>
<label for="designation">Designation:</label><br>
<input type="text" id="designation" name="designation"
required>
<br><br>
<label for="department">Department:</label><br>
<input type="text" id="department" name="department"
required>
<br><br>
<label for="salary">Salary:</label><br>
<input type="number" id="salary" name="salary"
required><br><br>
<input type="submit" name="sub" value="Submit">
</form>
<?php
if (isset($_POST['sub'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$designation = $_POST['designation'];
$department = $_POST['department'];
$salary = $_POST['salary'];
OUTPUT: -
Name: Krina
Email: Krina@cysec.com
Designation: Team Lead
Department: Technology
Salary: 54000
Name: Krina
Email: Krina@cysec.com
Designation: Team Lead
Department: Technology
Salary: 54000
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Employee Information Form</title>
<style>
p{
color: red;
}
</style>
</head>
<body>
<h2>Employee Information Form</h2>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name"><br><br>
Email: <input type="text" name="email"><br><br>
Designation: <input type="text" name="designation"><br><br>
Department: <input type="text" name="department"><br><br>
Salary: <input type="text" name="salary"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$error = false;
$name = $_POST['name'];
$email = $_POST['email'];
$designation = $_POST['designation'];
$department = $_POST['department'];
$salary = $_POST['salary'];
if (!preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-
Z]{2,}$/", $email)) {
echo "<p>Invalid email format</p><br>";
$error = true;
}
if (!is_numeric($salary)) {
echo "<p>Salary must be a Number</p><br>";
$error = true;
}
if (!preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-
Z]{2,}$/", $email)) {
echo "<p>Invalid email format</p><br>";
$error = true;
}
if (!is_numeric($salary)) {
echo "<p>Salary must be a Number</p><br>";
$error = true;
}
if (!$error) {
echo "<h2>Employee Information Submitted
Successfully</h2>";
echo "<p>Name: $name</p>";
echo "<p>Email: $email</p>";
echo "<p>Designation: $designation</p>";
echo "<p>Department: $department</p>";
echo "<p>Salary: $salary</p>";
}
}
?>
</body>
</html>
Name: Krina 5
Email: Krina13$gmail.com
Designation: Team Lead
Department: Technology
Salary: 54000ONLY
CODE: -
PAGE-1
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 1</title>
</head>
<body>
<h1>Page 1</h1>
<form action="page2.php" method="get">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name">
<br><br>
<label for="age">Enter your age:</label>
<input type="number" id="age" name="age">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
PAGE-2
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 2</title>
</head>
<body>
<h1>Page 2</h1>
<?php
// Retrieving data from URL using GET method
if(isset($_GET['name']) && isset($_GET['age'])){
$name = $_GET['name'];
$age = $_GET['age'];
echo "<p>Hello, $name! Your age is $age.</p>";
} else {
echo "<p>No data received.</p>";
}
?>
<p><a href="page1.php">Go back to Page 1</a></p></body></html>
CODE: -
PAGE-1
<?php
session_start();
$_SESSION["username"] = $name;
$_SESSION["email"] = $email;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 1</title>
</head>
<body>
<h1>Session - Page 1</h1>
<p>Session variables have been set.</p>
<p><a href='session2.php'>Go to Page 2</a></p>
</body>
</html>
PAGE-2
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page 2</title>
</head>
<body>
<h1>Session - Page 2</h1>
<?php
echo "<p>Welcome, " . $_SESSION["username"] . "!</p>";
echo "<p>Your email is: " . $_SESSION["email"] . "</p>";
?>
<p><a href="session1.php">Go back to Page 1</a></p>
</body>
</html>
CODE: -
PAGE-1
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cookie Demo</title>
</head>
<body>
<h1>Cookie Demo</h1>
<form action="storeCookie.php" method="post">
<label for="username">Enter your username:</label>
<input type="text" id="username" name="username">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
PAGE-2
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cookie Demo - Retrieve</title>
</head>
<body>
<h1>Cookie Demo - Retrieve</h1>
<?php
// Check if the username cookie is set
if(isset($_COOKIE['username'])){
$username = $_COOKIE['username'];
echo "<p>Hello<i>, <h3><u>$username</h3></u></i> Your
username is retrieved from the cookie.</p>";
} else {
echo "<p>Username not found in cookie.</p>";
}
?>
</body>
</html>
PAGE-3
<?php
if(isset($_POST['username'])){
$username = $_POST['username'];
setcookie("username", $username, time() + (86400 * 30), "/");
Krina Amar Parikh 75 226040307065
IWD 4340704
header("Location: retrieveCookie.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cookie Demo - Stored</title>
</head>
<body>
<h1>Cookie Demo - Stored</h1>
<?php
if(isset($_COOKIE['username'])){
$username = $_COOKIE['username'];
echo "<p>Hello, $username! Your username is stored in a
cookie.</p>";
echo "<p><a href='retrieveCookie.php'>Retrieve username from
cookie</a></p>";
} else {
echo "<p>Username not stored in cookie.</p>";
}
?>
</body> /html>
OUTPUT: -
KrinaParikh1310
PRACTICAL - 10
Aim: - A] Create a web page that reads employee information
using a form and stores it in the database.
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add Employee</title>
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Add Employee</h2>
<form action="" method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control"
id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-
control" id="password" name="password"
required>
</div>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control"
id="name" name="name" required>
</div>
<div class="form-group">
<label
for="department">Department:</label>
<input type="text" class="form-control"
id="department" name="department"
required>
</div>
<div class="form-group">
Krina Amar Parikh 78 226040307065
IWD 4340704
<label for="salary">Salary:</label>
<input type="number" class="form-control"
id="salary" name="salary" required> </div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control"
id="email" name="email" required>
</div>
<button type="submit" style="margin-bottom: 10px;"
class="btn btn- primary">Submit</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
}
?>
</body>
</html>
OUTPUT: -
Krina1310
Krina1310
Lead
130100
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";
$username = isset($_POST['username']) ?
$_POST['username'] : "";
$password = isset($_POST['password']) ?
$_POST['password'] : "";
$username = mysqli_real_escape_string($conn,
$username);
$password = mysqli_real_escape_string($conn,
$password);
if ($result->num_rows == 1) {
echo "<center><h2 class='text-
success'>Logged In
Successfully</center></h2>";
} else {
echo "<center><h2 class='text-
danger'>Invalid username or
password</h2></center>";
}
$conn->close();
}
?>
</div>
</body>
</html>
Krina131
0
Logged In Successfully
$conn->close();
} else {
// Redirect to login page if username
parameter is not set
header("Location: login.php");
exit(); // Ensure no more output is sent
}
?>
</div>
</body>
</html>
OUTPUT: -
if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
$username = isset($_POST['username']) ?
$_POST['username'] : "";
$username = mysqli_real_escape_string($conn,
$username);
OUTPUT: -
Krina1310
CODE: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>Change Password</title>
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Change Password</h2>
<form method="post">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control"
id="username" name="username"
required>
</div>
<div class="form-group">
<label for="current_password">Current
Password:</label>
<input type="password" class="form-
control" id="current_password"
name="current_password" required>
</div>
<div class="form-group">
<label for="new_password">New
Password:</label>
<input type="password" class="form-
control" id="new_password" name="new_password"
required>
</div>
<button type="submit" class="btn btn-primary"
name="submit">Change Password</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$servername = "localhost";
$username = "root";
if ($conn->connect_error) {
die("Connection failed: " . $conn-
>connect_error);
}
$username = isset($_POST['username']) ?
$_POST['username'] : "";
$current_password =
isset($_POST['current_password']) ?
$_POST['current_password'] : "";
$new_password = isset($_POST['new_password'])
? $_POST['new_password'] : "";
$username = mysqli_real_escape_string($conn,
$username);
$current_password =
mysqli_real_escape_string($conn,
$current_password);
$new_password =
mysqli_real_escape_string($conn, $new_password);
$sql = "SELECT * FROM employee WHERE
username='$username' AND
password='$current_password'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$update_sql = "UPDATE employee SET
password='$new_password' WHERE
username='$username'";
if ($conn->query($update_sql) === TRUE) {
echo "<div class='alert alert-success
mt-3' role='alert'>Password
changed successfully.</div>";
} else {
echo "<div class='alert alert-danger
mt-3' role='alert'>Error changing
password: " . $conn->error . "</div>";
}
} else {
OUTPUT: -
Krina1310
PRACTICAL - 11
Aim: - Write a script to generate a salary slip for an employee in
PDF format.
CODE: -
<?php
require_once 'vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "krinadb";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$name = $row['name'];
$department = $row['department'];
$salary = $row['salary'];
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("salary_slip.pdf",
array("Attachment" => false));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Employee Information</title>
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/c
ss/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2 class="mt-5 mb-4">Employee Information</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
Krina Amar Parikh 92 226040307065
IWD 4340704
<tbody>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $department; ?></td>
<td><?php echo $salary; ?></td>
</tr>
</tbody>
</table>
<form method="post">
<button type="submit" class="btn btn-primary"
name="generate">Generate PDF</button>
</form>
</div>
</body>
</html>
<?php
if (isset($_POST['generate'])) {
generatePDF($name, $department, $salary);
}
} else {
echo "Employee not found.";
}
$conn->close();
?>
OUTPUT: -
Krina Parikh
CSE 130100
Krina Parikh
CSE
130100