Step 1: config.php (Database Connection)

This is where the connection to the database is established.

<?php // config.php define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', ''); define('DB_NAME', 'your_database_name'); // Attempt to connect to MySQL database $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); // Check the connection if ($connection === false) { die("ERROR: Could not connect. " . mysqli_connect_error()); } ?>

Step 2: index.php (Displaying Employee Records)

This file will display employee records fetched from the database.

<?php // index.php require_once 'config.php'; // Fetch records from the database $sql = "SELECT * FROM employees"; $result = mysqli_query($connection, $sql); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Employee Records</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Employee Records</h2> <a href="create.php" class="btn btn-success mb-3">Add New Employee</a> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start Date</th> <th>Salary</th> <th>Actions</th> </tr> </thead> <tbody> <?php while($row = mysqli_fetch_assoc($result)): ?> <tr> <td><?= $row['id']; ?></td> <td><?= $row['name']; ?></td> <td><?= $row['position']; ?></td> <td><?= $row['office']; ?></td> <td><?= $row['age']; ?></td> <td><?= $row['start_date']; ?></td> <td><?= $row['salary']; ?></td> <td> <a href="view.php?id=<?= $row['id']; ?>" class="btn btn-info">View</a> <a href="update.php?id=<?= $row['id']; ?>" class="btn btn-primary">Update</a> <a href="delete.php?id=<?= $row['id']; ?>" class="btn btn-danger">Delete</a> </td> </tr> <?php endwhile; ?> </tbody> </table> </div> </body> </html>

Step 3: create.php (Create Employee)

This page allows the creation of new employee records.

<?php // create.php require_once 'config.php'; $name = $position = $office = $age = $start_date = $salary = ""; $name_err = $position_err = $office_err = $age_err = $start_date_err = $salary_err = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { // Validate inputs $name = trim($_POST['name']); $position = trim($_POST['position']); $office = trim($_POST['office']); $age = trim($_POST['age']); $start_date = trim($_POST['start_date']); $salary = trim($_POST['salary']); if (empty($name) || empty($position) || empty($office) || empty($age) || empty($start_date) || empty($salary)) { echo "Please fill in all fields!"; } else { $sql = "INSERT INTO employees (name, position, office, age, start_date, salary) VALUES (?, ?, ?, ?, ?, ?)"; if ($stmt = mysqli_prepare($connection, $sql)) { mysqli_stmt_bind_param($stmt, "sssssi", $name, $position, $office, $age, $start_date, $salary); if (mysqli_stmt_execute($stmt)) { header("Location: index.php"); exit(); } else { echo "Something went wrong. Please try again."; } } mysqli_stmt_close($stmt); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Add Employee</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Add New Employee</h2> <form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control" value="<?= $name; ?>" required> </div> <div class="form-group"> <label>Position</label> <input type="text" name="position" class="form-control" value="<?= $position; ?>" required> </div> <div class="form-group"> <label>Office</label> <input type="text" name="office" class="form-control" value="<?= $office; ?>" required> </div> <div class="form-group"> <label>Age</label> <input type="number" name="age" class="form-control" value="<?= $age; ?>" required> </div> <div class="form-group"> <label>Start Date</label> <input type="date" name="start_date" class="form-control" value="<?= $start_date; ?>" required> </div> <div class="form-group"> <label>Salary</label> <input type="number" name="salary" class="form-control" value="<?= $salary; ?>" required> </div> <input type="submit" class="btn btn-primary" value="Add Employee"> <a href="index.php" class="btn btn-default">Cancel</a> </form> </div> </body> </html>

Step 4: view.php (View Employee Details)

This page shows the details of a specific employee.

<?php // view.php require_once 'config.php'; $id = $_GET['id']; $sql = "SELECT * FROM employees WHERE id = ?"; if ($stmt = mysqli_prepare($connection, $sql)) { mysqli_stmt_bind_param($stmt, "i", $id); if (mysqli_stmt_execute($stmt)) { $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) { $name = $row['name']; $position = $row['position']; $office = $row['office']; $age = $row['age']; $start_date = $row['start_date']; $salary = $row['salary']; } } mysqli_stmt_close($stmt); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>View Employee</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Employee Details</h2> <p><strong>Name:</strong> <?= $name; ?></p> <p><strong>Position:</strong> <?= $position; ?></p> <p><strong>Office:</strong> <?= $office; ?></p> <p><strong>Age:</strong> <?= $age; ?></p> <p><strong>Start Date:</strong> <?= $start_date; ?></p> <p><strong>Salary:</strong> <?= $salary; ?></p> <a href="index.php" class="btn btn-primary">Back to List</a> </div> </body> </html>

Step 5: update.php (Update Employee Details)

This page allows updating an employee's details.

<?php // update.php require_once 'config.php'; $id = $_GET['id']; $sql = "SELECT * FROM employees WHERE id = ?"; if ($stmt = mysqli_prepare($connection, $sql)) { mysqli_stmt_bind_param($stmt, "i", $id); if (mysqli_stmt_execute($stmt)) { $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) { $name = $row['name']; $position = $row['position']; $office = $row['office']; $age = $row['age']; $start_date = $row['start_date']; $salary = $row['salary']; } } mysqli_stmt_close($stmt); } if ($_SERVER["REQUEST_METHOD"] == "POST") { // Validate inputs $name = trim($_POST['name']); $position = trim($_POST['position']); $office = trim($_POST['office']); $age = trim($_POST['age']); $start_date = trim($_POST['start_date']); $salary = trim($_POST['salary']); if (empty($name) || empty($position) || empty($office) || empty($age) || empty($start_date) || empty($salary)) { echo "Please fill in all fields!"; } else { $sql = "UPDATE employees SET name=?, position=?, office=?, age=?, start_date=?, salary=? WHERE id=?"; if ($stmt = mysqli_prepare($connection, $sql)) { mysqli_stmt_bind_param($stmt, "ssssssi", $name, $position, $office, $age, $start_date, $salary, $id); if (mysqli_stmt_execute($stmt)) { header("Location: index.php"); exit(); } else { echo "Something went wrong. Please try again."; } } mysqli_stmt_close($stmt); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Update Employee</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Update Employee</h2> <form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control" value="<?= $name; ?>" required> </div> <div class="form-group"> <label>Position</label> <input type="text" name="position" class="form-control" value="<?= $position; ?>" required> </div> <div class="form-group"> <label>Office</label> <input type="text" name="office" class="form-control" value="<?= $office; ?>" required> </div> <div class="form-group"> <label>Age</label> <input type="number" name="age" class="form-control" value="<?= $age; ?>" required> </div> <div class="form-group"> <label>Start Date</label> <input type="date" name="start_date" class="form-control" value="<?= $start_date; ?>" required> </div> <div class="form-group"> <label>Salary</label> <input type="number" name="salary" class="form-control" value="<?= $salary; ?>" required> </div> <input type="submit" class="btn btn-primary" value="Update Employee"> <a href="index.php" class="btn btn-default">Cancel</a> </form> </div> </body> </html>

Step 6: delete.php (Delete Employee)

This page handles the deletion of employee records.

<?php // delete.php require_once 'config.php'; $id = $_GET['id']; $sql = "DELETE FROM employees WHERE id = ?"; if ($stmt = mysqli_prepare($connection, $sql)) { mysqli_stmt_bind_param($stmt, "i", $id); if (mysqli_stmt_execute($stmt)) { header("Location: index.php"); exit(); } else { echo "Error deleting record."; } mysqli_stmt_close($stmt); } ?>

Step 7: error.php (Error Page)

This page shows an error when an invalid request is made.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Error</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"> </head> <body> <div class="container"> <div class="alert alert-danger"> <strong>Error!</strong> Invalid request. Please <a href="index.php" class="alert-link">go back</a> and try again. </div> </div> </body> </html>

This setup should be fully functional for basic CRUD operations (Create, Read, Update, Delete) on employee records in a MySQL database. You can further enhance and style it according to your needs.