Rumah >pembangunan bahagian belakang >tutorial php >Cara menggunakan PHP untuk membangunkan fungsi soal jawab dalam talian
Cara menggunakan PHP untuk membangunkan fungsi Soal Jawab dalam talian
Pengenalan:
Dengan perkembangan Internet, platform Soal Jawab dalam talian secara beransur-ansur menjadi salah satu cara utama untuk orang ramai memperoleh pengetahuan dan maklumat. Dalam proses ini, PHP, sebagai bahasa pengaturcaraan back-end yang berkuasa, digunakan secara meluas dalam pembangunan pelbagai laman web dan aplikasi. Artikel ini akan memperkenalkan cara menggunakan PHP untuk membangunkan fungsi soal jawab dalam talian yang mudah, termasuk pendaftaran pengguna, log masuk, soalan, jawapan dan fungsi lain, dan menyediakan contoh kod yang sepadan.
1. Pelaksanaan fungsi pendaftaran dan log masuk pengguna
$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();
Halaman pendaftaran (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>
Pengendali pendaftaran (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();
Laman log masuk (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>
Pengendali log masuk (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. Pelaksanaan fungsi soal jawab
$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();
<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>Tanya pengendali (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();
<form action="answer_process.php" method="POST"> <textarea name="content" placeholder="回答内容" required></textarea><br> <button type="submit">回答</button> </form>Pengendali jawapan (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();Kesimpulan:
Melalui langkah di atas, kami telah menyelesaikan pembangunan Soal Jawab dalam talian asas fungsi. Pengguna boleh mendaftar, log masuk, bertanya dan menjawab soalan. Sudah tentu, ini hanyalah contoh mudah, dan lebih banyak fungsi dan langkah keselamatan mungkin perlu ditambah dalam aplikasi sebenar. Saya harap artikel ini boleh membantu untuk membangunkan fungsi Soal Jawab dalam talian menggunakan PHP. Terima kasih kerana membaca.
Atas ialah kandungan terperinci Cara menggunakan PHP untuk membangunkan fungsi soal jawab dalam talian. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!