Home  >  Article  >  Backend Development  >  How to use PHP to develop a simple recruitment information publishing function

How to use PHP to develop a simple recruitment information publishing function

王林
王林Original
2023-09-20 12:29:001194browse

How to use PHP to develop a simple recruitment information publishing function

How to use PHP to develop a simple recruitment information publishing function

In recent years, with the rapid development of the Internet, recruitment information publishing platforms have become more and more important. As a powerful and easy-to-learn back-end development language, PHP provides us with a wealth of tools and functions to implement the recruitment information publishing function. This article will introduce how to use PHP to develop a simple recruitment information publishing function and provide specific code examples.

  1. Create database and table
    First, we need to create a table in the MySQL database to store recruitment information. You can use the following SQL statement to create a table named "jobs":
CREATE TABLE jobs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT NOT NULL,
    company VARCHAR(255) NOT NULL,
    location VARCHAR(255) NOT NULL,
    salary DECIMAL(10, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
  1. Create release information form
    Next, we need to create an HTML form for users to post recruitment information. You can use the following code example to create a simple form:
<form method="POST" action="submit_job.php">
    <input type="text" name="title" placeholder="职位名称" required>
    <textarea name="description" placeholder="职位描述" required></textarea>
    <input type="text" name="company" placeholder="公司名称" required>
    <input type="text" name="location" placeholder="工作地点" required>
    <input type="text" name="salary" placeholder="薪资" required>
    <button type="submit">发布</button>
</form>
  1. Processing form submission
    When the user submits the release information form, we need to use PHP to process the form data and Store data into database. You can use the following code example to create a file named "submit_job.php" to handle the submission of the form:
<?php
// 连接到数据库
$connection = mysqli_connect("localhost", "root", "password", "database_name");

// 检查是否连接成功
if (!$connection) {
    die("数据库连接失败:" . mysqli_connect_error());
}

// 获取表单中的数据
$title = $_POST['title'];
$description = $_POST['description'];
$company = $_POST['company'];
$location = $_POST['location'];
$salary = $_POST['salary'];

// 将数据插入到数据库中
$sql = "INSERT INTO jobs (title, description, company, location, salary) VALUES ('$title', '$description', '$company', '$location', $salary)";

if (mysqli_query($connection, $sq)) {
    echo "招聘信息发布成功!";
} else {
    echo "发布失败:" . mysqli_error($connection);
}

// 关闭数据库连接
mysqli_close($connection);
?>
  1. Display Published Job Information
    In order to allow users to browse and To view published job information, we need to create a page to display the published information. You can use the following code example to create a file named "jobs.php" to display published recruitment information:
<?php
// 连接到数据库
$connection = mysqli_connect("localhost", "root", "password", "database_name");

// 检查是否连接成功
if (!$connection) {
    die("数据库连接失败:" . mysqli_connect_error());
}

// 查询数据库中的所有招聘信息
$sql = "SELECT * FROM jobs";
$result = mysqli_query($connection, $sql);

// 显示查询结果
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<h2>{$row['title']}</h2>";
        echo "<p>{$row['description']}</p>";
        echo "<p>{$row['company']}</p>";
        echo "<p>{$row['location']}</p>";
        echo "<p>{$row['salary']}</p>";
        echo "<hr>";
    }
} else {
    echo "暂无招聘信息";
}

// 关闭数据库连接
mysqli_close($connection);
?>

Through the above steps, we have successfully developed a simple recruitment using PHP Information release function. Users can post job postings by filling out a form, and can also view posted job postings on another page. Of course, this is just a simple example, you can optimize and extend the code according to your own needs and business rules. Hope this article can be helpful to you!

The above is the detailed content of How to use PHP to develop a simple recruitment information publishing function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn