头部文件header.php
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="lib/dist/css/bootstrap.css"> <script src="lib/jquery-3.2.1.js"></script> <script src="lib/dist/js/bootstrap.js"></script> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title><?php echo isset($pageTitle)?$pageTitle :'注册'?></title> <!-- <--! 标题通过一个变量($pageTitle)进行判断--> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12"> <nav class="navbar navbar-inverse"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">我的博客</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <!-- <li><a href="#">我的记录 <span>(current)</span></a></li>--> <!-- <li><a href="#">经验</a></li>--> <!-- <li><a href="#">笔记</a></li>--> </ul> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> </div> </div> <!--头部--> 底部文件footer.php <div class="row"> <div class="col-md-12"> <nav class="navbar navbar-inverse"> </nav> </div> </div> <!--底部--> </div> </body> </html> 注册页面login.php <?php include 'public/header.php'?> <div class="container"> <div class="row"> <div class="col-md-12"> <h3 class="text-center">用户注册</h3> <form class="form-horizontal" action="register.php" method="post"> <div class="form-group"> <label for="name">用户名</label> <input type="text" class="form-control" id="exampleInputEmail1" placeholder="用户名" name="name" value="<?php echo isset($_POST['name'])?$_POST['name']:''?>"> </div> <div class="form-group"> <label for="email">邮箱</label> <input type="email" class="form-control" id="email" placeholder="Email" name="email" value="<?php echo isset($_POST['email'])?$_POST['emai']:''?>"> </div> <div class="form-group"> <label for="password1">密码</label> <input type="password" class="form-control" id="password1" placeholder="Password" name="password1" value="<?php echo isset($_POST['password1'])?$_POST['password1']:''?>"> </div> <div class="form-group"> <label for="password2">确认密码</label> <input type="password" class="form-control" id="password2" placeholder="Password" name="password2" value="<?php echo isset($_POST['password2'])?$_POST['password2']:''?>"> </div> <button type="submit" class="btn btn-primary btn-block">Submit</button> </form> </div> </div> </div> <?php include 'public/footer.php'?> 注册处理文件register.php //注册处理操作 <?php /* * 1.检测注册表单提交过来的信息 * 2.将用户信息插入到表中 * 3.对错误信息进行处理 */ $pageTitle = '注册处理'; include 'public/header.php'; //对请求进行处理 if ($_SERVER['REQUEST_METHOD']) { //创建一个错误信息的数组 $error = ''; //检测用户名 if (empty($_POST['name'])) { $error[] = '没有输入用户名'; }else { $name = trim($_POST['name']);//trim()可以删除字符串前后空格 } //检测邮箱 if (empty($_POST['email'])) { $error[] = '没有输入邮箱'; }else { $email = trim($_POST['email']);//trim()可以删除字符串前后空格 } //检测密码:密码要进行两次输入,需进行比较 if (empty($_POST['password1'])) { $error[] = '没有输入密码'; }else { if (empty($_POST['password2'])) { $error[] = '没有输入校验密码'; }else { if ($_POST['password1'] == $_POST['password2']) { $password = trim($_POST['password1']); } else { $error[] = '两次密码输入不一致'; } } } //检测如果出错,$error数组一定是有值的 if (empty($error)) { //为空,正确。执行插入操作 //连接数据库 require 'public/connect.php'; //创建sql $sql = "INSERT user1 SET `name`='{$name}', `email`='{$email}',`password`=sha1('{$password}')"; //执行插入操作 if (mysqli_query($db,$sql)) { echo '<h3>成功添加了'.mysqli_affected_rows($db).'条记录,新增主键ID'.mysqli_insert_id($db).'</h3>';//mysqli_affected_rows()返回当前受影响的记录条数 更新 插入 删除 都可以用他// mysqli_insert_id()可以返回当前新增主键的ID } else { echo '<h3>添加失败:'.mysqli_error($db).'</h3>'; } } else { //不为空出错,打印出错信息 foreach ($error as $message) { echo '<h3>'.$message.'</h3>'; } } } include 'public/footer.php'; 获取数据库用户文件 view.php //获取用户列表 <?php $pageTitle = '用户信息列表'; include 'public/header.php'; //获取到user1 里面的所有记录 //1. 连接数据库 require 'public/connect.php'; //查询操作 $sql = "SELECT * FROM user1";//获取字段信息user1 $result = mysqli_query($db,$sql);// 查询$sql 返回一个结果集 $row = []; //判断 当前结果集是否存在 if ($result && mysqli_num_rows($result) > 0) {//如果当前结果集存在并且有数据 while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {//将记录解析为数组 $row一维数组 $rows[] = $row; } }else { echo '数据表为空'.mysqli_error($db); } //释放结果集 mysqli_free_result($result); //关闭连接 mysqli_close($db); ?> <!--生成一个表格,把用户数据显示出来--> <div class="container"> <div class="row"> <div class="col-md-12"> <h3 class="text-center">用户信息表</h3> <table class="table table-bordered table-responsive text-center"> <tr> <td>ID</td><td>姓名</td><td>邮箱</td><td>操作</td> </tr> <!-- php循环的替代语法,干掉大括号--> <?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="delete.php?id=<?php echo $row['id']?>" role="button">删除</a> </td> </tr> <?php endforeach; ?> </table> </div> </div> </div> <?php include'public/footer.php';?>
对用户列表项的用户进行编辑处理文件edit.php
//编辑用户信息处理 <?php $pageTitle = '更新数据'; include 'public/header.php'; $id = $_GET['id'];//获取一下编辑的id require 'public/connect.php';//连接数据库 $sql = "SELECT * FROM user1 WHERE id =".$id;//查询所有数据user1 $res = mysqli_query($db,$sql);//返回结果集 if ($res && mysqli_num_rows($res)>0) { $row = mysqli_fetch_array($res,MYSQLI_ASSOC); } ?> <div class="container"> <div class="row"> <div class="col-md-12"> <h3 class="text-center">编辑信息</h3> <form class="form-horizontal" action="do_edit.php?id=<?php echo $row['id'] ?>" method="post"> <div class="form-group"> <label for="name">用户名</label> <input type="text" class="form-control" id="exampleInputEmail1" placeholder="用户名" name="name" value="<?php echo $row['name']?>"readonly> </div> <div class="form-group"> <label for="email">邮箱</label> <input type="email" class="form-control" id="email" placeholder="Email" name="email" value="<?php echo $row['email'] ?>"> </div> <button type="submit" class="btn btn-primary btn-block">保存</button> </form> </div> </div> </div> <?php include 'public/footer.php'?>
对编辑用户信息处理更新与验证do_edit.php
//编辑用户信息处理验证更新 <?php $pageTitle = '更新'; include 'public/header.php'; //对请求进行处理 if ($_SERVER['REQUEST_METHOD']) { //创建一个错误信息的数组 $error = ''; //用户只读不需要检查 //检测邮箱 if (empty($_POST['email'])) { $error[] = '没有输入邮箱'; }else { $email = trim($_POST['email']);//trim()可以删除字符串前后空格 } //检测如果出错,$error数组一定是有值的 if (empty($error)) { //为空,正确。执行插入操作 //连接数据库 require 'public/connect.php'; //创建sql $sql = "UPDATE user1 SET `email`='{$email}' WHERE id={$_GET['id']}"; //执行插入操作 if (mysqli_query($db,$sql)) { echo '<h3>成功更新了'.mysqli_affected_rows($db).'条记录</h3>';//mysqli_affected_rows()返回当前受影响的记录条数 更新 插入 删除 都可以用他// mysqli_insert_id()可以返回当前新增主键的ID } else { echo '<h3>更新失败:'.mysqli_error($db).'</h3>'; } } else { //不为空出错,打印出错信息 foreach ($error as $message) { echo '<h3>'.$message.'</h3>'; } } } include 'public/footer.php';
对用户进行删除操作delete.php
//删除用户信息操作处理 <?php require 'public/connect.php'; $sql = "DELETE FROM user1 WHERE id = ".$_GET['id']; if (mysqli_query($db,$sql)) { echo '<script> var sure=confirm( \'确认你的操作吗 \'); if (1==sure){alert( \'你选择了是 \')} else {alert( \'你选择了否 \');}</script>';//mysqli_affected_rows()返回删除几条值 } else { echo '<script>alert("删除失败'.mysqli_affected_rows($db).'")</script>'; } mysqli_close($db); header('location: '.$_SERVER['HTTP_REFERER']);//返回当前页面 //<?php echo "<script> var sure=confirm( '确认你的操作吗 '); if (1==sure){alert( '你选择了是 ')} else {alert( '你选择了否 ');}</script>";?>