利用web开发一段网吧管理系统代码
时间: 2023-08-23 13:05:51 浏览: 176
这里提供一个简单的网吧管理系统的代码,包括基本的登录、注册、添加机器、删除机器、显示机器等功能。
前端使用HTML、CSS、JavaScript和Bootstrap框架,后端使用PHP和MySQL数据库。
1. 登录页面(login.html)
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网吧管理系统 - 登录</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>网吧管理系统 - 登录</h2>
<form class="form-horizontal" action="login.php" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="username">用户名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" placeholder="请输入用户名" name="username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password">密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="请输入密码" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">登录</button>
<a href="register.html" class="btn btn-primary">注册</a>
</div>
</div>
</form>
</div>
</body>
</html>
```
2. 注册页面(register.html)
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网吧管理系统 - 注册</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>网吧管理系统 - 注册</h2>
<form class="form-horizontal" action="register.php" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="username">用户名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" placeholder="请输入用户名" name="username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password">密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="请输入密码" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">注册</button>
</div>
</div>
</form>
</div>
</body>
</html>
```
3. 添加机器页面(add.html)
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网吧管理系统 - 添加机器</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>网吧管理系统 - 添加机器</h2>
<form class="form-horizontal" action="add.php" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="name">机器名称:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" placeholder="请输入机器名称" name="name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="ip">IP地址:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="ip" placeholder="请输入IP地址" name="ip">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">添加</button>
</div>
</div>
</form>
</div>
</body>
</html>
```
4. 删除机器页面和显示机器页面(delete.php和index.php)
```php
<!-- delete.php -->
<?php
require_once 'common.php';
$id = $_GET['id'];
$sql = "DELETE FROM machine WHERE id=$id";
$result = mysqli_query($conn, $sql);
if ($result) {
header('Location: index.php');
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
<!-- index.php -->
<?php
require_once 'common.php';
$sql = "SELECT * FROM machine";
$result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网吧管理系统 - 机器列表</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>网吧管理系统 - 机器列表</h2>
<a href="add.html" class="btn btn-primary">添加机器</a>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>机器名称</th>
<th>IP地址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['ip']."</td>";
echo "<td><a href='delete.php?id=".$row['id']."'>删除</a></td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
```
5. 登录、注册、添加机器、删除机器的PHP代码(login.php、register.php、add.php)
```php
<!-- common.php -->
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webcafe";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<!-- login.php -->
<?php
require_once 'common.php';
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM user WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
header('Location: index.php');
} else {
echo "用户名或密码错误";
}
mysqli_close($conn);
?>
<!-- register.php -->
<?php
require_once 'common.php';
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "INSERT INTO user (username, password) VALUES ('$username', '$password')";
$result = mysqli_query($conn, $sql);
if ($result) {
header('Location: login.html');
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
<!-- add.php -->
<?php
require_once 'common.php';
$name = $_POST['name'];
$ip = $_POST['ip'];
$sql = "INSERT INTO machine (name, ip) VALUES ('$name', '$ip')";
$result = mysqli_query($conn, $sql);
if ($result) {
header('Location: index.php');
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
```
这样一个简单的网吧管理系统就完成了。需要注意的是,这里只提供了基本的功能,实际应用中需要考虑更多的安全性和可靠性问题。
阅读全文