How to use PHP to develop online Q&A function
Introduction:
With the development of the Internet, online Q&A platforms have gradually become the main way for people to obtain knowledge and information. one. In this process, PHP, as a powerful back-end programming language, is widely used in the development of various websites and applications. This article will introduce how to use PHP to develop a simple online question and answer function, including user registration, login, question, answer and other functions, and provide corresponding code examples.
1. Implementation of user registration and login functions
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); $sql = "CREATE TABLE users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, password VARCHAR(30) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table users created successfully"; } else { echo "Error creating table: " . $conn->error; } $conn->close();
Registration page (register.php):
<form action="register_process.php" method="POST"> <input type="text" name="username" placeholder="用户名" required><br> <input type="password" name="password" placeholder="密码" required><br> <button type="submit">注册</button> </form>
Registration handler (register_process.php):
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); $username = $_POST['username']; $password = $_POST['password']; $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; if ($conn->query($sql) === TRUE) { echo "注册成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close();
Login page (login.php):
<form action="login_process.php" method="POST"> <input type="text" name="username" placeholder="用户名" required><br> <input type="password" name="password" placeholder="密码" required><br> <button type="submit">登录</button> </form>
Login handler (login_process.php):
session_start(); $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = $conn->query($sql); if ($result->num_rows == 1) { $_SESSION['username'] = $username; echo "登录成功"; } else { echo "登录失败"; } $conn->close();
2. Question and answer function Implementation
$servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); $sql = "CREATE TABLE questions ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id INT(6) UNSIGNED, title VARCHAR(100) NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table questions created successfully"; } else { echo "Error creating table: " . $conn->error; } $sql = "CREATE TABLE answers ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, question_id INT(6) UNSIGNED, user_id INT(6) UNSIGNED, content TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table answers created successfully"; } else { echo "Error creating table: " . $conn->error; } $conn->close();
Question page (ask.php):
<form action="ask_process.php" method="POST"> <input type="text" name="title" placeholder="问题标题" required><br> <textarea name="content" placeholder="问题内容" required></textarea><br> <button type="submit">提问</button> </form>
Question handler (ask_process.php):
session_start(); $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); $title = $_POST['title']; $content = $_POST['content']; $user_id = $_SESSION['user_id']; $sql = "INSERT INTO questions (user_id, title, content) VALUES ('$user_id', '$title', '$content')"; if ($conn->query($sql) === TRUE) { echo "提问成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close();
Answer page (answer.php):
<form action="answer_process.php" method="POST"> <textarea name="content" placeholder="回答内容" required></textarea><br> <button type="submit">回答</button> </form>
Answer handler (answer_process.php):
session_start(); $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); $content = $_POST['content']; $question_id = $_POST['question_id']; $user_id = $_SESSION['user_id']; $sql = "INSERT INTO answers (question_id, user_id, content) VALUES ('$question_id', '$user_id', '$content')"; if ($conn->query($sql) === TRUE) { echo "回答成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close();
Conclusion:
Passed In the above steps, we have completed the development of a basic online question and answer function. Users can register, log in, ask and answer questions. Of course, this is just a simple example, and more functions and security measures may need to be added in actual applications. I hope this article can be helpful to developing online Q&A functions using PHP. Thank you for reading.
The above is the detailed content of How to use PHP to develop online question and answer function. For more information, please follow other related articles on the PHP Chinese website!