0% found this document useful (0 votes)
10 views

php lab part b manual

The document outlines a series of PHP and MySQL lab exercises for students at St. Joseph's Degree College, Hunsur. It includes tasks such as form handling using GET and POST methods, checking for palindromes, matrix addition, file uploads, and database creation. Each task is accompanied by example PHP scripts to guide students in their implementation.

Uploaded by

Ajay Kumar R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

php lab part b manual

The document outlines a series of PHP and MySQL lab exercises for students at St. Joseph's Degree College, Hunsur. It includes tasks such as form handling using GET and POST methods, checking for palindromes, matrix addition, file uploads, and database creation. Each task is accompanied by example PHP scripts to guide students in their implementation.

Uploaded by

Ajay Kumar R
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ST.

Joseph’s Degree college, Hunsur


Department of Computer Application
PHP and MYSQL LAB

Sl Part B
No
01 Write a PHP script to implement form handling using get method.

02 Write a PHP script to implement form handling using post method.

03 Write a PHP script that receive form input by the method post to
check the number is palindrome or not.
04 Write a PHP script that receive string as a form input.

05 Write a PHP script to Compute addition of two matrices as a form


input.
Write a PHP script to Show the functionality of date and time
06 function.

07 Write a PHP Program to upload a file

08 Write a PHP script to implement database creation

09 Write a PHP script to create table

Write a PHP program to design a college admission form using


10 MYSQL database.

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


01.Write a PHP script to implement form handling using get method.
<?php
// Handle form submission if data is received via GET method
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['name']) &&
isset($_GET['email'])) {
// Retrieve and sanitize form data
$name = htmlspecialchars($_GET['name']);
$email = htmlspecialchars($_GET['email']);

// Display the submitted form data


echo "<h2>Form Submission Successful!</h2>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
} else {
// Display the form
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Handling with GET Method</title>
</head>
<body>
<h2>Form Handling with GET Method</h2>
<form action="form_handler.php" method="get">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="email">Email:</label>
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
<input type="email" id="email" name="email" required>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
}
?>
02.Write a PHP script to implement form handling using post method.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['name']) &&
isset($_POST['email'])) {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
echo "<h2>Form Submission Successful!</h2>";
echo "<p><strong>Name:</strong> $name</p>";
echo "<p><strong>Email:</strong> $email</p>";
} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Handling with POST Method</title>
</head>
<body>
<h2>Form Handling with POST Method</h2>
<form action="form_handler1.php" method="post">
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
}
?>
03.Write a PHP script that receive form input by the method post to check
the number is palindrome or not.
<?php
function isPrime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false;
}
}
return true;
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['number'])) {
$number = (int)$_POST['number'];
$is_prime = isPrime($number);
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Number Checker</title>
</head>
<body>
<h1>Prime Number Checker</h1>
<form action="prime_checker.php" method="POST">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<input type="submit" value="Check">
</form>
<?php
if (isset($is_prime)) {
if ($is_prime) {
echo "<h2>$number is a Prime Number</h2>";
} else {
echo "<h2>$number is Not a Prime Number</h2>";
}
}
?>
</body>
</html>
04.Write a PHP script that receive string as a form input.
<?php
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['input_string'])) {
$input_string = htmlspecialchars($_POST['input_string']); // Sanitize the input to prevent
XSS attacks
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Input Form</title>
</head>
<body>
<h1>Submit a String</h1>
<!-- Form to receive string input -->
<form action="receive_string.php" method="POST">
<label for="input_string">Enter a string:</label>
<input type="text" id="input_string" name="input_string" required>
<input type="submit" value="Submit">
</form>
<?php
if (isset($input_string)) {
echo "<h2>You entered: $input_string</h2>";
}
?>
</body>
</html>
05.Write a PHP script to Compute addition of two matrices as a form input.
<?php
$matrix1 = $matrix2 = $result = [];
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$matrix1 = [
[$_POST['m11'], $_POST['m12']],
[$_POST['m21'], $_POST['m22']]
];
$matrix2 = [
[$_POST['m31'], $_POST['m32']],
[$_POST['m41'], $_POST['m42']]
];
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
$result[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Matrix Addition</title>
</head>
<body>
<h1>Matrix Addition</h1>
<!-- Form to receive matrix input -->
<form action="matrix_addition.php" method="POST">
<h2>Enter Matrix 1</h2>
<input type="number" name="m11" required> <input type="number" name="m12"
required><br>
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
<input type="number" name="m21" required> <input type="number" name="m22"
required><br>
<h2>Enter Matrix 2</h2>
<input type="number" name="m31" required> <input type="number" name="m32"
required><br>
<input type="number" name="m41" required> <input type="number" name="m42"
required><br>
<input type="submit" value="Add Matrices">
</form>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST"): ?>
<h2>Result of Matrix Addition:</h2>
<table border="1">
<?php for ($i = 0; $i < 2; $i++): ?>
<tr>
<?php for ($j = 0; $j < 2; $j++): ?>
<td><?= $result[$i][$j] ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
<?php endif; ?>
</body>
</html>
06.Write a PHP script to Show the functionality of date and time function.
<?php
date_default_timezone_set('Asia/Kolkata');
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentTimestamp = time();
$nextDay = date("Y-m-d", strtotime("+1 day")); // Add 1 day
$dayOfWeek = date("l");
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
$monthName = date("F");
$currentYear = date("Y");
$nextWeek = date("Y-m-d", strtotime("+1 week"));
$nextMonth = date("Y-m-d", strtotime("+1 month"));
$date1 = "2025-01-11";
$date2 = "2025-02-11";
$dateDiff = date_diff(date_create($date1), date_create($date2));
$timezone = date_default_timezone_get(); // Current timezone
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>India Time Zone (IST)</title>
</head>
<body>

<h1>Current Date and Time in India (IST)</h1>

<!-- Display current date and time in IST -->


<p><strong>Current Date (IST):</strong> <?= $currentDate ?></p>
<p><strong>Current Time (IST):</strong> <?= $currentTime ?></p>

<!-- Display the current Unix timestamp -->


<p><strong>Current Unix Timestamp (IST):</strong> <?= $currentTimestamp ?></p>

<!-- Display the next day -->


<p><strong>Next Day (IST):</strong> <?= $nextDay ?></p>

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


<!-- Display day, month, and year info -->
<p><strong>Day of the Week (IST):</strong> <?= $dayOfWeek ?></p>
<p><strong>Month Name (IST):</strong> <?= $monthName ?></p>
<p><strong>Current Year (IST):</strong> <?= $currentYear ?></p>

<!-- Display time operations -->


<p><strong>Next Week (IST):</strong> <?= $nextWeek ?></p>
<p><strong>Next Month (IST):</strong> <?= $nextMonth ?></p>
<p><strong>Date Difference:</strong> <?= $dateDiff->days ?> days</p>
<p><strong>Current Timezone:</strong> <?= $timezone ?></p>
</body>
</html>
07.Write a PHP Program to upload a file
<?php
// Define the directory where the file will be uploaded
$target_dir = "uploads/";

// Flag to check if the upload is successful


$uploadOk = 1;

// Check if the form is submitted


if (isset($_POST["submit"])) {
// Get the name of the uploaded file
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

// Get the file extension of the uploaded file


$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

// Check if the file is too large


if ($_FILES["fileToUpload"]["size"] > 500000) {
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
echo "Sorry, your file is too large. Maximum allowed size is 500KB.";
$uploadOk = 0;
}

// Allow only specific file formats (JPG, JPEG, PNG, GIF)


if($fileType != "jpg" && $fileType != "jpeg" && $fileType != "png" && $fileType !=
"gif") {
echo "Sorry, only JPG, JPEG, PNG, and GIF files are allowed.";
$uploadOk = 0;
}

// Check if everything is okay to upload


if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
// Create the upload directory if it doesn't exist
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true);
}

// If everything is okay, move the uploaded file to the target directory


if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . "
has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>

Aishwarya, Assistant professor, St. Joseph’s college, hunsur


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>

<h1>Upload a File</h1>

<!-- Form for uploading a file -->


<form action="" method="POST" enctype="multipart/form-data">
<label for="fileToUpload">Choose a file to upload:</label>
<input type="file" name="fileToUpload" id="fileToUpload" required>
<br><br>
<input type="submit" value="Upload File" name="submit">
</form>
</body>
</html>
08.Write a PHP script to implement database creation
<?php
$servername = "localhost:3307"; // MySQL server address with port
$username = "root"; // MySQL username
$password = ""; // MySQL password (empty if no password is set)
$dbname = "your_database_name"; // The name of the database you want to create

// Create connection
$conn = new mysqli($servername, $username, $password);
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Check if the database already exists


$db_check = "SHOW DATABASES LIKE '$dbname'";
$result = $conn->query($db_check);

if ($result && $result->num_rows > 0) {


echo "Database '$dbname' already exists.";
} else {
// SQL query to create a new database
$sql = "CREATE DATABASE $dbname";

// Execute the query


if ($conn->query($sql) === TRUE) {
echo "Database '$dbname' created successfully!";
} else {
echo "Error creating database: " . $conn->error;
}
}
// Close the connection
$conn->close();
?>
09.Write a PHP script to create table
<?php
$servername = "localhost:3307"; // MySQL server address with port (adjust as needed)
$username = "root"; // MySQL username
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
$password = ""; // MySQL password (empty if no password is set)
$dbname = "your_database_name"; // The database in which the table will be created

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// SQL query to create a new table


$sql = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

// Execute the query


if ($conn->query($sql) === TRUE) {
echo "Table 'users' created successfully!";
} else {
echo "Error creating table: " . $conn->error;
}

// Close the connection


$conn->close();
?>
Aishwarya, Assistant professor, St. Joseph’s college, hunsur
Aishwarya, Assistant professor, St. Joseph’s college, hunsur

You might also like