Practical: 10
1. Create a web page that reads employee information using a form and stores it in the
database.
index.php
<html>
<head>
<title>Employee Information</title>
</head>
<body>
<h2>Add Employee</h2>
<form action="process.php" method="POST">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<label for="position">Position:</label><br>
<input type="text" id="position" name="position"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
process.php
<?php
// Establish database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "employees";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$position = $_POST['position'];
// Insert data into database
$sql = "INSERT INTO employees (name, email, position) VALUES ('$name', '$email',
'$position')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
2. Create a web page for employee log-in.
index.php
<html>
<head>
<title>Employee Login</title>
</head>
<body>
<h2>Employee Login</h2>
<form action="login.php" 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>
login.php
<?php
// Establish a database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "employees";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get input values from the form
$username = $_POST['username'];
$password = $_POST['password'];
// Query the database for the user
$sql = "SELECT * FROM emp WHERE username = '$username' AND password =
'$password'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
// User found, redirect to dashboard or another page
header("Location: dashboard.php");
} else {
// User not found or credentials are incorrect
echo "Invalid username or password";
}
$conn->close();
?>
dashboard.php
<html >
<head>
<title>Dashboard</title>
</head>
<body>
<h2 style="color: pink;">Welcome to the Dashboard</h2>
</body>
</html>
Output:
3. Write a script to upload an image to the server.
image_upload.html
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<h2>Upload an Image</h2>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" required>
<button type="submit">Upload</button>
</form>
</body>
</html>
upload.php
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_FILES["image"])) {
$uploadDir = "C:/wamp64/www/adrika/pra10_3/";
$uploadFile = $uploadDir . basename($_FILES["image"]["name"]);
if (move_uploaded_file($_FILES["image"]["tmp_name"], $uploadFile)) {
echo "The file ". basename($_FILES["image"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
} else {
echo "No image uploaded.";
}
?>
Output:
4. After an employee logs in, create a Home web page that displays basic employee
information.
login.php
<?php
session_start();
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// Connect to MySQL
$conn = mysqli_connect('localhost', 'root', '', 'employees1');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Query to check if username and password match
$sql = "SELECT * FROM employees1 WHERE username='$username' AND
password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
header('Location: home.php');
} else {
echo "Invalid username or password";
}
mysqli_close($conn);
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST" action="">
USER NAME:<input type="text" name="username" placeholder="Username"
required><br>
PASSWORD:<input type="password" name="password" placeholder="Password"
required><br>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>
home.php
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit();
}
// Connect to MySQL
$conn = mysqli_connect('localhost', 'root', '', 'employees1');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$username = $_SESSION['username'];
// Query to get employee information
$sql = "SELECT * FROM employees1 WHERE username='$username'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_assoc($result);
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
// You can fetch other information similarly
} else {
echo "Employee not found";
}
mysqli_close($conn);
?>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1 style="color: green;">Welcome, <?php echo $name; ?></h1>
<p>ID: <?php echo $id; ?></p>
<p>Email: <?php echo $email; ?></p>
</body>
</html>
Output
5. Create a web page to delete employee profiles from the database.
delete.php
<?php
$conn = mysqli_connect('localhost', 'root', '', 'employees1');
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<title>Delete Employee Profiles</title>
</head>
<body>
<h1>Delete Employee Profiles</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
<?php
$sql = "SELECT * FROM employees1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><a href='delete_employee.php?id=" . $row['id'] .
"'>Delete</a></td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='4'>No records found</td></tr>";
}
?>
</table>
</body>
</html>
delete_employee.php
<?php
$conn = mysqli_connect('localhost', 'root', '', 'employees1');
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "DELETE FROM employees1 WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Employee deleted successfully";
} else {
echo "Error deleting employee: " . mysqli_error($conn);
}
} else {
echo "Invalid request";
}
mysqli_close($conn);
?>
utput:
6. Create a web page that allows employees to change their password.
change_password.php
<?php
$conn = mysqli_connect('localhost', 'root', '', 'employees1');
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<title>Change Password</title>
</head>
<body>
<h1>Change Password</h1>
<form method="POST" action="process_change_password.php">
Current Password:
<input type="password" id="current_password" name="current_password"
required><br><br>
New Password:
<input type="password" id="new_password" name="new_password"
required><br><br>
Confirm New Password:
<input type="password" id="confirm_password" name="confirm_password"
required><br><br>
<input type="submit" name="submit" value="Change Password">
</form>
</body>
</html>
process_change_password.php
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit();
}
$conn = mysqli_connect('localhost', 'root', '', 'employees1');
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$username = $_SESSION['username'];
$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
// Check if new password and confirm password match
if ($new_password !== $confirm_password) {
echo "New password and confirm password do not match.";
exit();
}
// Query to check if current password is correct
$sql = "SELECT * FROM employees1 WHERE username='$username' AND
password='$current_password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
// Update password
$update_sql = "UPDATE employees1 SET password='$new_password' WHERE
username='$username'";
if (mysqli_query($conn, $update_sql)) {
echo "Password changed successfully.";
} else {
echo "Error updating password: " . mysqli_error($conn);
}
} else {
echo "Incorrect current password.";
}
mysqli_close($conn);
?>
Output