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

PHP Assignment (My) (1) 3

Uploaded by

solankikusump
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)
31 views

PHP Assignment (My) (1) 3

Uploaded by

solankikusump
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/ 47

1.

Create Php Web Pages for Employees Registration form with following
fields: Employee Name, Employee Number, Designation, Mobile no,
Salary, Gender, email id, address, hoto, username, password, confirm
password, Resume . Details should be edit, Delete and View with
appropriate validations. Also apply proper design accordingly and display
all the details employee number wise.

Db1.php
<?php
// Create connection
$conn = mysqli_connect("localhost", "root", "root", "mystore");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>

Table1.php
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "root", "root");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database if it doesn't exist


$sql = "CREATE DATABASE IF NOT EXISTS php";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}

// Select the database


mysqli_select_db($conn, "php");

// SQL query to create the table


$sql = "CREATE TABLE IF NOT EXISTS employees (
emp_id INT AUTO_INCREMENT PRIMARY KEY,
emp_name VARCHAR(255) NOT NULL,
emp_number INT UNIQUE NOT NULL,
designation VARCHAR(255) NOT NULL,
mobile VARCHAR(15) NOT NULL,
salary DECIMAL(10,2) NOT NULL,
gender ENUM('Male', 'Female', 'Other') NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
address TEXT NOT NULL,
photo VARCHAR(255),
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
resume VARCHAR(255) NOT NULL
)";

// Execute the query


if (mysqli_query($conn, $sql)) {
echo "Table 'php' created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn);
}

// Close the connection


mysqli_close($conn);
?>

register_employee1.php
<?php
// Database connection
$conn = new mysqli('localhost', 'root', 'root', 'php');

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


$emp_name = $_POST['emp_name'];
$emp_number = $_POST['emp_number'];
$designation = $_POST['designation'];
$mobile = $_POST['mobile'];
$salary = $_POST['salary'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$address = $_POST['address'];
$username = $_POST['username'];
$password = password_hash($_POST['password'],
PASSWORD_BCRYPT);
$resume = $_FILES['resume']['name'];
$photo = $_FILES['photo']['name'];

// Validations
if (empty($emp_name) || empty($emp_number) || empty($email) ||
empty($password) || $_POST['password'] !=
$_POST['confirm_password']) {
echo "Please fill all required fields correctly.";
} else {
// File upload for resume and photo
move_uploaded_file($_FILES['resume']['tmp_name'],
"uploads/resumes/$resume");
move_uploaded_file($_FILES['photo']['tmp_name'],
"uploads/photos/$photo");

// Insert into database


$sql = "INSERT INTO employees (emp_name, emp_number,
designation, mobile, salary, gender, email, address, username, password,
resume, photo)
VALUES ('$emp_name', '$emp_number', '$designation',
'$mobile', '$salary', '$gender', '$email', '$address', '$username',
'$password', '$resume', '$photo')";

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


echo "Employee Registered Successfully!";
} else {
echo "Error: " . $conn->error;
}
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Employee Registration</title>
<style>
/* Add some styling */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
form {
display: flex;
flex-direction: column;
width: 400px;
margin: auto;
}
input, select, textarea {
margin-bottom: 10px;
padding: 10px;
}
</style>
</head>
<body>
<h2>Register Employee</h2>
<form method="POST" enctype="multipart/form-data">
<input type="text" name="emp_name" placeholder="Employee
Name" required>
<input type="number" name="emp_number"
placeholder="Employee Number" required>
<input type="text" name="designation" placeholder="Designation"
required>
<input type="text" name="mobile" placeholder="Mobile No"
required>
<input type="number" step="0.01" name="salary"
placeholder="Salary" required>
<select name="gender" required>
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
<input type="email" name="email" placeholder="Email ID"
required>
<textarea name="address" placeholder="Address"
required></textarea>
<input type="file" name="photo" accept="image/*" required>
<input type="file" name="resume" accept=".pdf,.docx" required>
<input type="text" name="username" placeholder="Username"
required>
<input type="password" name="password" placeholder="Password"
required>
<input type="password" name="confirm_password"
placeholder="Confirm Password" required>
<button type="submit">Register</button>
</form>
</body>
</html>

View_employees.php
<?php
$conn = new mysqli('localhost', 'root', 'root', 'php');

// Fetch employees
$result = $conn->query("SELECT * FROM employees ORDER BY
emp_number ASC");

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>View Employees</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<h2>Employee List</h2>
<table>
<thead>
<tr>
<th>Employee Number</th>
<th>Name</th>
<th>Designation</th>
<th>Mobile</th>
<th>Salary</th>
<th>Gender</th>
<th>Email</th>
<th>Address</th>
<th>Photo</th>
<th>Resume</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?= $row['emp_number'] ?></td>
<td><?= $row['emp_name'] ?></td>
<td><?= $row['designation'] ?></td>
<td><?= $row['mobile'] ?></td>
<td><?= $row['salary'] ?></td>
<td><?= $row['gender'] ?></td>
<td><?= $row['email'] ?></td>
<td><?= $row['address'] ?></td>
<td><img src="uploads/photos/<?= $row['photo'] ?>"
width="50" alt="photo"></td>
<td><a href="uploads/resumes/<?= $row['resume'] ?>">View
Resume</a></td>
<td><a href="edit_employee.php?id=<?= $row['emp_id']
?>">Edit</a></td>
<td><a href="delete_employee.php?id=<?= $row['emp_id'] ?>"
onclick="return confirm('Are you sure?')">Delete</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>

Edit_employee.php
<?php
$conn = new mysqli('localhost', 'root', 'root', 'php');

if (isset($_GET['id'])) {
$emp_id = $_GET['id'];
$sql = "DELETE FROM employees WHERE emp_id=$emp_id";

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


echo "Employee deleted successfully.";
} else {
echo "Error: " . $conn->error;
}
}
header("Location: view_employees.php");
?>
2.Create Php Web Pages for Super Store to help admin to manage the
customer details with Customer Id, Name, Address, Contact No, DOB,
Emil id, Address Proof( any photo licence/aadhar card) and provide
functionalities like. a) Add new customer b) view all customer c) delete
customer d) search customer based on contactno Note: Address Proof(
any photo licence/aadhar card) – you can add any sample image(Not
Personal)

Table2.php
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "root", "root");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database if it doesn't exist


$sql = "CREATE DATABASE IF NOT EXISTS php";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}

// Select the database


mysqli_select_db($conn, "php");

// SQL query to create the table


$sql = "CREATE TABLE IF NOT EXISTS customers (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
address TEXT NOT NULL,
contact_no VARCHAR(15) UNIQUE NOT NULL,
dob DATE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
address_proof VARCHAR(255) NOT NULL
)";

// Execute the query


if (mysqli_query($conn, $sql)) {
echo "Table 'customers' created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
// Close the connection
mysqli_close($conn);
?>

Add_customer.php
<?php
// Database connection
$conn = new mysqli('localhost', 'root', 'root', 'php');

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


$name = $_POST['name'];
$address = $_POST['address'];
$contact_no = $_POST['contact_no'];
$dob = $_POST['dob'];
$email = $_POST['email'];
$address_proof = $_FILES['address_proof']['name'];

// Validation
if (empty($name) || empty($contact_no) || empty($email) ||
empty($dob)) {
echo "Please fill all the required fields.";
} else {
// Upload address proof image
move_uploaded_file($_FILES['address_proof']['tmp_name'],
"uploads/address_proofs/$address_proof");

// Insert into database


$sql = "INSERT INTO customers (name, address, contact_no, dob,
email, address_proof)
VALUES ('$name', '$address', '$contact_no', '$dob', '$email',
'$address_proof')";

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


echo "Customer added successfully!";
} else {
echo "Error: " . $conn->error;
}
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Add New Customer</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
form {
display: flex;
flex-direction: column;
width: 400px;
margin: auto;
}
input, textarea {
margin-bottom: 10px;
padding: 10px;
}
</style>
</head>
<body>
<h2>Add New Customer</h2>
<form method="POST" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Customer Name"
required>
<textarea name="address" placeholder="Address"
required></textarea>
<input type="text" name="contact_no" placeholder="Contact No"
required>
<input type="date" name="dob" required>
<input type="email" name="email" placeholder="Email ID"
required>
<input type="file" name="address_proof" accept="image/*"
required>
<button type="submit">Add Customer</button>
</form>
</body>
</html>

View_customer.php
<?php
$conn = new mysqli('localhost', 'root', 'root', 'php');

// Fetch all customers


$result = $conn->query("SELECT * FROM customers ORDER BY
customer_id ASC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>View Customers</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<h2>Customer List</h2>
<table>
<thead>
<tr>
<th>Customer ID</th>
<th>Name</th>
<th>Address</th>
<th>Contact No</th>
<th>Date of Birth</th>
<th>Email</th>
<th>Address Proof</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?= $row['customer_id'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['address'] ?></td>
<td><?= $row['contact_no'] ?></td>
<td><?= $row['dob'] ?></td>
<td><?= $row['email'] ?></td>
<td><img src="uploads/address_proofs/<?=
$row['address_proof'] ?>" width="50" alt="address proof"></td>
<td><a href="delete_customer.php?id=<?= $row['customer_id']
?>" onclick="return confirm('Are you sure?')">Delete</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>

Delete_customer.php
<?php
$conn = new mysqli('localhost', 'root', 'root', 'php');

if (isset($_GET['id'])) {
$customer_id = $_GET['id'];
$sql = "DELETE FROM customers WHERE customer_id=$customer_id";

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


echo "Customer deleted successfully.";
} else {
echo "Error: " . $conn->error;
}
}
header("Location: view_customers.php");
?>

search_customer.php
<?php
$conn = new mysqli('localhost', 'root', 'root', 'php ');

$search_result = null;
if (isset($_POST['search'])) {
$contact_no = $_POST['contact_no'];
$result = $conn->query("SELECT * FROM customers WHERE
contact_no = '$contact_no'");
$search_result = $result->fetch_assoc();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Search Customer</title>
<style>
input {
padding: 10px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<h2>Search Customer by Contact No</h2>
<form method="POST">
<input type="text" name="contact_no" placeholder="Enter Contact
No" required>
<button type="submit" name="search">Search</button>
</form>

<?php if ($search_result) { ?>


<h3>Customer Details</h3>
<p><strong>Name:</strong> <?= $search_result['name'] ?></p>
<p><strong>Address:</strong> <?= $search_result['address'] ?></p>
<p><strong>Contact No:</strong> <?= $search_result['contact_no']
?></p>
<p><strong>Date of Birth:</strong> <?= $search_result['dob']
?></p>
<p><strong>Email:</strong> <?= $search_result['email'] ?></p>
<img src="uploads/address_proofs/<?=
$search_result['address_proof'] ?>" width="100" alt="Address Proof">
<?php } elseif (isset($_POST['search'])) { ?>
<p>No customer found with that contact number.</p>
<?php } ?>
</body>
</html>
3.Create Php Web Pages for Employee Management System. Provide
insert, update, delete and search facilities. Provide searching of
employee for particular department. Tables: Emp(empid, name,
Contactno, dept_id, empphoto, empresume) Dept(dept_id, dname)

<?php
// Connect to the database
$conn = mysqli_connect("localhost", "root", "root");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database if it doesn't exist


$sql = "CREATE DATABASE IF NOT EXISTS php";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}

// Select the database


mysqli_select_db($conn, "php");

// SQL query to create the table


$sql = "CREATE TABLE IF NOT EXISTS Dept (
dept_id INT AUTO_INCREMENT PRIMARY KEY,
dname VARCHAR(100) NOT NULL
)";

// Execute the query


if (mysqli_query($conn, $sql)) {
echo "Table 'dept' created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn);
}

// Close the connection


mysqli_close($conn);
?>

<?php
// Connect to the database
$conn = mysqli_connect("localhost", "root", "root");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database if it doesn't exist


$sql = "CREATE DATABASE IF NOT EXISTS php";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}

// Select the database


mysqli_select_db($conn, "php");

// SQL query to create the table


$sql = "CREATE TABLE IF NOT EXISTS Emp (
empid INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
contactno VARCHAR(15) NOT NULL,
dept_id INT,
empphoto VARCHAR(255),
empresume VARCHAR(255),
FOREIGN KEY (dept_id) REFERENCES Dept(dept_id) ON DELETE SET
NULL
)";

// Execute the query


if (mysqli_query($conn, $sql)) {
echo "Table 'Emp' created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn);
}

// Close the connection


mysqli_close($conn);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Employee Management System</title>
</head>
<body>
<h2>Employee Management System</h2>
<nav>
<ul>
<li><a href="insert_emp.php">Insert Employee</a></li>
<li><a href="search_emp.php">Search Employee by
Department</a></li>
<li><a href="view_all_emp.php">View All Employees</a></li>
</ul>
</nav>
</body>
</html>

<?php
$conn = new mysqli("localhost", "root", "root", "php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['add_emp'])) {
$name = $conn->real_escape_string($_POST['name']);
$contactno = $conn->real_escape_string($_POST['contactno']);
$dept_id = (int)$_POST['dept_id'];
$empphoto = $_FILES['empphoto']['name'];
$empresume = $_FILES['empresume']['name'];

// File upload paths


$photo_target = "uploads/" . basename($empphoto);
$resume_target = "uploads/" . basename($empresume);

if (move_uploaded_file($_FILES['empphoto']['tmp_name'],
$photo_target) &&
move_uploaded_file($_FILES['empresume']['tmp_name'],
$resume_target)) {
$sql = "INSERT INTO Emp (name, contactno, dept_id, empphoto,
empresume)
VALUES ('$name', '$contactno', '$dept_id', '$empphoto',
'$empresume')";
if ($conn->query($sql) === TRUE) {
echo "Employee added successfully.";
} else {
echo "Error: " . $conn->error;
}
} else {
echo "Failed to upload files.";
}
}

// Fetch departments
$dept_query = "SELECT * FROM Dept";
$departments = $conn->query($dept_query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Insert Employee</title>
</head>
<body>
<h2>Insert Employee</h2>
<form action="insert_emp.php" method="POST"
enctype="multipart/form-data">
<label>Name:</label>
<input type="text" name="name" required><br>
<label>Contact No:</label>
<input type="text" name="contactno" required><br>
<label>Department:</label>
<select name="dept_id" required>
<?php while ($row = $departments->fetch_assoc()): ?>
<option value="<?= $row['dept_id'] ?>"><?= $row['dname']
?></option>
<?php endwhile; ?>
</select><br>
<label>Photo:</label>
<input type="file" name="empphoto" required><br>
<label>Resume:</label>
<input type="file" name="empresume" required><br>
<input type="submit" name="add_emp" value="Add Employee">
</form>
<a href="index.php">Back to Home</a>
</body>
</html>

<?php
$conn = new mysqli("localhost", "root", "root", "php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Fetch employees
$sql = "SELECT Emp.*, Dept.dname FROM Emp LEFT JOIN Dept ON
Emp.dept_id = Dept.dept_id";
$employees = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>All Employees</title>
</head>
<body>
<h2>All Employees</h2>
<table border="1">
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Contact No</th>
<th>Department</th>
<th>Photo</th>
<th>Resume</th>
<th>Actions</th>
</tr>
<?php while ($row = $employees->fetch_assoc()): ?>
<tr>
<td><?= $row['empid'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['contactno'] ?></td>
<td><?= $row['dname'] ?></td>
<td><img src="uploads/<?= $row['empphoto'] ?>"
width="50"></td>
<td><a href="uploads/<?= $row['empresume'] ?>"
target="_blank">View</a></td>
<td>
<a href="edit_emp.php?empid=<?= $row['empid']
?>">Edit</a> |
<a href="delete_emp.php?empid=<?= $row['empid'] ?>"
onclick="return confirm('Are you sure?')">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
<a href="index.php">Back to Home</a>
</body>
</html>

<?php
$conn = new mysqli("localhost", "root", "root", "php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['search_dept'])) {
$dept_id = (int)$_POST['dept_id'];
$sql = "SELECT Emp.*, Dept.dname FROM Emp LEFT JOIN Dept ON
Emp.dept_id = Dept.dept_id WHERE Emp.dept_id = '$dept_id'";
$result = $conn->query($sql);
}

// Fetch departments
$dept_query = "SELECT * FROM Dept";
$departments = $conn->query($dept_query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Search Employee by Department</title>
</head>
<body>
<h2>Search Employee by Department</h2>
<form action="search_emp.php" method="POST">
<label>Department:</label>
<select name="dept_id" required>
<?php while ($row = $departments->fetch_assoc()): ?>
<option value="<?= $row['dept_id'] ?>"><?= $row['dname']
?></option>
<?php endwhile; ?>
</select>
<input type="submit" name="search_dept" value="Search">
</form>

<?php if (isset($result)): ?>


<h3>Employees in Department</h3>
<table border="1">
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Contact No</th>
<th>Department</th>
<th>Photo</th>
<th>Resume</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?= $row['empid'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['contactno'] ?></td>
<td><?= $row['dname'] ?></td>
<td><img src="uploads/<?= $row['empphoto'] ?>"
width="50"></td>
<td><a href="uploads/<?= $row['empresume'] ?>"
target="_blank">View</a></td>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>

<a href="index.php">Back to Home</a>


</body>
</html>
4.Create Php Web Pages for Academic Institute. Provide insert, update,
delete and search facilities. Student table(Stud_id, Stud_name,
Course_id, Contact_no, Address,photo) BCourse table(Course_id,
Course_type[eg. : IT, Management, Commerce), Fees) Provide searching
of Students for particular Course.

<?php
// Create connection
$con = mysqli_connect("localhost", "root", "", "");
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_error());
} else {
echo "Connection successful<br>";
}
// Create database
if (mysqli_query($con, "CREATE DATABASE academic_institute")) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($con);
}

// Close connection
mysqli_close($con);
?>
• Output:
Connection successful
Database created successfully

• Academic Institute:

<?php
// Database connection
$conn = new mysqli('localhost', 'root','','academic_institute');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create tables if they do not exist
$create_course_table = "CREATE TABLE IF NOT EXISTS BCourse (
Course_id INT AUTO_INCREMENT PRIMARY KEY,
Course_type VARCHAR(100) NOT NULL,
Fees DECIMAL(10, 2) NOT NULL
)";
$conn->query($create_course_table);
$create_student_table = "CREATE TABLE IF NOT EXISTS Student (
Stud_id INT AUTO_INCREMENT PRIMARY KEY,
Stud_name VARCHAR(100) NOT NULL,
Course_id INT NOT NULL,
Contact_no VARCHAR(15) NOT NULL UNIQUE,
Address VARCHAR(255),
photo VARCHAR(255),
FOREIGN KEY (Course_id) REFERENCES BCourse(Course_id)
)";
$conn->query($create_student_table);
// Sample Courses (uncomment to add sample data)
$sample_courses = [
["IT", 1000],
["Management", 1200],
["Commerce", 800]
];
foreach ($sample_courses as $course) {
$conn->query("INSERT IGNORE INTO BCourse (Course_type, Fees)
VALUES ('$course[0]', $course[1])");
}
// Handle adding a new student
if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
isset($_POST['add_student'])) {
$name = $_POST['stud_name'];
$contact_no = $_POST['contact_no'];
$address = $_POST['address'];
$course_id = $_POST['course_id'];
// File upload
$photo = $_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/' .
$photo);
$stmt = $conn->prepare("INSERT INTO Student (Stud_name, Course_id,
Contact_no, Address, photo) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sisss", $name, $course_id, $contact_no,
$address, $photo);
if ($stmt->execute()) {
echo "<p class='success'>Student added successfully!</p>";
} else {
echo "<p class='error'>Error: " . $stmt->error . "</p>";
}
$stmt->close();
}
// Handle updating a student
if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
isset($_POST['update_student'])) {
$stud_id = $_POST['stud_id'];
$name = $_POST['stud_name'];
$contact_no = $_POST['contact_no'];
$address = $_POST['address'];
$course_id = $_POST['course_id'];
$stmt = $conn->prepare("UPDATE Student SET Stud_name=?,
Course_id=?, Contact_no=?, Address=? WHERE Stud_id=?");
$stmt->bind_param("sissi", $name, $course_id, $contact_no,
$address, $stud_id);

if ($stmt->execute()) {
echo "<p class='success'>Student updated successfully!</p>";
} else {
echo "<p class='error'>Error: " . $stmt->error . "</p>";
}
$stmt->close();
}
// Handle deleting a student
if (isset($_GET['delete_id'])) {
$id = $_GET['delete_id'];
$conn->query("DELETE FROM Student WHERE Stud_id=$id");
}
// Handle searching students
$search_result = null;
if (isset($_POST['search_student'])) {
$course_id = $_POST['course_id'];
$search_result = $conn->query("SELECT * FROM Student WHERE
Course_id='$course_id'");
}
// Fetch all courses for dropdown
$courses = $conn->query("SELECT * FROM BCourse");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Academic Institute Management</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color:
#f4f4f4; }
.container { max-width: 800px; margin: auto; background: #fff;
padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0,
0.1); }
h2 { border-bottom: 2px solid #007BFF; padding-bottom: 10px;
color: #007BFF; }
label { display: block; margin: 10px 0 5px; }
input[type="text"], input[type="file"], select {
width: calc(100% - 20px);
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
padding: 10px 15px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover { background-color: #0056b3; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
table, th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #007BFF; color: #fff; }
.success { color: green; }
.error { color: red; }
.delete-link { color: red; text-decoration: none; }
.delete-link:hover { text-decoration: underline; }
</style>
</head>
<body>
<div class="container">
<h2>Add New Student</h2>
<form action="" method="post" enctype="multipart/form-data">
<label>Name:</label>
<input type="text" name="stud_name" required
placeholder="Enter Student Name">
<label>Contact No:</label>
<input type="text" name="contact_no" required
placeholder="Enter Contact No">
<label>Address:</label>
<input type="text" name="address" required placeholder="Enter
Address">
<label>Course:</label>
<select name="course_id" required>
<option value="">Select Course</option>
<?php while ($row = $courses->fetch_assoc()): ?>
<option value="<?php echo $row['Course_id']; ?>"><?php echo
$row['Course_type']; ?></option>
<?php endwhile; ?>
</select>
<label>Photo:</label>
<input type="file" name="photo" accept="image/*" required>
<button type="submit" name="add_student">Add Student</button>
</form>
<h2>Search Students by Course</h2>
<form action="" method="post">
<label>Course:</label>
<select name="course_id" required>
<option value="">Select Course</option>
<?php
$courses->data_seek(0); // Reset pointer to the beginning
while ($row = $courses->fetch_assoc()): ?>
<option value="<?php echo $row['Course_id']; ?>"><?php
echo $row['Course_type']; ?></option>
<?php endwhile; ?>
</select>
<button type="submit" name="search_student">Search</button>
</form>
<?php if ($search_result): ?>
<h3>Search Results</h3>
<table>
<tr>
<th>Student ID</th>
<th>Name</th>
<th>Contact No</th>
<th>Address</th>
<th>Course ID</th>
<th>Photo</th>
<th>Actions</th>
</tr>
<?php while ($row = $search_result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['Stud_id']; ?></td>
<td><?php echo $row['Stud_name']; ?></td>
<td><?php echo $row['Contact_no']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Course_id']; ?></td>
<td><img src="uploads/<?php echo $row['photo']; ?>"
width="50" alt="Student Photo"></td>
<td>
<a class="delete-link" href="?delete_id=<?php echo
$row['Stud_id']; ?>" onclick="return confirm('Are you sure you want to
delete this student?');">Delete</a>
<a href="#" onclick="editStudent(<?php echo
$row['Stud_id']; ?>, '<?php echo $row['Stud_name']; ?>', '<?php echo
$row['Contact_no']; ?>', '<?php echo $row['Address']; ?>', <?php echo
$row['Course_id']; ?>)">Edit</a>
</td>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
<h2>All Students</h2>
<table>
<tr>
<th>Student ID</th>
<th>Name</th>
<th>Contact No</th>
<th>Address</th>
<th>Course ID</th>
<th>Photo</th>
<th>Actions</th>
</tr>
<?php $all_students = $conn->query("SELECT * FROM Student");
?>
<?php while ($row = $all_students->fetch_assoc()): ?>
<tr>
<td><?php echo $row['Stud_id']; ?></td>
<td><?php echo $row['Stud_name']; ?></td>
<td><?php echo $row['Contact_no']; ?></td>
<td><?php echo $row['Address']; ?></td>
<td><?php echo $row['Course_id']; ?></td>
<td><img src="uploads/<?php echo $row['photo']; ?>"
width="50" alt="Student Photo"></td>
<td>
<a class="delete-link" href="?delete_id=<?php echo
$row['Stud_id']; ?>" onclick="return confirm('Are you sure you want to
delete this student?');">Delete</a>
<a href="#" onclick="editStudent(<?php echo $row['Stud_id'];
?>, '<?php echo $row['Stud_name']; ?>', '<?php echo
$row['Contact_no']; ?>', '<?php echo $row['Address']; ?>', <?php echo
$row['Course_id']; ?>)">Edit</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<script>
function editStudent(stud_id, name, contact_no, address,
course_id) {
const courses = <?php
$courses->data_seek(0);
echo json_encode($courses->fetch_all(MYSQLI_ASSOC));
?>;
let options = '<option value="">Select Course</option>';
courses.forEach(course => {options += `<option
value="${course.Course_id}" ${course_id == course.Course_id ?
'selected' : ''}>${course.Course_type}</option>`;
});
document.querySelector('.container').innerHTML += `
<h2>Update Student</h2>
<form action="" method="post" enctype="multipart/form-
data">
<input type="hidden" name="stud_id" value="${stud_id}">
<label>Name:</label>
<input type="text" name="stud_name" value="${name}"
required>
<label>Contact No:</label>
<input type="text" name="contact_no"
value="${contact_no}" required> <label>Address:</label>
<input type="text" name="address" value="${address}"
required>
<label>Course:</label>
<select name="course_id" required>${options}</select>
<button type="submit" name="update_student">Update
Student</button>
</form>’; }
</script>
</body>
</html>
<?php $conn->close(); ?>
• Search,update and delete:

• Database:

• ===========================================================

5.Create Php Web Pages for Student Registration form with following
fields: Student Name, Enrollment Number, Department, Year, Sem,
Mobile no., email id, address, username, password, confirm password.
Details should be edit, Delete and View with appropriate validations.
Also apply proper design accordingly. Note: Create tables as per required
and Provide Validations as wherever required, also make the use of
Sessions, Cookies and jQuery.

<?php
// Connect to the database
$conn = mysqli_connect("localhost", "root", "root");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database if it doesn't exist


$sql = "CREATE DATABASE IF NOT EXISTS php";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}

// Select the database


mysqli_select_db($conn, "php");

// SQL query to create the table


$sql = "CREATE TABLE IF NOT EXISTS Student (
student_id INT AUTO_INCREMENT PRIMARY KEY,
enrollment_number VARCHAR(15) NOT NULL UNIQUE,
student_name VARCHAR(255) NOT NULL,
department VARCHAR(100) NOT NULL,
year INT NOT NULL,
semester VARCHAR(10) NOT NULL,
mobile_no VARCHAR(15) NOT NULL,
email_id VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
)";

// Execute the query


if (mysqli_query($conn, $sql)) {
echo "Table 'student' created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn);
}

// Close the connection


mysqli_close($conn);
?>

<?php
$conn = new mysqli('localhost', 'root', 'root', 'php');

session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get form data
$enrollment_number = $_POST['enrollment_number'];
$student_name = $_POST['student_name'];
$department = $_POST['department'];
$year = $_POST['year'];
$semester = $_POST['semester'];
$mobile_no = $_POST['mobile_no'];
$email_id = $_POST['email_id'];
$address = $_POST['address'];
$username = $_POST['username'];
$password = password_hash($_POST['password'],
PASSWORD_BCRYPT); // Hashing the password

// Check for duplicate username or enrollment number


$check_sql = "SELECT * FROM Student WHERE username =
'$username' OR enrollment_number = '$enrollment_number'";
$check_result = $conn->query($check_sql);

if ($check_result->num_rows > 0) {
echo "Username or Enrollment number already exists.";
} else {
// Insert student into database
$sql = "INSERT INTO Student (enrollment_number, student_name,
department, year, semester, mobile_no, email_id, address, username,
password)
VALUES ('$enrollment_number', '$student_name',
'$department', '$year', '$semester', '$mobile_no', '$email_id',
'$address', '$username', '$password')";

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


echo "Student registered successfully!";
} else {
echo "Error: " . $conn->error;
}
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Student Registration</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#registrationForm').on('submit', function(e) {
var password = $('#password').val();
var confirmPassword = $('#confirm_password').val();
if (password !== confirmPassword) {
alert("Passwords do not match!");
e.preventDefault();
}
});
});
</script>
</head>
<body>
<h2>Student Registration</h2>
<form id="registrationForm" method="POST">
<input type="text" name="enrollment_number"
placeholder="Enrollment Number" required><br>
<input type="text" name="student_name" placeholder="Student
Name" required><br>
<input type="text" name="department" placeholder="Department"
required><br>
<input type="number" name="year" placeholder="Year"
required><br>
<input type="text" name="semester" placeholder="Semester"
required><br>
<input type="text" name="mobile_no" placeholder="Mobile No"
required><br>
<input type="email" name="email_id" placeholder="Email ID"
required><br>
<textarea name="address" placeholder="Address"
required></textarea><br>
<input type="text" name="username" placeholder="Username"
required><br>
<input type="password" id="password" name="password"
placeholder="Password" required><br>
<input type="password" id="confirm_password"
placeholder="Confirm Password" required><br>
<button type="submit">Register</button>
</form>
</body>
</html>

<?php
$conn = new mysqli('localhost', 'root', 'root', 'php');
session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM Student WHERE username='$username'";
$result = $conn->query($sql);

if ($result->num_rows == 1) {
$user = $result->fetch_assoc();
if (password_verify($password, $user['password'])) {
// Set session
$_SESSION['username'] = $user['username'];

// Set cookies if remember me is checked


if (isset($_POST['remember_me'])) {
setcookie('username', $user['username'], time() + (86400 * 30),
"/");
}

header("Location: dashboard.php");
} else {
echo "Invalid password.";
}
} else {
echo "Username does not exist.";
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="POST">
<input type="text" name="username" placeholder="Username"
required><br>
<input type="password" name="password" placeholder="Password"
required><br>
<label>
<input type="checkbox" name="remember_me"> Remember Me
</label><br>
<button type="submit">Login</button>
</form>
</body>
</html>

<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Dashboard</title>
</head>
<body>
<h2>Welcome, <?= $_SESSION['username'] ?></h2>
<a href="edit_student.php">Edit Details</a> |
<a href="delete_student.php">Delete Account</a> |
<a href="view_student.php">View Details</a> |
<a href="logout.php">Logout</a>
</body>
</html>
<?php
session_start();
$conn = new mysqli('localhost', 'root', 'root', 'php');

if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit;
}

$username = $_SESSION['username'];
$sql = "SELECT * FROM Student WHERE username='$username'";
$result = $conn->query($sql);
$student = $result->fetch_assoc();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$student_name = $_POST['student_name'];
$department = $_POST['department'];
$year = $_POST['year'];
$semester = $_POST['semester'];
$mobile_no = $_POST['mobile_no'];
$email_id = $_POST['email_id'];
$address = $_POST['address'];

$update_sql = "UPDATE Student SET student_name='$student_name',


department='$department', year='$year', semester='$semester',
mobile_no='$mobile_no', email_id='$email_id', address='$address'
WHERE username='$username'";

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


echo "Details updated successfully!";
} else {
echo "Error: " . $conn->error;
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Edit Student Details</title>
</head>
<body>
<h2>Edit Student Details</h2>
<form method="POST">
<input type="text" name="student_name" value="<?=
$student['student_name'] ?>" required><br>
<input type="text" name="department" value="<?=
$student['department'] ?>" required><br>
<input type="number" name="year" value="<?= $student['year'] ?>"
required><br>
<input type="text" name="semester" value="<?=
$student['semester'] ?>" required><br>
<input type="text" name="semester" value="<?=
$student['semester'] ?>" required><br>
<input type="text" name="mobile_no" value="<?=
$student['mobile_no'] ?>" required><br>
<input type="email" name="email_id" value="<?=
$student['email_id'] ?>" required><br>
<textarea name="address" required><?= $student['address']
?></textarea><br>
<button type="submit">Update</button>
</form>
</body>
</html>

<?php
session_start();
$conn = new mysqli('localhost', 'root', 'root', 'php');

if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit;
}

$username = $_SESSION['username'];

$sql = "DELETE FROM Student WHERE username='$username'";

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


session_destroy();
echo "Account deleted successfully!";
} else {
echo "Error: " . $conn->error;
}
?>

<?php
session_start();
$conn = new mysqli('localhost', 'root', 'root', 'php');

if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit;
}

$username = $_SESSION['username'];
$sql = "SELECT * FROM Student WHERE username='$username'";
$result = $conn->query($sql);
$student = $result->fetch_assoc();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>View Student Details</title>
</head>
<body>
<h2>Student Details</h2>
<p><strong>Enrollment Number:</strong> <?=
$student['enrollment_number'] ?></p>
<p><strong>Student Name:</strong> <?= $student['student_name']
?></p>
<p><strong>Department:</strong> <?= $student['department']
?></p>
<p><strong>Year:</strong> <?= $student['year'] ?></p>
<p><strong>Semester:</strong> <?= $student['semester'] ?></p>
<p><strong>Mobile No:</strong> <?= $student['mobile_no'] ?></p>
<p><strong>Email ID:</strong> <?= $student['email_id'] ?></p>
<p><strong>Address:</strong> <?= $student['address'] ?></p>
</body>
</html>

<?php
session_start();
session_destroy();
header('Location: login.php');
exit;
?>

You might also like