1..
Write a PHP program to display Fibonacci series
<?php
// Define the number of terms to display
$n = 10;
// Initialize the first two terms
$first = 0;
$second = 1;
echo "Fibonacci Series: ";
for ($i = 0; $i < $n; $i++) {
echo $first . " ";
// Calculate the next term
$next = $first + $second;
// Update the terms
$first = $second;
$second = $next;
?>
2..Write a PHP Program to read the employee details.
<!DOCTYPE html>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
<h2>Enter Employee Details</h2>
<form method="post" action="">
Name: <input type="text" name="name" required><br><br>
Age: <input type="number" name="age" required><br><br>
Position: <input type="text" name="position" required><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = htmlspecialchars($_POST['name']);
$age = htmlspecialchars($_POST['age']);
$position = htmlspecialchars($_POST['position']);
// Display employee details
echo "<h3>Employee Details:</h3>";
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Position: " . $position . "<br>";
?>
</body>
</html>
3..Write a PHP program to prepare the student marks list
<!DOCTYPE html>
<html>
<head>
<title>Student Marks List</title>
</head>
<body>
<h2>Enter Student Marks</h2>
<form method="post" action="">
Student Name: <input type="text" name="name" required><br><br>
Subject 1 Marks: <input type="number" name="subject1" required><br><br>
Subject 2 Marks: <input type="number" name="subject2" required><br><br>
Subject 3 Marks: <input type="number" name="subject3" required><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = htmlspecialchars($_POST['name']);
$subject1 = (int)$_POST['subject1'];
$subject2 = (int)$_POST['subject2'];
$subject3 = (int)$_POST['subject3'];
// Calculate total and average
$total = $subject1 + $subject2 + $subject3;
$average = $total / 3;
// Display the marks list
echo "<h3>Student Marks List:</h3>";
echo "Student Name: " . $name . "<br>";
echo "Subject 1 Marks: " . $subject1 . "<br>";
echo "Subject 2 Marks: " . $subject2 . "<br>";
echo "Subject 3 Marks: " . $subject3 . "<br>";
echo "Total Marks: " . $total . "<br>";
echo "Average Marks: " . number_format($average, 2) . "<br>";
?>
</body>
</html>
4.Write a PHP program to generate the multiplication of two matrices
<?php
// Define matrices
$matrixA = [
[1, 2, 3],
[4, 5, 6]
];
$matrixB = [
[7, 8],
[9, 10],
[11, 12]
];
// Get matrix dimensions
$rowsA = count($matrixA);
$colsA = count($matrixA[0]);
$rowsB = count($matrixB);
$colsB = count($matrixB[0]);
// Check if multiplication is possible
if ($colsA != $rowsB) {
echo "Matrix multiplication not possible. Number of columns in Matrix A must equal number of rows
in Matrix B.";
exit;
// Initialize result matrix
$result = [];
for ($i = 0; $i < $rowsA; $i++) {
for ($j = 0; $j < $colsB; $j++) {
$result[$i][$j] = 0;
for ($k = 0; $k < $colsA; $k++) {
$result[$i][$j] += $matrixA[$i][$k] * $matrixB[$k][$j];
// Display result matrix
echo "Resultant Matrix (A x B):<br>";
for ($i = 0; $i < $rowsA; $i++) {
for ($j = 0; $j < $colsB; $j++) {
echo $result[$i][$j] . " ";
echo "<br>";
?>
5.Create student registration form using text box, check box, radio button, select, submit button. And
display user inserted value in new PHP page.
Create the Registration Form (e.g., registration_form.php):
<!DOCTYPE html>
<html>
<head>
<title>Student Registration Form</title>
</head>
<body>
<h2>Student Registration Form</h2>
<form action="display_registration.php" method="post">
<label for="name">Name:</label>
<input type="text" name="name" required><br><br>
<label for="age">Age:</label>
<input type="text" name="age" required><br><br>
<label for="gender">Gender:</label><br>
<input type="radio" name="gender" value="Male" required> Male<br>
<input type="radio" name="gender" value="Female" required> Female<br><br>
<label for="course">Select Course:</label>
<select name="course" required>
<option value="Computer Science">Computer Science</option>
<option value="Mathematics">Mathematics</option>
<option value="Physics">Physics</option>
<option value="Chemistry">Chemistry</option>
</select><br><br>
<label>Hobbies:</label><br>
<input type="checkbox" name="hobbies[]" value="Reading"> Reading<br>
<input type="checkbox" name="hobbies[]" value="Sports"> Sports<br>
<input type="checkbox" name="hobbies[]" value="Music"> Music<br>
<input type="checkbox" name="hobbies[]" value="Traveling"> Traveling<br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
2. Create the Display Page (e.g., display_registration.php):
<!DOCTYPE html>
<html>
<head>
<title>Student Registration Details</title>
</head>
<body>
<h2>Student Registration Details</h2>
<?php
// Retrieve form data using POST method
$name = htmlspecialchars($_POST['name']);
$age = htmlspecialchars($_POST['age']);
$gender = htmlspecialchars($_POST['gender']);
$course = htmlspecialchars($_POST['course']);
$hobbies = isset($_POST['hobbies']) ? $_POST['hobbies'] : [];
// Display the data
echo "<strong>Name:</strong> " . $name . "<br>";
echo "<strong>Age:</strong> " . $age . "<br>";
echo "<strong>Gender:</strong> " . $gender . "<br>";
echo "<strong>Course:</strong> " . $course . "<br>";
echo "<strong>Hobbies:</strong> ";
if (!empty($hobbies)) {
echo implode(", ", array_map('htmlspecialchars', $hobbies));
} else {
echo "None";
?>
</body>
</html>
6.Create Website Registration Form using text box, check box, radio button, select, submit button. And
display user inserted value in new PHP page.
1. Registration Form (e.g., registration_form.php):
<!DOCTYPE html>
<html>
<head>
<title>Website Registration Form</title>
</head>
<body>
<h2>Website Registration Form</h2>
<form action="display_registration.php" method="post">
<label for="username">Username:</label>
<input type="text" name="username" required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" required><br><br>
<label for="gender">Gender:</label><br>
<input type="radio" name="gender" value="Male" required> Male<br>
<input type="radio" name="gender" value="Female" required> Female<br>
<input type="radio" name="gender" value="Other" required> Other<br><br>
<label for="country">Country:</label>
<select name="country" required>
<option value="United States">United States</option>
<option value="Canada">Canada</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Australia">Australia</option>
</select><br><br>
<label>Interests:</label><br>
<input type="checkbox" name="interests[]" value="Technology">
Technology<br>
<input type="checkbox" name="interests[]" value="Sports"> Sports<br>
<input type="checkbox" name="interests[]" value="Music"> Music<br>
<input type="checkbox" name="interests[]" value="Travel"> Travel<br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
2. Display Page (e.g., display_registration.php):
<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
</head>
<body>
<h2>Registration Details</h2>
<?php
// Retrieve and sanitize form data
$username = htmlspecialchars($_POST['username']);
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
$gender = htmlspecialchars($_POST['gender']);
$country = htmlspecialchars($_POST['country']);
$interests = isset($_POST['interests']) ? $_POST['interests'] : [];
// Display the user-entered data
echo "<strong>Username:</strong> " . $username . "<br>";
echo "<strong>Email:</strong> " . $email . "<br>";
echo "<strong>Password:</strong> " . str_repeat("*", strlen($password)) . "
(Hidden for security)<br>";
echo "<strong>Gender:</strong> " . $gender . "<br>";
echo "<strong>Country:</strong> " . $country . "<br>";
echo "<strong>Interests:</strong> ";
if (!empty($interests)) {
echo implode(", ", array_map('htmlspecialchars', $interests));
} else {
echo "None";
}
?>
</body>
</html>
7.Write PHP script to demonstrate passing variables with cookies.
1. Set the Cookie (e.g., set_cookie.php):
<?php
// Set a cookie named "user" with the value "John Doe" and an expiration time
of 1 hour
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + 3600, "/"); // 3600 seconds =
1 hour
echo "Cookie 'user' has been set.<br>";
echo "<a href='read_cookie.php'>Go to the next page to read the cookie</a>";
?>
2. Read the Cookie (e.g., read_cookie.php):
<?php
// Check if the cookie is set
if (isset($_COOKIE["user"])) {
echo "Welcome, " . htmlspecialchars($_COOKIE["user"]) . "!<br>";
} else {
echo "User cookie is not set.<br>";
}
// Link to reset the cookie
echo "<a href='set_cookie.php'>Go back to set the cookie again</a>";
?>
10.Write a PHP application to add new Rows in a Table.
1. Database Setup
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
email VARCHAR(100) NOT NULL
);
2. Create a Form to Add New Rows (e.g., add_student.php):
<!DOCTYPE html>
<html>
<head>
<title>Add New Student</title>
</head>
<body>
<h2>Add New Student</h2>
<form action="insert_student.php" method="post">
<label for="name">Name:</label>
<input type="text" name="name" required><br><br>
<label for="age">Age:</label>
<input type="number" name="age" required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br><br>
<input type="submit" value="Add Student">
</form>
</body>
</html>
3. Insert Data into the Table (e.g., insert_student.php):
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get form data
$name = $conn->real_escape_string($_POST['name']);
$age = (int)$_POST['age'];
$email = $conn->real_escape_string($_POST['email']);
// SQL to insert data
$sql = "INSERT INTO students (name, age, email) VALUES ('$name', $age,
'$email')";
if ($conn->query($sql) === TRUE) {
echo "New student record added successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close the connection
$conn->close();
?>
8.Write a PHP application to modify the Rows in a Table.
1. Database Setup
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
email VARCHAR(100) NOT NULL
);
2. Create a Form to Edit a Student Record (e.g., edit_student.php):
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the student ID from the URL
if (isset($_GET['id'])) {
$id = (int)$_GET['id'];
// Fetch the current details of the student
$sql = "SELECT * FROM students WHERE id = $id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$student = $result->fetch_assoc();
} else {
echo "No record found.";
exit;
}
} else {
echo "No ID specified.";
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Student</title>
</head>
<body>
<h2>Edit Student</h2>
<form action="update_student.php" method="post">
<input type="hidden" name="id" value="<?php echo $student['id']; ?>">
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo
htmlspecialchars($student['name']); ?>" required><br><br>
<label for="age">Age:</label>
<input type="number" name="age" value="<?php echo $student['age']; ?>"
required><br><br>
<label for="email">Email:</label>
<input type="email" name="email" value="<?php echo
htmlspecialchars($student['email']); ?>" required><br><br>
<input type="submit" value="Update Student">
</form>
</body>
</html>
<?php
// Close the connection
$conn->close();
?>
3. Update the Record in the Database (e.g., update_student.php):
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the form data
$id = (int)$_POST['id'];
$name = $conn->real_escape_string($_POST['name']);
$age = (int)$_POST['age'];
$email = $conn->real_escape_string($_POST['email']);
// SQL to update the student record
$sql = "UPDATE students SET name='$name', age=$age, email='$email' WHERE
id=$id";
if ($conn->query($sql) === TRUE) {
echo "Student record updated successfully!";
} else {
echo "Error updating record: " . $conn->error;
}
// Close the connection
$conn->close();
?>
9.Write a program to read customer information like cust-no, cust-name, item- purchased, and mob-no,
from customer table and display all these information in table format on output screen.
1. Database Setup
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE customers (
cust_no INT AUTO_INCREMENT PRIMARY KEY,
cust_name VARCHAR(50) NOT NULL,
item_purchased VARCHAR(100) NOT NULL,
mob_no VARCHAR(15) NOT NULL
);
-- Insert sample data
INSERT INTO customers (cust_name, item_purchased, mob_no) VALUES
('Alice Smith', 'Laptop', '1234567890'),
('Bob Johnson', 'Smartphone', '2345678901'),
('Charlie Brown', 'Tablet', '3456789012');
2.PHP Program to Read and Display Customer Information (e.g., display_customers.php):
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// SQL query to fetch customer data
$sql = "SELECT cust_no, cust_name, item_purchased, mob_no FROM customers";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Start the HTML table
echo "<h2>Customer Information</h2>";
echo "<table border='1'>
<tr>
<th>Customer No</th>
<th>Customer Name</th>
<th>Item Purchased</th>
<th>Mobile No</th>
</tr>";
// Output data for each row
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . htmlspecialchars($row['cust_no']) . "</td>
<td>" . htmlspecialchars($row['cust_name']) . "</td>
<td>" . htmlspecialchars($row['item_purchased']) . "</td>
<td>" . htmlspecialchars($row['mob_no']) . "</td>
</tr>";
// End the HTML table
echo "</table>";
} else {
echo "No customer records found.";
// Close the connection
$conn->close();
?>
10.Write a program to read employee information like emp-no, emp-name, designation and salary from
EMP table and display all this information using table format in your website.
1. Database Setup
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE EMP (
emp_no INT AUTO_INCREMENT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
designation VARCHAR(50) NOT NULL,
salary DECIMAL(10, 2) NOT NULL
);
-- Insert sample data
INSERT INTO EMP (emp_name, designation, salary) VALUES
('John Doe', 'Software Engineer', 60000.00),
('Jane Smith', 'Project Manager', 75000.00),
('Mike Johnson', 'UX Designer', 55000.00);
2.PHP Program to Read and Display Employee Information (e.g., display_employees.php):
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// SQL query to fetch employee data
$sql = "SELECT emp_no, emp_name, designation, salary FROM EMP";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Start the HTML table
echo "<h2>Employee Information</h2>";
echo "<table border='1'>
<tr>
<th>Employee No</th>
<th>Employee Name</th>
<th>Designation</th>
<th>Salary</th>
</tr>";
// Output data for each row
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . htmlspecialchars($row['emp_no']) . "</td>
<td>" . htmlspecialchars($row['emp_name']) . "</td>
<td>" . htmlspecialchars($row['designation']) . "</td>
<td>" . htmlspecialchars($row['salary']) . "</td>
</tr>";
// End the HTML table
echo "</table>";
} else {
echo "No employee records found.";
// Close the connection
$conn->close();
?>