<?php // 数据库连接参数 $host = "localhost"; $username = "root"; $password = "123456"; $dbname = "itcast"; // 连接数据库 $db = new PDO("mysql:host={$host};dbname={$dbname}", $username, $password); // 查询员工列表 $sql = "SELECT * FROM employees"; $stmt = $db->prepare($sql); $stmt->execute(); $employees = $stmt->fetchAll(PDO::FETCH_ASSOC); // 输出员工列表 echo "<table>"; echo "<thead><tr><th>ID</th><th>姓名</th><th>性别</th><th>入职日期</th><th>地址</th><th>电话</th></tr></thead>"; echo "<tbody>"; foreach ($employees as $employee) { echo "<tr><td>{$employee['id']} </td><td>{$employee['name']} </td><td>{$employee['gender']} </td><td>{$employee['hire_date']} </td><td>{$employee['address']}</td> <td>{$employee['phone']}</td></tr> "; } echo "</tbody>"; echo "</table>"; ?>添加一个更改按钮
时间: 2023-11-16 17:20:16 浏览: 135
PHP连接及操作PostgreSQL数据库的方法详解
好的,你可以在表头中添加一个“操作”列,然后在每个员工行中添加一个“更改”按钮。代码如下:
```
<?php
// 数据库连接参数
$host = "localhost";
$username = "root";
$password = "123456";
$dbname = "itcast";
// 连接数据库
$db = new PDO("mysql:host={$host};dbname={$dbname}", $username, $password);
// 查询员工列表
$sql = "SELECT * FROM employees";
$stmt = $db->prepare($sql);
$stmt->execute();
$employees = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 输出员工列表
echo "<table>";
echo "<thead><tr><th>ID</th><th>姓名</th><th>性别</th><th>入职日期</th><th>地址</th><th>电话</th><th>操作</th></tr></thead>";
echo "<tbody>";
foreach ($employees as $employee) {
echo "<tr><td>{$employee['id']}</td><td>{$employee['name']}</td><td>{$employee['gender']}</td><td>{$employee['hire_date']}</td><td>{$employee['address']}</td><td>{$employee['phone']}</td>";
echo "<td><button onclick='editEmployee({$employee['id']})'>更改</button></td></tr>";
}
echo "</tbody>";
echo "</table>";
?>
<script>
function editEmployee(id) {
// 弹出一个对话框来更改员工信息
// 这里留给你自己实现
}
</script>
```
这里我们添加了一个操作列,并在每行中添加了一个更改按钮,点击该按钮会触发 `editEmployee` 函数,你需要在该函数中实现弹出一个对话框来更改员工信息的功能。
阅读全文