Blogger Information
Blog 61
fans 0
comment 0
visits 62889
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MySQL数据库面向过程编程实战的增删改查(CURD)
Pengsir
Original
630 people have browsed it

配置文件都是放在public这个文件夹下:

config

<?php
//连接参数
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','root');
define('DB_NAME','demo');
define('DB_CHAR','utf8');

connect

<?php
require 'config.php';
//1.数据库的连接返回的是一个对象
$db=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
//echo is_bool($db)?'true':'false';//失败的时候返回的是布尔false
//echo is_object($db)?'true':'false';//判断$db是不是一个对象,结果显示是一个对象

//2.判断是否连接成功
if(mysqli_connect_errno($db)){
    echo '连接失败'.mysqli_connect_error($db);
}
//else{
//    echo '连接成功';
//}
//3.选择默认数据库
mysqli_select_db($db,DB_NAME);
//4.设置默认客户端的字符集
mysqli_set_charset($db,DB_CHAR);

header

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <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">
    <link rel="stylesheet" href="../../lib/dist/css/bootstrap.css">
    <script src="../../lib/jquery-3.2.1.min.js"></script>
    <script src="../../lib/dist/js/bootstrap.js"></script>
    <title><?php echo  isset($pageTitle)?$pageTitle:'注册'?></title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="nav">
                <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>
                        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                            <ul class="nav navbar-nav">
                                <li class="active"><a href="#">首页 <span class="sr-only">(current)</span></a></li>
                                <li><a href="#">学习心得</a></li>
                                <li><a href="#">编程技巧</a></li>
                                <li><a href="#">源码分享</a></li>
                                <li><a href="#">资料下载</a></li>
                            </ul>
                        </div><!-- /.navbar-collapse -->
 </div><!-- /.container-fluid -->
 </nav>
            </div>
        </div>
    </div>    <!--头部导航-->

footer

<div class="row">
    <div class="col-md-12">
        <nav class="navbar navbar-inverse">
            <p class="text-center"><a href="">PHP中文网版权所有</a></p>
        </nav>
    </div>
</div>  <!--底部-->

</div>
</div>

</body>
</html>

登录login

<?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" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                <input type="text" class="form-control" id="name" placeholder="请输入用户名" name="name" value="<?php echo isset($_POST['name'])?$_POST['name']:'' ?>">
            </div>
            </div>

            <div class="form-group">
                <label for="email" class="col-sm-2 control-label">邮箱</label>
                <div class="col-sm-10">
                <input type="text" class="form-control" id="email" placeholder="请输入邮箱" name="email" value="<?php echo isset($_POST['email'])?$_POST['email']:''?>">
            </div>
            </div>

            <div class="form-group">
                <label for="password1" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                <input type="password" class="form-control" id="password1" placeholder="请输入密码" name="password1" value="<?php echo isset($_POST['password1'])?$_POST['password1']:'' ?>">
            </div>
            </div>

            <div class="form-group">
                <label for="password2" class="col-sm-2 control-label">密码确认</label>
                <div class="col-sm-10">
                <input type="password" class="form-control" id="password2" placeholder="密码确认" name="password2" value="<?php echo isset($_POST['password2'])?$_POST['password2']:'' ?>">
            </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10 text-center">
                    <button type="submit" class="btn btn-primary  btn-block">注册</button>
            </div>
            </div>
        </form>
    </div>
</div>

</div>
<?php include 'public/footer.php';?>

注册register

<?php
/*
 * 1.检测注册表单提交过来的信息
 * 2.将用户数据插入到表中
 * 3.对错误进行处理
 */
$pageTitle='注册处理';//设置当前的页面标题
include 'public/header.php';//导入公共头部

//对用户的注册请求进行处理
if($_SERVER['REQUEST_METHOD']=='POST'){
    $error=[];//创建一个错误数组

    //检查用户名
    if(empty($_POST['name'])){
        $error[]='用户名不能为空';
    }else{
        $name=trim($_POST['name']);
    }

    //检查用邮箱
    if(empty($_POST['email'])){
        $error[]='邮箱不能为空';
    }else{
        $email=trim($_POST['email']);
    }

    //检测密码:因为密码需要输入二遍,所以要做相等校验
    if(empty($_POST['password1'])){//第一行密码如果为空
        $error[]='没有输入密码,请检查';
    }else{ //再检查第二行的校验密码
        if(empty($_POST['password2'])){
            $error[]="没有输入校验密码,请检查";
        }else{//两个密码进行比较
            if($_POST['password1']==$_POST['password2']){//两者相等,操作正确
                $password=trim($_POST['password1']);
            }else{//否则就要重新输入
                $error[]='二次输入密码不一致,请检查';
            }
        }
    }

//将用户数据添加到数据库
    if(empty($error)){//如果错误数组为空,将数据添加到数据库
//        1.连接数据库
        require 'public/connect.php';
//        2.写SQL语句
        $sql="INSERT user1 SET `name`='{$name}',`email`='{$email}',`password`=sha1('{$password}')";
//        3.执行SQL语句,向数据库插入数据
        if(mysqli_query($db,$sql)){
//          mysqli_affected_rows($db) 返回前一次MYSQL操作所受影响的记录行数
            echo '<h3 style="color: #c1e2b3">成功添加了'.mysqli_affected_rows($db).'条记录,新增主键id是:'.mysqli_insert_id($db).'</h3>';
        }else{
            echo '<h3 style="color: #888888">添加失败:'.mysqli_error($db).'</h3>';
        }
        //    关闭连接
        mysqli_close($db);
    }else{
//        打印错误信息
       foreach ($error as $message){
           echo '<h3>'.$message.'</h3>>';
       }
    }
}
include 'public/footer.php';

用户信息表列表view

<?php
$pageTetle='用户信息列表';
//连接数据库
require 'public/connect.php';
include 'public/header.php';
//1.第一大步:从数据库里取数据
//查询数据库里的数据分二步:第一步写sql语句  第二步:执行sql语句
$sql="SELECT * FROM user1";//sql语句是字符串

$result=mysqli_query($db,$sql);//执行查询

$rows=[];//创建一个结果集容器

if($result && mysqli_num_rows($result) >0){//结果与返回结果集行的数目有数据时
    while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC))//从所有行中取一行保存在row里
    $rows[]=$row;//把取出的一行放到结果集容器里
}else{
    echo '<h3>数据表没有数据</h3>';
}
?>
<!---->
<!--2.第二步建一个表格把取的数据放到一个表格中-->
<!--用bootStrap来建一个表格-->

        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <h3 style="color: red" class="text-center" >用户信息表</h3>
                    <table class="table  text-center" border="1" cellpadding="3" cellspacing="0" >
                        <tr>
                            <td>ID</td><td>姓名</td><td>邮箱</td><td>操作</td>
                        </tr>

<!--                        把数据从rows[]中取出来放到表格里-->

<!--                        1.下面循环输出用户信息,用foreach()的替代语法,请尽快熟悉这种写法,以后写模板就靠这个玩意了-->
<!--                        2. 替代语法的目标就是一个: 干掉php代码中的大括号,因为html与php混写的时候,不断的切换会有很多大括号-->
<!--                        3. 过多的大括号,即不利于代码整洁,也容易出错,不利于代码维护-->

                        <?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'?>

更新处理do_edit

<?php
/**
 * 直接复用注册处理程序
 * 1.检测编辑表单提交过来的信息
 * 2.对用户的提交信息进行检查并更新数据表
 * 3.对错误进行处理
 */

$pageTitle='更新处理';//设置当前的页面标题
include 'public/header.php';//导入公共头部

//对用户的注册请求进行处理
if($_SERVER['REQUEST_METHOD']=='POST'){
    $error=[];//创建一个错误数组

//    用户名是只读的,所有不需要检查

    //检查邮箱
    if(empty($_POST['email'])){
        $error[]='邮箱不能为空';
    }else{
        $email=trim($_POST['email']);
    }

//    密码不需要检查

//将用户数据添加到数据库
    if(empty($error)){//如果错误数组为空,将数据添加到数据库
//        1.连接数据库
        require 'public/connect.php';
//        2.写SQL语句
        $sql = "UPDATE user1 SET  `email`='{$email}' WHERE `id`={$_GET['id']}";
//        3.执行SQL语句,向数据库插入数据
        if(mysqli_query($db,$sql)){
//          mysqli_affected_rows($db) 返回前一次MYSQL操作所受影响的记录行数
            echo '<h3 style="color: #c1e2b3">成功添加了'.mysqli_affected_rows($db).'条记录</h3>';
        }else{
            echo '<h3 style="color: #888888">添加失败:'.mysqli_error($db).'</h3>';
        }
        //    关闭连接
        mysqli_close($db);
    }else{
//        打印错误信息
        foreach ($error as $message){
            echo '<h3>'.$message.'</h3>>';
        }
    }
}
include 'public/footer.php';

用户信息编辑的表edit

<?php include 'public/header.php';
$id = $_GET['id'];  //获取到要编辑的记录的id

require 'public/connect.php';  //连接数据库
//准备查询语句
$sql = "SELECT * FROM `user1` WHERE `id` = ".$id;
$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>
                <!--                修改处理的php脚本名称-->
                <form class="form-horizontal" action="do_edit.php?id=<?php echo $row['id'] ?>" method="post">
                    <div class="form-group">
                        <!--                        用户名和密码是不允许修改的,因为这是登陆的重要信息,仅允许修改邮箱-->
                        <label for="name" class="col-sm-2 control-label">用户名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="name" placeholder="userName" name="name" value="<?php echo $row['name'] ?>" readonly>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="email" placeholder="Email" name="email" value="<?php echo $row['email'] ?>">
                        </div>
                    </div>


                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10  text-center">
                            <button type="submit" class="btn btn-primary  btn-block">保存</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php include 'public/footer.php' ?>

删除

<?php
require 'public/connect.php';

$sql = "DELETE FROM user1 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);

附两张图

用户信息表.png用户注册表.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post