<?php
// 数据库配置信息
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'student_work_management';
// 创建数据库连接
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 用户登录验证
function checkLogin($username, $password) {
global $conn;
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
return true;
} else {
return false;
}
}
// 添加新用户
function addUser($username, $password, $role) {
global $conn;
$sql = "INSERT INTO users (username, password, role) VALUES ('$username', '$password', '$role')";
if ($conn->query($sql) === TRUE) {
echo "新用户添加成功";
} else {
echo "错误: " . $sql . "<br>" . $conn->error;
}
}
// 显示所有用户
function showUsers() {
global $conn;
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "用户名: " . $row["username"]. " - 角色: " . $row["role"]. "<br>";
}
} else {
echo "0 结果";
}
}
?>
本站知识库部分内容及素材来源于互联网,如有侵权,联系必删!