校園失物管理系統是一款方便學生和教職員管理失物招領資訊的實用工具。透過 PHP 程式語言,我們可以實現一個簡單且功能完餵的校園失物管理系統。下面,我將透過原始碼解析的方式,介紹該系統的設計想法以及具體的程式碼範例。
此失物管理系統主要包括以下幾個功能模組:
首先我們需要設計資料庫,包括以下資料表:
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "campus_lost_found"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
<form method="post" action="submit_lost_item.php"> <input type="text" name="title" placeholder="失物名称" required> <textarea name="description" placeholder="失物描述" required></textarea> <!-- 其他字段省略 --> <button type="submit">发布失物信息</button> </form>
<?php include 'db_connect.php'; $title = $_POST['title']; $description = $_POST['description']; $user_id = $_SESSION['user_id']; $sql = "INSERT INTO lost_items (title, description, user_id) VALUES ('$title', '$description', '$user_id')"; if ($conn->query($sql) === TRUE) { echo "失物信息发布成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
<?php include 'db_connect.php'; $search_query = $_GET['search']; $sql = "SELECT * FROM lost_items WHERE title LIKE '%$search_query%' OR description LIKE '%$search_query%'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "标题: " . $row["title"]. " - 描述: " . $row["description"]. "<br>"; } } else { echo "未找到相关失物信息"; } $conn->close(); ?>
<form method="post" action="admin_login.php"> <input type="text" name="username" placeholder="管理员用户名" required> <input type="password" name="password" placeholder="管理员密码" required> <button type="submit">登录</button> </form>
<?php include 'db_connect.php'; $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username='$username' AND password='$password' AND is_admin=1"; $result = $conn->query($sql); if ($result->num_rows > 0) { $_SESSION['admin_logged_in'] = true; echo "管理员登录成功"; } else { echo "管理员用户名或密码错误"; } $conn->close(); ?>
總結
以上是PHP實現校園失物管理系統的原始碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!