使用MVC结构,实现简单的界面与数据库间数据的交互
时间: 2024-01-22 17:19:15 浏览: 65
MVC是一种常用的软件架构模式,它将应用程序分为三个部分:模型(Model)、视图(View)和控制器(Controller)。在MVC中,模型用于处理数据逻辑,视图显示数据,控制器负责接收和处理用户输入,并将其传递给模型或视图。
下面我们来实现一个简单的界面与数据库间数据的交互:
1. 创建一个数据库表
我们以学生信息管理系统为例,创建一个名为students的表,包含id、name、age和sex四个字段。
```
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
sex VARCHAR(10) NOT NULL
);
```
2. 创建一个Model类
在这个例子中,我们使用PHP语言,创建一个StudentModel类,用于处理与数据库的交互。该类需要实现以下功能:
- 连接数据库
- 插入新记录
- 修改记录
- 删除记录
- 查询记录
代码如下:
```php
<?php
class StudentModel {
private $host = 'localhost';
private $user = 'root';
private $password = 'password';
private $db = 'test';
private $conn;
public function __construct() {
$this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->db);
if (!$this->conn) {
die('Could not connect: ' . mysqli_error($this->conn));
}
}
public function insert($name, $age, $sex) {
$sql = "INSERT INTO students (name, age, sex) VALUES ('$name', $age, '$sex')";
if (mysqli_query($this->conn, $sql)) {
return true;
} else {
return false;
}
}
public function update($id, $name, $age, $sex) {
$sql = "UPDATE students SET name='$name', age=$age, sex='$sex' WHERE id=$id";
if (mysqli_query($this->conn, $sql)) {
return true;
} else {
return false;
}
}
public function delete($id) {
$sql = "DELETE FROM students WHERE id=$id";
if (mysqli_query($this->conn, $sql)) {
return true;
} else {
return false;
}
}
public function selectAll() {
$sql = "SELECT * FROM students";
$result = mysqli_query($this->conn, $sql);
$students = array();
while ($row = mysqli_fetch_assoc($result)) {
$students[] = $row;
}
return $students;
}
public function selectById($id) {
$sql = "SELECT * FROM students WHERE id=$id";
$result = mysqli_query($this->conn, $sql);
$row = mysqli_fetch_assoc($result);
return $row;
}
public function __destruct() {
mysqli_close($this->conn);
}
}
?>
```
3. 创建一个View类
我们再创建一个StudentView类,用于显示学生信息并与用户交互。该类需要实现以下功能:
- 显示学生列表
- 显示单个学生信息
- 添加学生
- 修改学生信息
- 删除学生
代码如下:
```php
<?php
class StudentView {
public function showAll($students) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Age</th><th>Sex</th><th>操作</th></tr>";
foreach ($students as $student) {
echo "<tr>";
echo "<td>" . $student['id'] . "</td>";
echo "<td>" . $student['name'] . "</td>";
echo "<td>" . $student['age'] . "</td>";
echo "<td>" . $student['sex'] . "</td>";
echo "<td><a href='update.php?id=" . $student['id'] . "'>修改</a> <a href='delete.php?id=" . $student['id'] . "'>删除</a></td>";
echo "</tr>";
}
echo "</table>";
}
public function show($student) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Age</th><th>Sex</th></tr>";
echo "<tr>";
echo "<td>" . $student['id'] . "</td>";
echo "<td>" . $student['name'] . "</td>";
echo "<td>" . $student['age'] . "</td>";
echo "<td>" . $student['sex'] . "</td>";
echo "</tr>";
echo "</table>";
}
public function add() {
echo "<form action='add.php' method='post'>";
echo "<label>Name:</label> <input type='text' name='name'><br>";
echo "<label>Age:</label> <input type='number' name='age'><br>";
echo "<label>Sex:</label> <input type='text' name='sex'><br>";
echo "<input type='submit' value='添加'>";
echo "</form>";
}
public function update($student) {
echo "<form action='update.php' method='post'>";
echo "<input type='hidden' name='id' value='" . $student['id'] . "'>";
echo "<label>Name:</label> <input type='text' name='name' value='" . $student['name'] . "'><br>";
echo "<label>Age:</label> <input type='number' name='age' value='" . $student['age'] . "'><br>";
echo "<label>Sex:</label> <input type='text' name='sex' value='" . $student['sex'] . "'><br>";
echo "<input type='submit' value='修改'>";
echo "</form>";
}
public function delete($id) {
echo "<p>确定删除该学生吗?</p>";
echo "<form action='delete.php' method='post'>";
echo "<input type='hidden' name='id' value='$id'>";
echo "<input type='submit' value='确定'>";
echo "</form>";
}
}
?>
```
4. 创建一个Controller类
最后,我们创建一个StudentController类,用于接收用户输入并调用Model和View的方法。它需要实现以下功能:
- 显示学生列表
- 显示单个学生信息
- 添加学生
- 修改学生信息
- 删除学生
代码如下:
```php
<?php
require_once 'StudentModel.php';
require_once 'StudentView.php';
class StudentController {
private $model;
private $view;
public function __construct($model, $view) {
$this->model = $model;
$this->view = $view;
}
public function index() {
$students = $this->model->selectAll();
$this->view->showAll($students);
}
public function show($id) {
$student = $this->model->selectById($id);
$this->view->show($student);
}
public function add() {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$result = $this->model->insert($name, $age, $sex);
if ($result) {
echo "添加成功!";
} else {
echo "添加失败!";
}
} else {
$this->view->add();
}
}
public function update($id) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$result = $this->model->update($id, $name, $age, $sex);
if ($result) {
echo "修改成功!";
} else {
echo "修改失败!";
}
} else {
$student = $this->model->selectById($id);
$this->view->update($student);
}
}
public function delete($id) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$result = $this->model->delete($id);
if ($result) {
echo "删除成功!";
} else {
echo "删除失败!";
}
} else {
$this->view->delete($id);
}
}
}
$model = new StudentModel();
$view = new StudentView();
$controller = new StudentController($model, $view);
if (isset($_GET['id'])) {
$controller->show($_GET['id']);
} else if (isset($_GET['add'])) {
$controller->add();
} else if (isset($_GET['update'])) {
$controller->update($_GET['id']);
} else if (isset($_GET['delete'])) {
$controller->delete($_GET['id']);
} else {
$controller->index();
}
?>
```
5. 创建一个入口文件index.php
最后,我们创建一个入口文件index.php,将所有类和方法整合起来。代码如下:
```php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息管理系统</h1>
<a href="index.php?add">添加学生</a>
<?php require_once 'StudentController.php'; ?>
</body>
</html>
```
现在,我们可以通过访问index.php来运行这个程序了,它将显示一个学生列表,并提供添加、修改和删除学生的功能。
阅读全文