11.
Write a PHP program to create and manage a database using SQL
commands.
12. Write a PHP program to create and validate a email id.
13. Using PHP and SQL, create and validate a sample login form.
14. Using PHP and SQL, develop a program to accept student information viz.
Accession
name, regno, course, sem, marks in 3 subjects from a web page and store
the information
in a database and to calculate total and average and to display the results
with proper
headings.
<?php(11)
// Database credentials
$servername = "localhost";
$username = "your_username";
$password = "your_password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Connected successfully <br>";
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . $conn->error;
$conn->close();
?>
<?php(12)
$email = [email protected];
If (filter_var($email, FILTER_VALIDATE_EMAIL)) {
Echo “$email is a valid email address”;
} else {
Echo “$email is not a valid email address”;
?>
<?php(13)
// Database credentials
$servername = “localhost”;
$username = “your_username”;
$password = “your_password”;
$dbname = “myDB”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
If ($conn->connect_error) {
Die(“Connection failed: “ . $conn->connect_error);
If ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$username = $_POST[“username”];
$password = $_POST[“password”];
// SQL query to select user
$sql = “SELECT * FROM users WHERE username = ‘$username’”;
$result = $conn->query($sql);
If ($result->num_rows > 0) {
$row = $result->fetch_assoc();
// Basic password comparison (for demonstration only)
If ($password == $row[“password”]) {
Echo “Login successful!”;
} else {
Echo “Incorrect password.”;
}
} else {
Echo “User not found.”;
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action=”<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]); ?>”
method=”post”>
<label for=”username”>Username:</label>
<input type=”text” id=”username” name=”username”
required><br><br>
<label for=”password”>Password:</label>
<input type=”password” id=”password” name=”password”
required><br><br>
<button type=”submit”>Login</button>
</form>
</body>
</html>
-----_
_______
<?php(14)
// Database connection details
$servername = “localhost”;
$username = “your_username”;
$password = “your_password”;
$dbname = “your_database_name”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
If ($conn->connect_error) {
Die(“Connection failed: “ . $conn->connect_error);
// Handle form submission
If ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$accession_name = $_POST[“accession_name”];
$regno = $_POST[“regno”];
$course = $_POST[“course”];
$sem = $_POST[“sem”];
$sub1 = $_POST[“sub1”];
$sub2 = $_POST[“sub2”];
$sub3 = $_POST[“sub3”];
// Insert data into database
$sql = “INSERT INTO students (accession_name, regno, course, sem, sub1,
sub2, sub3)
VALUES (‘$accession_name’, ‘$regno’, ‘$course’, ‘$sem’, ‘$sub1’,
‘$sub2’, ‘$sub3’)”;
If ($conn->query($sql) === TRUE) {
Echo “New record created successfully”;
} else {
Echo “Error: “ . $sql . “<br>” . $conn->error;
// Fetch and display student data
$sql = “SELECT accession_name, regno, course, sem, sub1, sub2, sub3,
(sub1 + sub2 + sub3) AS total,
(sub1 + sub2 + sub3) / 3 AS average
FROM students”;
$result = $conn->query($sql);
If ($result->num_rows > 0) {
Echo “<table>”;
Echo “<tr><th>Accession
Name</th><th>Regno</th><th>Course</th><th>Sem</th><th>Sub1</t
h><th>Sub2</th><th>Sub3</th><th>Total</th><th>Average</th></
tr>”;
While($row = $result->fetch_assoc()) {
Echo “<tr>”;
Echo “<td>” . $row[“accession_name”] . “</td>”;
Echo “<td>” . $row[“regno”] . “</td>”;
Echo “<td>” . $row[“course”] . “</td>”;
Echo “<td>” . $row[“sem”] . “</td>”;
Echo “<td>” . $row[“sub1”] . “</td>”;
Echo “<td>” . $row[“sub2”] . “</td>”;
Echo “<td>” . $row[“sub3”] . “</td>”;
Echo “<td>” . $row[“total”] . “</td>”;
Echo “<td>” . $row[“average”] . “</td>”;
Echo “</tr>”;
Echo “</table>”;
} else {
Echo “0 results”;
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Student Information</title>
</head>
<body>
<h2>Enter Student Information</h2>
<form method=”post” action=”<?php echo
htmlspecialchars($_SERVER[“PHP_SELF”]);?>”>
Accession Name: <input type=”text”
name=”accession_name”><br><br>
Regno: <input type=”text” name=”regno”><br><br>
Course: <input type=”text” name=”course”><br><br>
Sem: <input type=”text” name=”sem”><br><br>
Subject 1: <input type=”text” name=”sub1”><br><br>
Subject 2: <input type=”text” name=”sub2”><br><br>
Subject 3: <input type=”text” name=”sub3”><br><br>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>
<?php(15)
// Database connection details
$servername = “localhost”;
$username = “your_username”;
$password = “your_password”;
$dbname = “your_database_name”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
If ($conn->connect_error) {
Die(“Connection failed: “ . $conn->connect_error);
// Handle form submission
If ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$accession_no = $_POST[“accession_no”];
$name = $_POST[“name”];
$basic = $_POST[“basic”];
// Calculate allowances and deductions
$da = 0.4 * $basic; // DA = 40% of basic
$hra = 0.2 * $basic; // HRA = 20% of basic
$pf = 0.12 * $basic; // PF = 12% of basic
$tax = 0.1 * ($basic + $da + $hra); // Tax = 10% of gross salary
// Calculate gross and net pay
$gross = $basic + $da + $hra;
$net_pay = $gross - $pf - $tax;
// Insert data into database
$sql = “INSERT INTO employees (accession_no, name, basic, da, hra, pf,
tax, gross, net_pay)
VALUES (‘$accession_no’, ‘$name’, ‘$basic’, ‘$da’, ‘$hra’, ‘$pf’, ‘$tax’,
‘$gross’, ‘$net_pay’)”;
If ($conn->query($sql) === TRUE) {
Echo “New record created successfully”;
} else {
Echo “Error: “ . $sql . “<br>” . $conn->error;
// Fetch and display employee data
$sql = “SELECT accession_no, name, basic, da, hra, pf, tax, gross, net_pay
FROM employees”;
$result = $conn->query($sql);
If ($result->num_rows > 0) {
Echo “<table>”;
Echo “<tr><th>Accession
No</th><th>Name</th><th>Basic</th><th>DA</th><th>HRA</th><th
>PF</th><th>Tax</th><th>Gross</th><th>Net Pay</th></tr>”;
While($row = $result->fetch_assoc()) {
Echo “<tr>”;
Echo “<td>” . $row[“accession_no”] . “</td>”;
Echo “<td>” . $row[“name”] . “</td>”;
Echo “<td>” . $row[“basic”] . “</td>”;
Echo “<td>” . $row[“da”] . “</td>”;
Echo “<td>” . $row[“hra”] . “</td>”;
Echo “<td>” . $row[“pf”] . “</td>”;
Echo “<td>” . $row[“tax”] . “</td>”;
Echo “<td>” . $row[“gross”] . “</td>”;
Echo “<td>” . $row[“net_pay”] . “</td>”;
Echo “</tr>”;
Echo “</table>”;
} else {
Echo “0 results”;
$conn->close();
?>
<!DOCTYPE html>
<html>
<head>
<title>Employee Information</title>
</head>
<body>
<h2>Enter Employee Information</h2>
<form method=”post” action=”<?php echo
htmlspecialchars($_SERVER[“PHP_SELF”]);?>”>
Accession No: <input type=”text” name=”accession_no”><br><br>
Name: <input type=”text” name=”name”><br><br>
Basic Salary: <input type=”text” name=”basic”><br><br>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>