基于kimi ai快速生成大作业(增删改查),无需数据库

参考

环境

软件/系统 版本 说明
windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
vs code 1.96.2
php 7.4.30 下载
firefox 133.0.3

正文

关键词:基于php语言,生成一个人员管理系统的增删改查,包含查询界面、删除行、编辑内容、新增内容; 支持php内置web客户端启动,数据存储使用文件存储;人员管理包含字段为:id、姓名、年龄、性别 。

步骤

步骤写在前面,防止看不到

  1. 下载 php,将 php 可执行文件加入系统变量。
  2. https://kimi.moonshot.cn 输入关键词,自动生成相关代码。(代码已放到文章尾部的模板,任选一个模板即可)
  3. 创建 数据存储文件格式 ,将ai生成的内容存入。
  4. 创建 index.php ,将代码写入(与 数据存储文件格式 文件在同一文件夹)。
  5. 进入项目所在文件夹,执行 php -S localhost:8000
  6. 浏览器访问http://localhost:8000即可。

模板

模板1

image

  1. data.json
    {"1":{"id":2,"name":"李四","age":30,"gender":"女"},"2":{"id":3,"name":"张三","age":"15","gender":"男"}}
    
  2. index.php
    <?php
    // 读取人员数据文件
    function readData() {
    	$data = file_get_contents('data.json');
    	return json_decode($data, true);
    }
    
    // 写入人员数据文件
    function writeData($data) {
    	$dataJson = json_encode($data, JSON_UNESCAPED_UNICODE);
    	file_put_contents('data.json', $dataJson);
    }
    
    // 获取下一个可用的id
    function getNextId($data) {
    	$ids = array_column($data, 'id');
    	return max($ids) + 1;
    }
    
    // 处理新增人员
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
    	$data = readData();
    	$newPerson = [
    		'id' => getNextId($data),
    		'name' => $_POST['name'],
    		'age' => $_POST['age'],
    		'gender' => $_POST['gender']
    	];
    	$data[] = $newPerson;
    	writeData($data);
    	header('Location: index.php');
    	exit();
    }
    
    // 处理删除人员
    if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
    	$data = readData();
    	$data = array_filter($data, function($person) {
    		return $person['id'] !== (int)$_GET['id'];
    	});
    	writeData($data);
    	header('Location: index.php');
    	exit();
    }
    
    // 处理编辑人员
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'edit') {
    	$data = readData();
    	foreach ($data as &$person) {
    		if ($person['id'] === (int)$_POST['id']) {
    			$person['name'] = $_POST['name'];
    			$person['age'] = $_POST['age'];
    			$person['gender'] = $_POST['gender'];
    			break;
    		}
    	}
    	writeData($data);
    	header('Location: index.php');
    	exit();
    }
    
    // 获取人员数据
    $data = readData();
    ?>
    
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
    	<meta charset="UTF-8">
    	<title>人员管理系统</title>
    	<style>
    		table, th, td {
    			border: 1px solid black;
    			border-collapse: collapse;
    			padding: 8px;
    			text-align: center;
    		}
    		th {
    			background-color: #f2f2f2;
    		}
    		.form-container {
    			margin-bottom: 20px;
    		}
    	</style>
    </head>
    <body>
    	<h1>人员管理系统</h1>
    
    	<!-- 新增人员表单 -->
    	<div class="form-container">
    		<h2>新增人员</h2>
    		<form method="post" action="index.php">
    			<input type="hidden" name="action" value="add">
    			<label for="name">姓名:</label>
    			<input type="text" id="name" name="name" required>
    			<label for="age">年龄:</label>
    			<input type="number" id="age" name="age" required>
    			<label for="gender">性别:</label>
    			<input type="text" id="gender" name="gender" required>
    			<button type="submit">新增</button>
    		</form>
    	</div>
    
    	<!-- 查询界面 -->
    	<h2>人员列表</h2>
    	<table>
    		<thead>
    			<tr>
    				<th>ID</th>
    				<th>姓名</th>
    				<th>年龄</th>
    				<th>性别</th>
    				<th>操作</th>
    			</tr>
    		</thead>
    		<tbody>
    			<?php foreach ($data as $person): ?>
    				<tr>
    					<td><?php echo $person['id']; ?></td>
    					<td><?php echo $person['name']; ?></td>
    					<td><?php echo $person['age']; ?></td>
    					<td><?php echo $person['gender']; ?></td>
    					<td>
    						<!-- 编辑按钮 -->
    						<button onclick="editPerson(<?php echo $person['id']; ?>, '<?php echo $person['name']; ?>', <?php echo $person['age']; ?>, '<?php echo $person['gender']; ?>')">编辑</button>
    						<!-- 删除按钮 -->
    						<a href="index.php?action=delete&id=<?php echo $person['id']; ?>">删除</a>
    					</td>
    				</tr>
    			<?php endforeach; ?>
    		</tbody>
    	</table>
    
    	<!-- 编辑人员表单(隐藏) -->
    	<div id="edit-form" style="display: none;">
    		<h2>编辑人员</h2>
    		<form method="post" action="index.php">
    			<input type="hidden" id="edit-id" name="id">
    			<input type="hidden" name="action" value="edit">
    			<label for="edit-name">姓名:</label>
    			<input type="text" id="edit-name" name="name" required>
    			<label for="edit-age">年龄:</label>
    			<input type="number" id="edit-age" name="age" required>
    			<label for="edit-gender">性别:</label>
    			<input type="text" id="edit-gender" name="gender" required>
    			<button type="submit">保存</button>
    			<button type="button" onclick="cancelEdit()">取消</button>
    		</form>
    	</div>
    
    	<script>
    		// 编辑人员函数
    		function editPerson(id, name, age, gender) {
    			document.getElementById('edit-id').value = id;
    			document.getElementById('edit-name').value = name;
    			document.getElementById('edit-age').value = age;
    			document.getElementById('edit-gender').value = gender;
    			document.getElementById('edit-form').style.display = 'block';
    		}
    
    		// 取消编辑函数
    		function cancelEdit() {
    			document.getElementById('edit-form').style.display = 'none';
    		}
    	</script>
    </body>
    </html>
    

模板2

image

  1. data.txt
    id,姓名,年龄,性别
    1,张三,22,男
    
  2. index.php
    <?php
    // 读取数据文件
    function readData() {
    	$data = [];
    	$file = fopen('data.txt', 'r');
    	if ($file) {
    		fgetcsv($file); // 跳过标题行
    		while (($row = fgetcsv($file)) !== FALSE) {
    			$data[] = $row;
    		}
    		fclose($file);
    	}
    	return $data;
    }
    
    // 写入数据文件
    function writeData($data) {
    	$file = fopen('data.txt', 'w');
    	if ($file) {
    		fputcsv($file, ['id', '姓名', '年龄', '性别']); // 写入标题行
    		foreach ($data as $row) {
    			fputcsv($file, $row);
    		}
    		fclose($file);
    	}
    }
    
    // 删除记录
    if (isset($_GET['delete'])) {
    	$id = $_GET['delete'];
    	$data = readData();
    	$newData = array_filter($data, function($row) use ($id) {
    		return $row[0] != $id;
    	});
    	writeData($newData);
    	header('Location: index.php');
    	exit;
    }
    
    // 编辑记录
    if (isset($_GET['edit'])) {
    	$id = $_GET['edit'];
    	$data = readData();
    	foreach ($data as $row) {
    		if ($row[0] == $id) {
    			$editRow = $row;
    			break;
    		}
    	}
    }
    
    // 更新记录
    if (isset($_POST['update'])) {
    	$id = $_POST['id'];
    	$name = $_POST['name'];
    	$age = $_POST['age'];
    	$gender = $_POST['gender'];
    	$data = readData();
    	$newData = [];
    	foreach ($data as $row) {
    		if ($row[0] == $id) {
    			$newData[] = [$id, $name, $age, $gender];
    		} else {
    			$newData[] = $row;
    		}
    	}
    	writeData($newData);
    	header('Location: index.php');
    	exit;
    }
    
    // 添加记录
    if (isset($_POST['add'])) {
    	$name = $_POST['name'];
    	$age = $_POST['age'];
    	$gender = $_POST['gender'];
    	$data = readData();
    	$id = count($data) + 1;
    	$data[] = [$id, $name, $age, $gender];
    	writeData($data);
    	header('Location: index.php');
    	exit;
    }
    
    $data = readData();
    ?>
    
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
    	<meta charset="UTF-8">
    	<title>人员管理系统</title>
    </head>
    <body>
    	<h1>人员管理系统</h1>
    	<h2>查询界面</h2>
    	<table border="1">
    		<tr>
    			<th>ID</th>
    			<th>姓名</th>
    			<th>年龄</th>
    			<th>性别</th>
    			<th>操作</th>
    		</tr>
    		<?php foreach ($data as $row): ?>
    			<tr>
    				<td><?php echo $row[0]; ?></td>
    				<td><?php echo $row[1]; ?></td>
    				<td><?php echo $row[2]; ?></td>
    				<td><?php echo $row[3]; ?></td>
    				<td>
    					<a href="index.php?edit=<?php echo $row[0]; ?>">编辑</a> |
    					<a href="index.php?delete=<?php echo $row[0]; ?>">删除</a>
    				</td>
    			</tr>
    		<?php endforeach; ?>
    	</table>
    
    	<h2>新增内容</h2>
    	<form method="post">
    		<label for="name">姓名:</label>
    		<input type="text" id="name" name="name" required><br><br>
    		<label for="age">年龄:</label>
    		<input type="number" id="age" name="age" required><br><br>
    		<label for="gender">性别:</label>
    		<input type="text" id="gender" name="gender" required><br><br>
    		<input type="submit" name="add" value="添加">
    	</form>
    
    	<?php if (isset($editRow)): ?>
    		<h2>编辑内容</h2>
    		<form method="post">
    			<input type="hidden" name="id" value="<?php echo $editRow[0]; ?>">
    			<label for="name">姓名:</label>
    			<input type="text" id="name" name="name" value="<?php echo $editRow[1]; ?>" required><br><br>
    			<label for="age">年龄:</label>
    			<input type="number" id="age" name="age" value="<?php echo $editRow[2]; ?>" required><br><br>
    			<label for="gender">性别:</label>
    			<input type="text" id="gender" name="gender" value="<?php echo $editRow[3]; ?>" required><br><br>
    			<input type="submit" name="update" value="更新">
    		</form>
    	<?php endif; ?>
    </body>
    </html>
    
posted @ 2025-01-08 20:17  夏秋初  阅读(6)  评论(0编辑  收藏  举报