用户信息表:
<?php //连接数据库 $pageTitle = '用户信息列表'; require 'public/connect.php'; //查询用户信息表user $sql = "SELECT * FROM user"; //创建查询语句 $result = mysqli_query($db, $sql); //执行查询 $rows = []; //创建查询结果容器,初始为空数组,该数组最终会成为一个二维数组,与数组表对应 if ($result && mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) //print_r($row); //查看查询到的数据 $rows[] = $row; //将每一次查询到的结果放在数组$rows[]中,这是最终返回的结果 } else { echo '<h3>数据表为空,没有数据~~</h3>'; } //释放结果集 mysqli_free_result($result); //关闭连接 mysqli_close($db); ?> <?php include 'public/header.php'?> <!--创建表格用来展示用户数据--> <div class="container"> <div class="row"> <div class="com-md-12"> <h3>用户信息表</h3> <table class="table table-bordered text-center"> <tr class="h5 bg-info"> <td>ID</td> <td>姓名</td> <td>邮箱</td> <td>操作</td> </tr> <?php foreach ($rows as $row) : ?> <tr> <td> <?php echo $row['id']?> </td> <td> <?php echo $row['name']?> </td> <td> <?php echo $row['email']?> </td> <td> <a class="btn btn-primary" href="edit.php?id=<?php echo $row['id']?>" role="button">编辑</a> <a class="btn btn-danger" href="del.php?id=<?php echo $row['id']?>" role="button">删除</a> </td> </tr> <?php endforeach;?> </table> </div> </div> </div> <!--公用底部--> <?php include 'public/footer.php'?>
删除功能实现:
<?php require 'public/connect.php'; //创建sql删除代码 $sql = "DELETE FROM user WHERE id=".$_GET['id']; //判处执行操作 if (mysqli_query($db, $sql)){ echo '<script>alert("成功删了'.mysqli_affected_rows($db).'条记录")</script>'; } else { echo '<script>alert("删除失败'.mysqli_error($db).'")</script>'; } //关闭数据库 mysqli_close($db);