Home > Backend Development > PHP Tutorial > PHP implements the question posting function in the knowledge Q&A website.

PHP implements the question posting function in the knowledge Q&A website.

WBOY
Release: 2023-07-01 20:04:02
Original
1436 people have browsed it

PHP implements the question publishing function in the knowledge question and answer website

With the popularity and development of the Internet, the knowledge question and answer website has become more and more popular. In a trivia website, users can ask questions and get answers from other users. In this article, we will introduce how to use PHP to implement the question posting function in the knowledge Q&A website.

The question posting function is a key feature that allows users to ask questions on the website and post the questions to the database. The following is a simple sample code that demonstrates how to implement the question posting function:

First, create a question posting table (question) and database connection.

CREATE TABLE question (
    id INT(11) AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    user_id INT(11) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

$conn = new mysqli('localhost', 'username', 'password', 'database'); // 替换为正确的数据库连接信息

if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}
Copy after login

On the front-end page of the website, create a form for entering the title and content of the question and send the data to the server via a POST request.

<form action="submit_question.php" method="post">
    <label for="title">问题标题:</label>
    <input type="text" name="title" id="title" required><br>
    <label for="content">问题内容:</label>
    <textarea name="content" id="content" required></textarea><br>
    <input type="submit" value="发布问题">
</form>
Copy after login

Create a file named submit_question.php to handle the logic of question posting.

<?php
// 获取从表单传递过来的问题标题和内容
$title = $_POST['title'];
$content = $_POST['content'];
$user_id = 1; // 这里假设用户ID为1,可以根据实际情况进行修改

// SQL插入语句,将问题插入到question表中
$sql = "INSERT INTO question (title, content, user_id) VALUES ('$title', '$content', $user_id)";

if ($conn->query($sql) === TRUE) {
    echo "问题发布成功!";
} else {
    echo "问题发布失败:" . $conn->error;
}

$conn->close();
?>
Copy after login

In the above code, first obtain the question title, content and user ID passed from the form. Then, insert the title, content, and user ID of the question into the question table by executing a SQL insert statement. If the insertion is successful, "Problem posted successfully!" is output, otherwise an error message is output.

Through the above code examples, we can implement the question publishing function in the knowledge Q&A website. Users can publish the question to the database by filling in the title and content of the question and clicking the publish button. Developers can expand and optimize functions on this basis based on actual needs.

It should be noted that this article only provides a simple sample code and does not involve security and user experience issues such as user identity authentication and preventing SQL injection. In actual development, these aspects should be considered and corresponding measures taken to ensure website security and user experience.

The above is the detailed content of PHP implements the question posting function in the knowledge Q&A website.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template