Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views86 pages

PHP Final

Uploaded by

Niki Gowthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views86 pages

PHP Final

Uploaded by

Niki Gowthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

DEPARTMENT OF COMPUTER

SCIENCE
GOVERNAMENT FIRST GRADE COLLEGE
KUSHALNAGAR, KODAGU-571234

CERTIFICATE
This is to certify that Mr./Mrs./Mis ………………………………………………………………………………………………………
Bearing Register………………………………………………………………………………………… has satisfactorily completed the

Course of experiments in practical PHP and MySQL Lab BCACAPN604

Prescribed by Mangalore University 6th Sem BCA course in Laboratory of this college in
the year 2023-24
Particulars of the examination are given below:

Date of examination:

Time of examination:

Batch No. of Candidate:

Max marks for the paper:

(Sign of Lecturer in Charge) (Head of the Department)

Name:

Date:
For Official Use During Examination
This record has been valued at the University Practical Examination

1. Name of the External Examiner: Signature ………………………………………


1. Name of the Internal Examiner: Signature ……………………………………….
INDEX
PART - A
SL PROGRAM NAME PAGENO.
NO.

1 1-3

2 4-7

3 8-12
4 13-16

5 17-19

6 20-22

7 23-26
8 27-30
PART - B
SL PROGRAM NAME PAGE
NO NO
1 32-
36

2 37-
44

3 45-
48

4 49-
51

5 52-
55
56-
6 63

7 64-
69
8
70-
79
GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, ini al-scale=1.0">
< tle>Contact Form</ tle>
</head>
<body>
<?php
// Check if form is submi ed
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form data
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Display submi ed informa on
echo "<h2>Form Submission Result</h2>";

Dept Of Computer Science 1


GFGC Kushalnagar

echo "<p><strong>Name:</strong> $name</p>";


echo "<p><strong>Email:</strong> $email</p>";
echo "<p><strong>Message:</strong> $message</p>";
} else {
?>
<h2>Contact Form</h2>
<form ac on="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br>
<label for="message">Message:</label><br>
<textarea id="message" name="message" rows="4" cols="50" required>
</textarea> <br>
<input type="submit" value="Submit">
</form>
<?php
}
?>
</body>
</html>

Dept Of Computer Science 2


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 3


GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>

<html>

<head>

< tle>Armstrong Number Checker</ tle>

</head>

<body>

<h2>Armstrong Number Checker</h2>

<form method="post">

Enter a number: <input type="text" name="number">

<input type="submit" value="Check">

</form>

<?php

// Func on to check if a number is an Armstrong number

func on isArmstrong($number) {

Dept Of Computer Science 4


GFGC Kushalnagar

$sum = 0;

$temp = $number;

$digits = strlen($number);

while ($temp != 0) {

$remainder = $temp % 10;

$sum += $remainder ** $digits;

$temp = (int)($temp / 10);

if ($sum == $number) {

return true;

} else {

return false;

// Check if form is submi ed

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$inputNumber = $_POST["number"];

if (is_numeric($inputNumber)) {

if (isArmstrong($inputNumber)) {

echo "<p>$inputNumber is an Armstrong number.</p>";

} else {

echo "<p>$inputNumber is not an Armstrong number.</p>";

Dept Of Computer Science 5


GFGC Kushalnagar

} else {

echo "<p>Please enter a valid number.</p>";

?>

</body>

</html>

Dept Of Computer Science 6


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 7


GFGC Kushalnagar

WELCOME.HTML
<?php

session_start();

// Display welcome message

$username = $_SESSION['username'];

// Check if logout is clicked

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

// Unset all session variables

session_unset();

// Destroy the session

session_destroy();

// Redirect to login page

header("Loca on: index.php");

exit;

?>

<!DOCTYPE html>

Dept Of Computer Science 8


GFGC Kushalnagar

<html>

<head>

< tle>Welcome</ tle>

</head>

<body>

<h2>Welcome, <?php echo $username; ?>!</h2>

<p>This is secure area.You are logged in.</p>

<form method="post">

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

</form>

</body>

</html>

INDEX.PHP
<?php

session_start();

// Check if the user is already logged in, if yes, redirect to welcome.php

if(isset($_SESSION['username'])) {

header("Loca on: welcome.php");

exit;

// Check if the form is submi ed

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Dummy username and password for demonstra on purposes

Dept Of Computer Science 9


GFGC Kushalnagar

$uname = "username";

$pwd = "password";

$username = $_POST["username"];

$password = $_POST["password"];

// Check if username and password are correct

if ($username === $uname && $password === $pwd) {

// Set session variables

$_SESSION["username"] = $uname;

$_SESSION["password"] = $pwd;

// Redirect to welcome page

header("Loca on: welcome.php");

exit;

} else {

echo "<p>Invalid username or password!</p>";

?>

<!DOCTYPE html>

<html>

<head>

< tle>Login</ tle>

</head>

<body>

<h2>Login</h2>

Dept Of Computer Science 10


GFGC Kushalnagar

<form method="post">

<label for="username">Username:</label>

<input type="text" name="username" required><br><br>

<label for="password">Password:</label>

<input type="password" name="password" required><br><br>

<input type="submit" value="Login">

</form>

</body>

</html>

Dept Of Computer Science 11


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 12


GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>

<html>

<head>

< tle>Simple PHP Calculator</ tle>

</head>

<body>

<h2>Simple PHP Calculator</h2>

<form method="post">

<input type="text" name="n1" placeholder="Enter First number">

<select name="op">

<op on value="+">+</op on>

<op on value="-">-</op on>

<op on value="*">*</op on>

<op on value="/">/</op on>

</select>

<input type="text" name="n2" placeholder="Enter Second number">

<input type="submit" name="submit" value="Calculate">


Dept Of Computer Science 13
GFGC Kushalnagar

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$n1 = $_POST['n1'];

$n2 = $_POST['n2'];

$op = $_POST['op'];

if (!is_numeric($n1) || !is_numeric($n2)) {

echo "Please enter valid numeric operands.";

} else {

// Performing calcula on based on the selected operator

switch ($op) {

case '+':

$result = $n1 + $n2;

echo "Result: $n1 $op $n2 = $result";

break;

case '-':

$result = $n1 - $n2;

echo "Result: $n1 $op $n2 = $result";

break;

case '*':

$result = $n1 * $n2;

echo "Result: $n1 $op $n2 = $result";

break;

case '/':

Dept Of Computer Science 14


GFGC Kushalnagar

// Checking if the second operand is not zero for division

if ($n2 == 0) {

echo "Cannot divide by zero.";

} else {

$result = $n1/ $n2;

echo "Result: $n1 $op $n2 = $result";

break;

default:

echo "Invalid operator selected.";

?>

</body>

</html>

Dept Of Computer Science 15


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 16


GFGC Kushalnagar

INDEX.PHP
<html>

<head> < tle>Age Calculator</ tle></head>

<body>

<h3>Age Calculator </h3>

<form method="post" ac on="<?php echo $_SERVER['PHP_SELF'];?>">

<label for="birthdate">Enter your birth date (format: d-m-Y): </label>

<input type="text" id="birthdate" name="birthdate" required>

<input type="submit" value="Calculate Age">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$birthdate = DateTime::createFromFormat('d-m-Y', $_POST['birthdate']);

if ($birthdate) {

$now = new DateTime();

$interval = $now->diff($birthdate);

$age = array(

'years' => $interval->y,

'months' => $interval->m,

'days' => $interval->d

Dept Of Computer Science 17


GFGC Kushalnagar

);

echo "Your age is " . $age['years'] . " years, " . $age['months'] . " months, and " .
$age['days'] . " days.";

} else {

echo "Invalid birth date format. Please use d-m-Y.";

?>

</body>

</html>

Dept Of Computer Science 18


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 19


GFGC Kushalnagar

INDEX.PHP
<html>

<head>< tle>Dic onary</ tle></head>

<body>

<h2>Dic onary</h2>

<form method="post" ac on="<?php echo $_SERVER['PHP_SELF'];?>">

<label for="word">Enter a word:</label>

<input type="text" id="word" name="word" required>

<input type="submit" name="submit" value="Search">

</form>

<?php

$dic onary = array(

"apple" => "A fruit that grows on trees.",

"book" => "A set of printed or wri en pages.",

"car" => "A road vehicle with four wheels.",

"dog" => "A domes cated carnivorous mammal.",

"flower" => "The seed-bearing part of a plant.",

"house" => "A building for human habita on.",

Dept Of Computer Science 20


GFGC Kushalnagar

"lamp" => "A device for giving light.",

"moon" => "The natural satellite of the earth.",

"pencil" => "An instrument for wri ng or drawing.",

"star" => "A fixed luminous point in the night sky.",

);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$search = strtolower($_POST['word']);

echo array_key_exists($search, $dic onary) ? "<p><strong>Meaning:</strong> " .


$dic onary[$search] . "</p>" : "Word not found.";

?>

</body>

</html>

Dept Of Computer Science 21


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 22


GFGC Kushalnagar

INDEX.PHP
<html>

<head>< tle>String Manipula on</ tle></head>

<body>

<h2>String Manipula on</h2>

<form method="post" ac on="<?php echo $_SERVER['PHP_SELF'];?>">

<label for="string">Enter a string:</label>

<input type="text" id="string" name="string" required><br><br>

<input type="submit" name="opera on" value="length">

<input type="submit" name="opera on" value="reverse">

<input type="submit" name="opera on" value="uppercase">

<input type="submit" name="opera on" value="lowercase">

<input type="submit" name="opera on" value="replace">

<input type="submit" name="opera on" value="palindrome">

<input type="submit" name="opera on" value="shuffle">

<input type="submit" name="opera on" value="count">

</form>

<?php

Dept Of Computer Science 23


GFGC Kushalnagar

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$strng = $_POST['string'];

$opera on = $_POST['opera on'];

switch ($opera on) {

case "length":

echo "Length of the string: " . strlen($strng);

break;

case "reverse":

echo "Reversed string: " . strrev($strng);

break;

case "uppercase":

echo "Uppercase string: " . strtoupper($strng);

break;

case "lowercase":

echo "Lowercase string: " . strtolower($strng);

break;

case "replace":

echo "Replaced string: " . str_replace('a', 'x', $strng);

break;

case "palindrome":

$reversed_string = strrev($strng);

if ($strng == $reversed_string) {

echo "The string is a palindrome.";

} else {

Dept Of Computer Science 24


GFGC Kushalnagar

echo "The string is not a palindrome."; }

break;

case "shuffle":

echo "Shuffled string: " . str_shuffle($strng);

break;

case "count":

echo "Word count: " . str_word_count($strng);

break;

default:

echo "Invalid opera on selected.";

?> </body> </html>

Dept Of Computer Science 25


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 26


GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>

<html>

<head>

< tle>Character Frequency Analyzer</ tle>

</head>

<body>

<h2>Character Frequency Analyzer</h2>

<form method="post" ac on="<?php echo $_SERVER['PHP_SELF'];?>">

<label for="input_string">Enter a string:</label>

<input type="text" id="input_string" name="input_string" required>

<input type="submit" name="analyze" value="Analyze">

</form>

Dept Of Computer Science 27


GFGC Kushalnagar

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$input_string = $_POST['input_string'];

$input_string_lower = strtolower($input_string);

$char_counts = array_count_values(str_split($input_string_lower));

ksort($char_counts);

echo "<h3>Character Frequencies:</h3>";

foreach ($char_counts as $char => $count) {

if ($char !== ' ') {

echo "'$char': $count mes<br>";

$most_used_char = array_search(max($char_counts), $char_counts);

echo "<p>The most used character is: '$most_used_char' (used " . max($char_counts) . "
mes)</p>";

$least_used_char = array_search(min($char_counts), $char_counts);

echo "<p>The least used character is: '$least_used_char' (used " . min($char_counts) . "
mes)</p>";

echo "<form method='post' ac on='" . $_SERVER['PHP_SELF'] . "'>";

echo "<input type='hidden' name='input_string' value='$input_string'>";

echo "<input type='submit' name='sort_asc' value='Sort Ascending'>";

echo "<input type='submit' name='sort_desc' value='Sort Descending'>";

Dept Of Computer Science 28


GFGC Kushalnagar

echo "</form>";

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

asort($char_counts);

} elseif (isset($_POST['sort_desc'])) {

arsort($char_counts);

echo "<h3>Sorted Character Frequencies:</h3>";

foreach ($char_counts as $char => $count) {

if ($char !== ' ') {

echo "'$char': $count mes<br>";

?>

</body>

</html>

Dept Of Computer Science 29


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 30


GFGC Kushalnagar

PART - B

Dept Of Computer Science 31


GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>
<html>
<head>
< tle>Student Registra on Form</ tle>
</head>
<body>
<h2>Student Registra on Form</h2>
<form method="post" ac on="reg.php">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" required><br><br>
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" required><br><br>
<label for="address">Address:</label>
<textarea id="address" name="address" required></textarea><br><br>
<label for="email">E-Mail:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="mobile">Mobile:</label>
<input type="text" id="mobile" name="mobile" required><br><br>
<label for="city">City:</label>
<input type="text" id="city" name="city" required><br><br>

Dept Of Computer Science 32


GFGC Kushalnagar

<label for="state">State:</label>
<input type="text" id="state" name="state" required><br><br>
<label>Gender:</label>
<input type="radio" id="male" name="gender" value="Male" required>
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="Female">
<label for="female">Female</label>
<br><br>
<label>Hobbies:</label>
<input type="checkbox" id="hobby1" name="hobbies[]" value="Reading">
<label for="hobby1">Reading</label>
<input type="checkbox" id="hobby2" name="hobbies[]" value="Sports">
<label for="hobby2">Sports</label>
<input type="checkbox" id="hobby3" name="hobbies[]" value="Music">
<label for="hobby3">Music</label>
<br><br>
<label for="blood_group">Blood Group:</label>
<select id="blood_group" name="blood_group" required>
<op on value="">Select</op on>
<op on value="A+">A+</op on>
<op on value="A-">A-</op on>
<op on value="B+">B+</op on>
<op on value="B-">B-</op on>
<op on value="AB+">AB+</op on>
<op on value="AB-">AB-</op on>
<op on value="O+">O+</op on>

Dept Of Computer Science 33


GFGC Kushalnagar

<op on value="O-">O-</op on>


</select><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

REG.PHP
<!DOCTYPE html>
<html>
<head>
< tle>Registra on Details</ tle>
</head>
<body>
<h2>Registra on Details</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<p><strong>First Name:</strong> " . $_POST['first_name'] . "</p>";
echo "<p><strong>Last Name:</strong> " . $_POST['last_name'] . "</p>";
echo "<p><strong>Address:</strong> " . $_POST['address'] . "</p>";
echo "<p><strong>E-Mail:</strong> " . $_POST['email'] . "</p>";
echo "<p><strong>Mobile:</strong> " . $_POST['mobile'] . "</p>";
echo "<p><strong>City:</strong> " . $_POST['city'] . "</p>";
echo "<p><strong>State:</strong> " . $_POST['state'] . "</p>";
echo "<p><strong>Gender:</strong> " . $_POST['gender'] . "</p>";
echo "<p><strong>Hobbies:</strong> " . implode(", ", $_POST['hobbies']) . "</p>";

Dept Of Computer Science 34


GFGC Kushalnagar

echo "<p><strong>Blood Group:</strong> " . $_POST['blood_group'] . "</p>";


}
?>
</body>
</html>

Dept Of Computer Science 35


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 36


GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>

<html lang="en">

<head>

< tle>Matrix Opera ons</ tle>

<style>

table {

border-collapse: collapse;

margin-bo om: 20px;

th, td {

padding: 8px;

Dept Of Computer Science 37


GFGC Kushalnagar

text-align: center;

border: 1px solid #ddd;

</style>

</head>

<body>

<h2>Matrix Opera ons</h2>

<form ac on="" method="post">

<label for="rows1">Rows (Matrix 1):</label>

<input type="number" id="rows1" name="rows1" min="1" required>

<label for="cols1">Columns (Matrix 1):</label>

<input type="number" id="cols1" name="cols1" min="1" required><br><br>

<label for="rows2">Rows (Matrix 2):</label>

<input type="number" id="rows2" name="rows2" min="1" required>

<label for="cols2">Columns (Matrix 2):</label>

<input type="number" id="cols2" name="cols2" min="1" required><br><br>

<input type="submit" name="generate" value="Generate Matrices">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["generate"])) {

$rows1 = $_POST["rows1"];

$cols1 = $_POST["cols1"];

$rows2 = $_POST["rows2"];

$cols2 = $_POST["cols2"];

Dept Of Computer Science 38


GFGC Kushalnagar

echo '<form ac on="" method="post">';

echo "<h3>Matrix 1:</h3>";

generateMatrixInput("matrix1", $rows1, $cols1);

echo "<h3>Matrix 2:</h3>";

generateMatrixInput("matrix2", $rows2, $cols2);

echo '<br><bu on type="submit" name="addi on">Add</bu on>';

if ($cols1 == $rows2) { // Check compa bility for mul plica on

echo '<bu on type="submit" name="mul plica on">Mul ply</bu on>';

} else {

echo '<bu on type="bu on" disabled>Mul ply</bu on>';

echo "<p>Matrices are not compa ble for mul plica on. Number of columns in

Matrix 1 must match number of rows in Matrix 2.</p>";

echo '</form>';

func on generateMatrixInput($matrixName, $rows, $cols) {

echo "<table>";

for ($i = 0; $i < $rows; $i++) {

echo "<tr>";

for ($j = 0; $j < $cols; $j++) {

echo '<td><input type="number" name="' . $matrixName . '[' . $i . '][' . $j . ']"

required></td>';

echo "</tr>";

Dept Of Computer Science 39


GFGC Kushalnagar

echo "</table>";

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["addi on"])) {

$matrix1 = $_POST["matrix1"];

$matrix2 = $_POST["matrix2"];

$result = matadd($matrix1, $matrix2);

echo "<h3>Result (Addi on):</h3>";

displayMatrix($result);

if ($_SERVER["REQUEST_METHOD"] == "POST" &&

isset($_POST["mul plica on"])) {

$matrix1 = $_POST["matrix1"];

$matrix2 = $_POST["matrix2"];

$result = matmul($matrix1, $matrix2);

echo "<h3>Result (Mul plica on):</h3>";

displayMatrix($result);

func on matadd($matrix1, $matrix2) {

$result = array();

for ($i = 0; $i < count($matrix1); $i++) {

for ($j = 0; $j < count($matrix1[0]); $j++) {

$result[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];

Dept Of Computer Science 40


GFGC Kushalnagar

return $result;

func on matmul($matrix1, $matrix2) {

$result = array();

$cols1 = count($matrix1[0]);

$rows2 = count($matrix2);

for ($i = 0; $i < count($matrix1); $i++) {

for ($j = 0; $j < count($matrix2[0]); $j++) {

$result[$i][$j] = 0;

for ($k = 0; $k < count($matrix2); $k++) {

$result[$i][$j] += $matrix1[$i][$k] * $matrix2[$k][$j];

return $result;

func on displayMatrix($matrix) {

echo "<table>";

foreach ($matrix as $row) {

echo "<tr>";

foreach ($row as $value) {

echo "<td>$value</td>";

Dept Of Computer Science 41


GFGC Kushalnagar

echo "</tr>";

echo "</table>";

?>

</body>

</html>

Dept Of Computer Science 42


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 43


GFGC Kushalnagar

Dept Of Computer Science 44


GFGC Kushalnagar

INDEX.HTML
<html lang="en">

<head>

< tle>Distance Calculator</ tle>

</head>

<body>

<h2>Distance Calculator</h2>

<form ac on="distance.php" method="post">

<label for="Distance1">Distance 1:</label><br>

<input type="number" id="feet1" name="feet1" required> feet

<input type="number" id="inches1" name="inches1" required> inches<br><br>

<label for="Distance2">Distance 2:</label><br>

<input type="number" id="feet2" name="feet2" required> feet

<input type="number" id="inches2" name="inches2" required> inches<br><br>

<input type="submit" value="Calculate">

Dept Of Computer Science 45


GFGC Kushalnagar

</form>

</body>

</html>

DISTANCE.PHP
<?php

class Distance {

public $feet;

public $inches;

public func on __construct($feet, $inches) {

$this->feet = $feet;

$this->inches = $inches;

public func on add($feet2, $inches2) {

$ = $this->feet + $feet2;

$in = $this->inches + $inches2;

if ($in >= 12) {

$ += floor($in / 12);

$in = $in % 12;

return new Distance($ , $in);

public func on subtract($feet2, $inches2) {

$ = $this->feet - $feet2;

$in = $this->inches - $inches2;

Dept Of Computer Science 46


GFGC Kushalnagar

if ($in < 0) {

$ --; // Reduce a foot

$in += 12; // Add 12 inches

return new Distance($ , $in);

public func on getDistance() {

return $this->feet . " feet " . $this->inches . " inches";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$feet1 = $_POST["feet1"];

$inches1 = $_POST["inches1"];

$feet2 = $_POST["feet2"];

$inches2 = $_POST["inches2"];

$distance1 = new Distance($feet1, $inches1);

$distance2 = new Distance($feet2, $inches2);

$sum = $distance1->add($feet2, $inches2);

$difference = $distance1->subtract($feet2, $inches2);

echo "<h2>Result</h2>";

echo "<p>Sum: " . $sum->getDistance() . "</p>";

echo "<p>Difference: " . $difference->getDistance() . "</p>";

?>

Dept Of Computer Science 47


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 48


GFGC Kushalnagar

INDEX.PHP
<?php

$host = "localhost";

$username = "root";

$password = "";

$database = "pb4.sql";

$conn = new mysqli($host, $username, $password, $database);

if ($conn->connect_error) {

die("Connec on failed: " . $conn->connect_error);

func on login($username, $password, $conn) {

$sql = "SELECT * FROM login WHERE uname = '$username' AND passwd =

'$password'";

$result = $conn->query($sql);

if ($result->num_rows == 1) {

echo "Login successful!";

} else {

echo "Invalid username or password";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$username = $_POST["username"];
Dept Of Computer Science 49
GFGC Kushalnagar

$password = $_POST["password"];

login($username, $password, $conn);

$conn->close();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

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

< tle>Login Form</ tle>

</head>

<body>

<h2>Login</h2>

<form method="post" ac on="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);

?>">

<label for="username">Username:</label><br>

<input type="text" id="username" name="username" required><br>

<label for="password">Password:</label><br>

<input type="password" id="password" name="password" required><br><br>

<input type="submit" value="Login">

</form>

</body>

</html>

Dept Of Computer Science 50


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 51


GFGC Kushalnagar

INDEX.PHP
<?php

// MySQL database connec on se ngs

$host = "localhost";

$username = "root";

$password = "";

$database = "feedback.sql";

// Establishing a connec on to the MySQL database

$conn = new mysqli($host, $username, $password, $database);

// Check connec on

if ($conn->connect_error) {

die("Connec on failed: " . $conn->connect_error);

// Check if the feedback form has been submi ed

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Retrieve form data

$name = $_POST["name"];

$email = $_POST["email"];

$subject = $_POST["subject"];

$message = $_POST["message"];

// SQL query to insert feedback into database

Dept Of Computer Science 52


GFGC Kushalnagar

$sql = "INSERT INTO feedback (name, email, subject, message) VALUES ('$name',

'$email', '$subject', '$message')";

if ($conn->query($sql) === TRUE) {

echo "Feedback submi ed successfully!";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

// Close the database connec on

$conn->close();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

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

< tle>Feedback Form</ tle>

</head>

<body>

<h2>Feedback Form</h2>

<form method="post" ac on="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);

?>">

<label for="name">Name:</label><br>

<input type="text" id="name" name="name" required><br><br>

Dept Of Computer Science 53


GFGC Kushalnagar

<label for="email">Email:</label><br>

<input type="email" id="email" name="email" required><br><br>

<label for="subject">Subject:</label><br>

<input type="text" id="subject" name="subject" required><br><br>

<label for="message">Message:</label><br>

<textarea id="message" name="message" rows="4" required></textarea><br><br><br>

<input type="submit" value="Submit">

</form>

</body>

</html>

FEEDBACK.SQL
CREATE TABLE feedback (

name varchar(20) ,

subject varchar(20) ,

email varchar(20) ,

message varchar(100)

);

Dept Of Computer Science 54


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 55


GFGC Kushalnagar

6) Develop a dynamic PHP applica on to efficiently manage and store customer


informa on, encompassing key fields like Customer Number, Customer Name, Item
Purchased, and Mobile Number in Database. This applica on should provide a user-
friendly interface with strategically placed bu ons to trigger specific func onali es.
These func onali es include:

1.Add Customer Informa on: Clicking this bu on should dynamically reveal a form for
entering new customer details. Include proper valida on checks for mobile numbers
(10 digits), and also for Customer id ensuring accuracy in data input.

2.Delete Customer Records: Triggering this bu on should prompt the appearance of a


form, specifically reques ng the Customer ID to iden fy and delete the corresponding
customer record. And provide appropriate messages for incorrect inputs.

3.Search for Par cular Entries: This func on should unveil a search form when
ac vated, allowing users to input Customer id to find specific customer records.

4.Sort Database Based on Customer Id: Clicking this bu on should facilitate the sor ng
of the en re database based on customer id.

5.Display Complete Set of Records: Ac va ng this func on should present a


comprehensive display of all customer records.

6.Interface Design: Ini ally, the interface should only showcase func onal bu ons.
Upon clicking a bu on, the respec ve form should dynamically appear, offering a
tailored and focused user experience.

7 .Give proper messages a er every transac on.

INDEX.PHP
<?php

// Database connec on

$servername = "localhost";

$username = "root";

$password = "";

Dept Of Computer Science 56


GFGC Kushalnagar

$database = "bca";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {

die("Connec on failed: " . $conn->connect_error);

// Func on to add customer informa on

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

$customer_id = $_POST['customer_id'];

$customer_name = $_POST['customer_name'];

$item_purchased = $_POST['item_purchased'];

$mobile_number = $_POST['mobile_number'];

if (strlen($mobile_number) != 10 || !ctype_digit($mobile_number)) {

echo "Invalid mobile number";

} else {

$sql = "INSERT INTO customer (cus d, cname, itemname, mobileno) VALUES

('$customer_id', '$customer_name', '$item_purchased', '$mobile_number')";

if ($conn->query($sql) === TRUE) {

echo "Customer added successfully"."<br>";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

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

Dept Of Computer Science 57


GFGC Kushalnagar

$customer_id = $_POST['customer_id'];

$sql = "DELETE FROM customer WHERE cus d='$customer_id'";

if ($conn->query($sql) === TRUE) {

if ($conn->affected_rows > 0) {

echo "Customer deleted successfully";

} else {

echo "Error: Customer with ID " . $customer_id . " not found";

} else {

echo "Error dele ng customer: " . $conn->error;

// Func on to search for par cular entries

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

$customer_id = $_POST['customer_id'];

$sql = "SELECT * FROM customer WHERE cus d='$customer_id'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while ($row = $result->fetch_assoc()) {

echo "Customer ID: " . $row['cus d'] . ", Name: " . $row['cname'] . ", Item Purchased:

" . $row['itemname'] . ", Mobile Number: " . $row['mobileno'] . "<br>";

} else {

echo "No results found";

Dept Of Computer Science 58


GFGC Kushalnagar

}if (isset($_POST['display_all'])) {

$sql = "SELECT * FROM customer";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while ($row = $result->fetch_assoc()) {

echo "Customer ID: " . $row['cus d'] . ", Name: " . $row['cname'] . ", Item Purchased:

" . $row['itemname'] . ", Mobile Number: " . $row['mobileno'] . "<br>";

} else {

echo "No results found";

// Func on to sort database based on customer id and display all records

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

$sql = "SELECT * FROM customer ORDER BY cname";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while ($row = $result->fetch_assoc()) {

echo "Customer ID: " . $row['cus d'] . ", Name: " . $row['cname'] . ", Item Purchased:

" . $row['itemname'] . ", Mobile Number: " . $row['mobileno'] . "<br>";

} else {

echo "No results found";

Dept Of Computer Science 59


GFGC Kushalnagar

$conn->close();

?>

<!DOCTYPE html>

<html>

<head>

< tle>Customer Management System</ tle>

</head>

<body>

<bu on onclick="document.getElementById('addForm').style.display = 'block'">Add

Customer Informa on</bu on>

<bu on onclick="document.getElementById('deleteForm').style.display = 'block'">Delete

Customer Records</bu on>

<bu on onclick="document.getElementById('searchForm').style.display = 'block'">Search

for Par cular Entries</bu on>

<form id="sortForm" method="post" style="display: inline;">

<input type="hidden" name="sort_and_display" value="true">

<input type="submit" value="Sort Database and Display All Records">

</form>

<form id="displayForm" method="post" style="display: inline;">

<input type="hidden" name="display_all" value="true">

<input type="submit" value="Display All Records">

</form>

Dept Of Computer Science 60


GFGC Kushalnagar

<div id="addForm" style="display: none;">

<form method="post">

Customer ID: <input type="text" name="customer_id" required><br>

Customer Name: <input type="text" name="customer_name" required><br>

Item Purchased: <input type="text" name="item_purchased" required><br>

Mobile Number: <input type="text" name="mobile_number" required><br>

<input type="submit" name="add_customer" value="Add Customer">

</form>

</div>

<div id="deleteForm" style="display: none;">

<form method="post">

Customer ID: <input type="text" name="customer_id" required><br>

<input type="submit" name="delete_customer" value="Delete Customer">

</form>

</div><div id="searchForm" style="display: none;">

<form method="post">

Customer ID: <input type="text" name="customer_id" required><br>

<input type="submit" name="search_customer" value="Search Customer">

</form>

</div>

</body>

</html>

Dept Of Computer Science 61


GFGC Kushalnagar

CUSTOMER.SQL
CREATE TABLE customer (

cus d INT ,

cname VARCHAR(20),

itemname VARCHAR(50),

mobileno BIGINT(20));

Dept Of Computer Science 62


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 63


GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>

<html>

<head>

< tle>Book Shopping Form</ tle>

<style>

table {

border-collapse: collapse;

width: 100%;

th, td {

border: 1px solid black;

padding: 8px;

text-align: le ;

th {

background-color: #f2f2f2;

Dept Of Computer Science 64


GFGC Kushalnagar

</style>

</head>

<body>

<h2>Book Shopping Form</h2>

<form method="post" ac on="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);

?>">

Book Number: <input type="number" name="book_number" required><br><br>

Book Title: <input type="text" name="book_ tle" required><br><br>

Price: <input type="number" name="price" step="0.01" required><br><br>

Quan ty: <input type="number" name="quan ty" required><br><br>

<input type="submit" name="submit" value="Submit">

</form>

<?php

// MySQL connec on

$servername = "localhost";

$username = "root";

$password = "";

$database = "bca";

// Create connec on

$conn = new mysqli($servername, $username, $password, $database);

// Check connec on

if ($conn->connect_error) {

die("Connec on failed: " . $conn->connect_error);

Dept Of Computer Science 65


GFGC Kushalnagar

// Func on to calculate discount amount based on discount rate

func on calDiscount($price, $discount_rate) {

return ($price * $discount_rate) / 100;

// Func on to calculate net bill amount

func on calNetBill($price, $quan ty, $discount_amount) {

return ($price * $quan ty) - $discount_amount;

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$book_number = $_POST["book_number"];

$book_ tle = $_POST["book_ tle"];

$price = $_POST["price"];

$quan ty = $_POST["quan ty"];

// Calculate discount rate based on book number

switch ($book_number) {

case 101:

$discount_rate = 15;

break;

case 102:

$discount_rate = 20;

break;

case 103:

$discount_rate = 25;

Dept Of Computer Science 66


GFGC Kushalnagar

break;

default:

$discount_rate = 5;

// Calculate discount amount

$discount_amount = calDiscount($price, $discount_rate);

// Calculate net bill amount

$net_bill_amount = calNetBill($price, $quan ty, $discount_amount);

// Insert data into MySQL table

$sql = "INSERT INTO BOOK (book_number, book_ tle, price, quan ty, discount_rate,

discount_amount, net_bill_amount)

VALUES ('$book_number', '$book_ tle', '$price', '$quan ty', '$discount_rate',

'$discount_amount', '$net_bill_amount')";

if ($conn->query($sql) === TRUE) {

echo "Bill saved successfully.";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

echo "<h2 style='text-align: center;'>Book Bill</h2>";

echo "<table>";

echo "<tr><th>Book Number</th><th>Book

Title</th><th>Price</th><th>Quan ty</th><th>Discount Rate (%)</th><th>Discount

Amount</th><th>Net Bill Amount</th></tr>";

echo "<tr>";

Dept Of Computer Science 67


GFGC Kushalnagar

echo "<td>".$book_number."</td>";

echo "<td>".$book_ tle."</td>";

echo "<td>".$price."</td>";

echo "<td>".$quan ty."</td>";

echo "<td>".$discount_rate."</td>";

echo "<td>".$discount_amount."</td>";

echo "<td>".$net_bill_amount."</td>";

echo "</tr>";

echo "</table>";

$conn->close();

?>

</body>

</html>

BOOK.SQL
CREATE TABLE book (

book_number int ,

book_ tle varchar(255) ,

price decimal(10,2) ,

quan ty int ,

discount_rate decimal(5,2) ,

discount_amount decimal(10,2),

net_bill_amount decimal(10,2)

Dept Of Computer Science 68


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 69


GFGC Kushalnagar

INDEX.PHP
<!DOCTYPE html>

<html>

<head>

< tle>Hotel Reserva on System</ tle>

</head>

<body>

<h2>Hotel Reserva on System</h2>

<!-- Form for inser ng records -->

<h3>Insert Records</h3>

<form method="post" ac on="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);

?>">

<table>

Dept Of Computer Science 70


GFGC Kushalnagar

<tr>

<td>Room Number:</td>

<td><input type="number" name="room_number" required></td>

</tr>

<tr>

<td>Room Type:</td>

<td><input type="text" name="room_type" required></td>

</tr>

<tr>

<td>Capacity:</td>

<td><input type="number" name="capacity" required></td>

</tr>

<tr>

<td>Status:</td>

<td><input type="text" name="status" required></td>

</tr>

</table>

<br>

<input type="submit" name="insert" value="Insert Record">

</form>

<?php

$servername = "localhost";

$username = "root";

$password = "";

Dept Of Computer Science 71


GFGC Kushalnagar

$database = "bca";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {

die("Connec on failed: " . $conn->connect_error);

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

$room_number = $_POST["room_number"];

$room_type = $_POST["room_type"];

$capacity = $_POST["capacity"];

$status = $_POST["status"];

$sql_insert = "INSERT INTO rooms (room_number, room_type, capacity, status)

VALUES ('$room_number', '$room_type', '$capacity', '$status')";

if ($conn->query($sql_insert) === TRUE) {

echo "Record inserted successfully.";

} else {

echo "Error: " . $sql_insert . "<br>" . $conn->error;

?>

<!-- Displaying available and booked rooms in tabular form -->

<h3>Available Rooms</h3>

<?php

$sql_available = "SELECT * FROM rooms WHERE status = 'available'";

$result_available = $conn->query($sql_available);

Dept Of Computer Science 72


GFGC Kushalnagar

if ($result_available->num_rows > 0) {

echo "<table border='1'>

<tr>

<th>Room Number</th>

<th>Room Type</th>

<th>Capacity</th>

</tr>";

while($row = $result_available->fetch_assoc()) {

echo "<tr>

<td>" . $row["room_number"] . "</td>

<td>" . $row["room_type"] . "</td>

<td>" . $row["capacity"] . "</td>

</tr>";

echo "</table>";

} else {

echo "No available rooms.";

?>

<h3>Booked Rooms</h3>

<?php

$sql_booked = "SELECT * FROM rooms WHERE status = 'booked'";

$result_booked = $conn->query($sql_booked);

if ($result_booked->num_rows > 0) {

Dept Of Computer Science 73


GFGC Kushalnagar

echo "<table border='1'>

<tr>

<th>Room Number</th>

<th>Room Type</th>

<th>Capacity</th>

</tr>";

while($row = $result_booked->fetch_assoc()) {

echo "<tr>

<td>" . $row["room_number"] . "</td>

<td>" . $row["room_type"] . "</td>

<td>" . $row["capacity"] . "</td>

</tr>";

echo "</table>";

} else {

echo "No booked rooms.";

$conn->close();

?>

<!-- Form for checking in -->

<h3>Check-in</h3>

<form method="post" >

Room Number: <input type="number" name="room_number" required><br><br>

<input type="submit" name="check_in" value="Check-in">

Dept Of Computer Science 74


GFGC Kushalnagar

</form>

<!-- Form for checking out -->

<h3>Check-out</h3>

<form method="post" >

Room Number: <input type="number" name="room_number" required><br><br>

<input type="submit" name="check_out" value="Check-out">

</form>

<?php

$conn = new mysqli($servername, $username, $password, $database);

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

$room_number = $_POST["room_number"];

$sql_check_room = "SELECT * FROM rooms WHERE room_number =

'$room_number' AND status = 'available'";

$result_check_room = $conn->query($sql_check_room);

if ($result_check_room->num_rows == 1) {

$sql_check_in = "UPDATE rooms SET status = 'booked' WHERE room_number =

'$room_number'";

if ($conn->query($sql_check_in) === TRUE) {

echo "Room checked in successfully.";

} else {

echo "Error: " . $sql_check_in . "<br>" . $conn->error;

} else {

echo "Room is not available for check-in.";

Dept Of Computer Science 75


GFGC Kushalnagar

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

$room_number = $_POST["room_number"];

$sql_check_room = "SELECT * FROM rooms WHERE room_number =

'$room_number' AND status = 'booked'";

$result_check_room = $conn->query($sql_check_room);

if ($result_check_room->num_rows == 1) {

$sql_check_out = "UPDATE rooms SET status = 'available' WHERE room_number =

'$room_number'";

if ($conn->query($sql_check_out) === TRUE) {

echo "Room checked out successfully.";

} else {

echo "Error: " . $sql_check_out . "<br>" . $conn->error;

} else {

echo "Room is either not booked or does not exist.";

$conn->close();

?>

</body>

</html>

Dept Of Computer Science 76


GFGC Kushalnagar

ROOMS.SQL
CREATE TABLE rooms (

room_number INT NOT NULL PRIMARY KEY,

room_type VARCHAR(50),

capacity INT,

status ENUM('available', 'booked')

);

Dept Of Computer Science 77


GFGC Kushalnagar

OUTPUT:

Dept Of Computer Science 78


GFGC Kushalnagar

Dept Of Computer Science 79

You might also like