How to develop Projects(JPS) Using PHP and Mysql Part 9

Rashmi Mishra
4 minute read
0

 


How to develop Projects(JPS) Using PHP and Mysql   
Part 9

6️ Online Job Portal System

Module : Add Employer Details

📌 Folder Structure:

│── /employers/               

   ├── add_employer.php         # Add employer 

   ├── edit_employer.php        # Edit employer details 

   ├── delete_employer.php      # Delete employer 

   ├── employer_list.php        # List all employers 

Database Table:

CREATE TABLE employers (

    id INT AUTO_INCREMENT PRIMARY KEY,

    name VARCHAR(255) NOT NULL,

    company_name VARCHAR(255) NOT NULL,

    email VARCHAR(255) UNIQUE NOT NULL,

    industry VARCHAR(100),

    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

📌 File: employers/add_employer.php

<?php

// Include database connection

include('../config/db.php');

// Check if form is submitted

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

    $name = $_POST['name'];

    $company_name = $_POST['company_name'];

    $email = $_POST['email'];

    $industry = $_POST['industry'];

 

    // Validate inputs

    if (!empty($name) && !empty($company_name) && !empty($email)) {

        // Check if email already exists

        $email_check_query = "SELECT id FROM employers WHERE email = '$email'";

        $result = $conn->query($email_check_query);

 

        if ($result->num_rows > 0) {

            echo "Error: Email already exists!";

        } else {

            // Insert employer into the database

            $sql = "INSERT INTO employers (name, company_name, email, industry)

                    VALUES ('$name', '$company_name', '$email', '$industry')";

 

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

                echo "Employer added successfully.";

            } else {

                echo "Error: " . $conn->error;

            }

        }

    } else {

        echo "All fields except industry are required.";

    }

}

 

// Close connection

$conn->close();

?>

 

<!DOCTYPE html>

<html>

<head>

    <title>Add Employer</title>

</head>

<body>

    <h2>Add Employer</h2>

    <form method="POST" action="">

        <label>Name:</label>

        <input type="text" name="name" required><br><br>

 

        <label>Company Name:</label>

        <input type="text" name="company_name" required><br><br>

 

        <label>Email:</label>

        <input type="email" name="email" required><br><br>

 

        <label>Industry:</label>

        <input type="text" name="industry"><br><br>

 

        <input type="submit" value="Add Employer">

    </form>

</body>

</html>

Explanation:

1.   Connects to the MySQL database using MySQLi.

2.   Handles form submission via POST method.

3.   Checks if the email already exists in the database.

4.   Inserts data into the employers table if validation passes.

5.   Provides a basic HTML form for employer input.


File: employers/edit_employer.php

<?php

// Include database connection

include('../config/db.php');

// Check if employer ID is set

if (isset($_GET['id'])) {

    $id = $_GET['id'];

 

    // Fetch employer details

    $query = "SELECT * FROM employers WHERE id = $id";

    $result = $conn->query($query);

 

    if ($result->num_rows == 1) {

        $row = $result->fetch_assoc();

    } else {

        echo "Employer not found!";

        exit;

    }

}

 

// Handle form submission

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

    $id = $_POST['id'];

    $name = $_POST['name'];

    $company_name = $_POST['company_name'];

    $email = $_POST['email'];

    $industry = $_POST['industry'];

 

    // Validate inputs

    if (!empty($name) && !empty($company_name) && !empty($email)) {

        // Update employer details

        $updateQuery = "UPDATE employers

                        SET name='$name', company_name='$company_name', email='$email', industry='$industry'

                        WHERE id=$id";

 

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

            echo "Employer updated successfully.";

        } else {

            echo "Error: " . $conn->error;

        }

    } else {

        echo "All fields except industry are required.";

    }

}

 

// Close connection

$conn->close();

?>

 

<!DOCTYPE html>

<html>

<head>

    <title>Edit Employer</title>

</head>

<body>

    <h2>Edit Employer</h2>

    <form method="POST" action="">

        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">

 

        <label>Name:</label>

        <input type="text" name="name" value="<?php echo $row['name']; ?>" required><br><br>

 

        <label>Company Name:</label>

        <input type="text" name="company_name" value="<?php echo $row['company_name']; ?>" required><br><br>

 

        <label>Email:</label>

        <input type="email" name="email" value="<?php echo $row['email']; ?>" required><br><br>

 

        <label>Industry:</label>

        <input type="text" name="industry" value="<?php echo $row['industry']; ?>"><br><br>

 

        <input type="submit" value="Update Employer">

    </form>

</body>

</html>


Explanation:

1.   Connects to the MySQL database using MySQLi.

2.   Fetches employer details based on the id passed via GET method.

3.   Displays current employer details in an editable form.

4.   Handles form submission to update employer details in the database.

5.   Uses MySQLi query method to update the record.

6.   Basic validation ensures required fields are not empty.


Usage Instructions:

  • Access this file with a query parameter like:

http://localhost/employers/edit_employer.php?id=1

(Replace 1 with the actual employer ID)


File: employers/delete_employer.php

<?php

// Include database connection

include('../config/db.php');

 

// Check if employer ID is provided

if (isset($_GET['id'])) {

    $id = $_GET['id'];

 

    // Delete employer query

    $deleteQuery = "DELETE FROM employers WHERE id = $id";

 

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

        echo "Employer deleted successfully.";

    } else {

        echo "Error: " . $conn->error;

    }

} else {

    echo "No employer ID provided!";

}

 

// Close connection

$conn->close();

?>


Explanation:

1.   Connects to the MySQL database using MySQLi.

2.   Checks if an employer id is provided via GET method.

3.   Deletes the employer record from the database using a MySQLi query.

4.   Displays success or error messages based on the query execution.


Usage Instructions:

  • Call this file using a URL like:

http://localhost/employers/delete_employer.php?id=1

(Replace 1 with the actual employer ID)

  • To improve security, use prepared statements to prevent SQL injection. Would you like me to update the code with prepared statements? 🚀

File: employers/employer_list.php

<?php

// Include database connection

include('../config/db.php');

 

// Fetch all employers

$query = "SELECT * FROM employers";

$result = $conn->query($query);

?>

 

<!DOCTYPE html>

<html>

<head>

    <title>Employer List</title>

    <style>

        table {

            width: 100%;

            border-collapse: collapse;

        }

        table, th, td {

            border: 1px solid black;

        }

        th, td {

            padding: 10px;

            text-align: left;

        }

        th {

            background-color: #f2f2f2;

        }

        .action-links a {

            margin-right: 10px;

        }

    </style>

</head>

<body>

 

    <h2>Employer List</h2>

    <table>

        <tr>

            <th>ID</th>

            <th>Name</th>

            <th>Company Name</th>

            <th>Email</th>

            <th>Industry</th>

            <th>Created At</th>

            <th>Actions</th>

        </tr>

 

        <?php

        if ($result->num_rows > 0) {

            while ($row = $result->fetch_assoc()) {

                echo "<tr>

                        <td>{$row['id']}</td>

                        <td>{$row['name']}</td>

                        <td>{$row['company_name']}</td>

                        <td>{$row['email']}</td>

                        <td>{$row['industry']}</td>

                        <td>{$row['created_at']}</td>

                        <td class='action-links'>

                            <a href='edit_employer.php?id={$row['id']}'>Edit</a> |

                            <a href='delete_employer.php?id={$row['id']}' onclick='return confirm(\"Are you sure you want to delete this employer?\");'>Delete</a>

                        </td>

                      </tr>";

            }

        } else {

            echo "<tr><td colspan='7'>No employers found.</td></tr>";

        }

 

        // Close connection

        $conn->close();

        ?>

    </table>

 

</body>

</html>


Explanation:

1.   Connects to the MySQL database using MySQLi.

2.   Retrieves all employer records from the employers table.

3.   Displays the employer list in an HTML table.

4.   Includes action links for editing and deleting employers.

5.   Uses JavaScript confirm() before deleting an employer.


Usage Instructions:

  • Place this file inside the employers/ directory.
  • Access the file via:

http://localhost/employers/employer_list.php

  • Click "Edit" to update employer details.
  • Click "Delete" to remove an employer (with a confirmation prompt).

 

Post a Comment

0Comments

Post a Comment (0)