Practical - 10 IWD

 

Aim : Employee Management System using PHP & MySQL


Database Creation

CREATE DATABASE employee_db; USE employee_db; CREATE TABLE employees ( emp_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100), password VARCHAR(100), department VARCHAR(50), salary FLOAT );

Database Connection (db.php)

<?php $conn = mysqli_connect("localhost", "root", "", "employee_db"); if (!$conn) { die("Database Connection Failed"); } ?>

i) Employee Registration Form (register.php)

<form method="post"> Name: <input type="text" name="name"><br> Email: <input type="email" name="email"><br> Password: <input type="password" name="password"><br> Department: <input type="text" name="department"><br> Salary: <input type="number" name="salary"><br> <input type="submit" name="submit"> </form>

Store Data in Database

<?php include "db.php"; if(isset($_POST['submit'])){ $sql = "INSERT INTO employees(name,email,password,department,salary) VALUES('$_POST[name]','$_POST[email]','$_POST[password]', '$_POST[department]','$_POST[salary]')"; mysqli_query($conn,$sql); echo "Employee Registered Successfully"; } ?>

ii) Employee Login Page (login.php)

<form method="post"> Email: <input type="email" name="email"><br> Password: <input type="password" name="password"><br> <input type="submit" name="login"> </form>
<?php session_start(); include "db.php"; if(isset($_POST['login'])){ $q = mysqli_query($conn, "SELECT * FROM employees WHERE email='$_POST[email]' AND password='$_POST[password]'"); if(mysqli_num_rows($q)==1){ $_SESSION['email']=$_POST['email']; header("Location: home.php"); } else { echo "Invalid Login"; } } ?>

iii) Script to Upload Image to Other Server (upload.php)

<form method="post" enctype="multipart/form-data"> Select Image: <input type="file" name="image"> <input type="submit" name="upload"> </form>
<?php if(isset($_POST['upload'])){ move_uploaded_file($_FILES['image']['tmp_name'], "uploads/".$_FILES['image']['name']); echo "Image Uploaded Successfully"; } ?>

iv) Home Page after Login (home.php)

<?php session_start(); include "db.php"; $email = $_SESSION['email']; $q = mysqli_query($conn,"SELECT * FROM employees WHERE email='$email'"); $row = mysqli_fetch_assoc($q); ?> <h2>Welcome <?php echo $row['name']; ?></h2> Department: <?php echo $row['department']; ?><br> Salary: <?php echo $row['salary']; ?>

v) Delete Employee Profile (delete.php)

<form method="post"> Enter Employee ID: <input type="number" name="id"> <input type="submit" name="delete"> </form>
<?php include "db.php"; if(isset($_POST['delete'])){ mysqli_query($conn,"DELETE FROM employees WHERE emp_id=$_POST[id]"); echo "Employee Deleted Successfully"; } ?>

vi) Change Password Page (change_password.php)

<form method="post"> New Password: <input type="password" name="newpass"> <input type="submit" name="change"> </form>
<?php session_start(); include "db.php"; if(isset($_POST['change'])){ $email=$_SESSION['email']; mysqli_query($conn, "UPDATE employees SET password='$_POST[newpass]' WHERE email='$email'"); echo "Password Updated"; } ?>

Comments

Popular posts from this blog

Unit I - Introduction to PHP

Practical - 1 IWD