创建一个学生表使用LayUI前端框架实现对数据库学生表得CRUD
时间: 2024-02-23 10:03:05 浏览: 75
LayUI+Mybatis实现CRUD效果.rar
LayUI是一个前端UI框架,不能直接用来对数据库进行CRUD操作,你需要使用后端语言(如PHP、Python、Java等)来实现对数据库的操作。以下是一个使用PHP语言和MySQL数据库进行学生表CRUD操作的例子:
```
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 查询学生表
$sql = "SELECT * FROM student";
$result = mysqli_query($conn, $sql);
// 输出学生表
echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Age</th><th>Gender</th><th>Department</th></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["age"]. "</td><td>" . $row["gender"]. "</td><td>" . $row["department"]. "</td></tr>";
}
echo "</table>";
// 添加学生
if(isset($_POST["name"]) && isset($_POST["age"]) && isset($_POST["gender"]) && isset($_POST["department"])) {
$name = $_POST["name"];
$age = $_POST["age"];
$gender = $_POST["gender"];
$department = $_POST["department"];
$sql = "INSERT INTO student (name, age, gender, department) VALUES ('$name', $age, '$gender', '$department')";
if (mysqli_query($conn, $sql)) {
echo "New student record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
// 修改学生
if(isset($_POST["id"]) && isset($_POST["name"]) && isset($_POST["age"]) && isset($_POST["gender"]) && isset($_POST["department"])) {
$id = $_POST["id"];
$name = $_POST["name"];
$age = $_POST["age"];
$gender = $_POST["gender"];
$department = $_POST["department"];
$sql = "UPDATE student SET name='$name', age=$age, gender='$gender', department='$department' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Student record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
// 删除学生
if(isset($_POST["delete_id"])) {
$id = $_POST["delete_id"];
$sql = "DELETE FROM student WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Student record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
}
// 关闭数据库连接
mysqli_close($conn);
?>
```
这个例子中,我们使用了PHP语言和MySQL数据库来实现对学生表的CRUD操作。我们通过查询学生表来输出学生信息,通过表单提交来添加学生、修改学生和删除学生。你需要根据自己的需要进行修改和完善。同时,你还需要使用LayUI前端框架来美化学生表的界面。
阅读全文