博客列表 >Day26-2017/12/29(数据库连接操作增删改查操作)

Day26-2017/12/29(数据库连接操作增删改查操作)

SmallKing的博客
SmallKing的博客原创
2018年01月02日 03:28:38651浏览

内容:

数据库连接操作增删改查操作

将12月29日课堂实战案例全部写一遍,并增加删除确认与取消功能。

public/header.php  头文件

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.css">
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <title><?php echo isset($pageTitle)?$pageTitle:'注册' ?></title>
</head>
<body>
<div class='container"'>
    <div class="row">
        <nav class="navbar navbar-default">
            <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="#")><img src="https://www.sanwen8.cn/logo.gif" alt="" style='height: 30px'></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 class="active"><a href="#">首页</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">类型<span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">散文</a></li>
                                <li><a href="#">诗歌</a></li>
                                <li><a href="#">小说</a></li>
                                <li><a href="#">杂文</a></li>
                                <li><a href="#">随笔</a></li>
                            </ul>
                        </li>
                    </ul>
                    <form class="navbar-form navbar-left">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="搜索">
                        </div>
                        <button type="submit" class="btn btn-default">搜索</button>
                    </form>
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#">注册</a></li>
                        <li><a href="#">登入</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">其他<span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Action</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something else here</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="#">Separated link</a></li>
                            </ul>
                        </li>
                    </ul>
                </div><!-- /.navbar-collapse -->
 </div><!-- /.container-fluid -->
 </nav>
    </div>
</div> <!--//头-->

public/footer.php 页脚文件

 <div<div class="container-fluid"> class="row">       
        <div class="col-sm-12">
            <nav class="navbar navbar-default text-center" style="height: 30px">
                <p style="line-height: 25px">免责声明:本站所发表的作文版权归作者所有,若转载或者抄袭他人作品,带来的一切后果与本站无关。若您发现本站存在您非授权的原创作品,请第一时间联系本站删除!本站享有代替本站作者维权的权力!CopyRight ©2007-2017 散文吧网站鲁ICP备09059645号</p>
            </nav>

        </div>

    </div>
</div>   <!--底部内容-->
</body>
</html>

public/config.php 配置文件

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

public/connect.php 数据库连接文件

<?php
//引人配置常量
require 'config.php';
//连接数据库
$db=mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(mysqli_error($db)) {
    echo '连接失败'.mysqli_error($db);
}
else{
    mysqli_set_charset($db,DB_CHAR);
}

注册表单 login.php

<?php include 'public/header.php';?>
<!--设定表单-->
<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <form class="form-horizontal" method="post" action="register.php">
                <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="email" 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" name='password1' value="<?php echo  isset($_POST['password1'])?$_POST['password1']:'' ?>" placeholder="密码">
                    </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"  name='password2'  value="<?php echo  isset($_POST['password2'])?$_POST['password2']:'' ?>" placeholder="确认密码">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default btn-primary btn-block">注册</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

</div>
<!--引入页脚-->
<?php include 'public/footer.php';?>

注册.png

增(数据) register.php

<?php
$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)){
        //连接数据库
        require 'public/connect.php';
        //插入数据并判断是否成功
        $sql="INSERT userinfo SET `name`='{$name}',`email`='{$email}',`password`=sha1('{$password}') ";
        if(mysqli_query($db,$sql)) {
            echo '<h3 style="color:green">成功添加'.mysqli_affected_rows($db).'条数据,新数据ID='.mysqli_insert_id($db).'</h3>';
        } else echo "<h3 style='color: red;'>添加失败".mysqli_error($db)."</h3>";


    }else {
        //用户输入信息不正确,返回错误信息
        foreach($error as $value)
            echo "<h3 class='text-center' style='color: red;'>".$value."</h3>";
    }

}
?>
<?php include "public/footer.php";?>

增.png

查(数据) veiw.php

<?php
$pageTitle='用户信息表';
include "public/header.php";

//连接数据库
require 'public/connect.php';
//查询用户信息
 $sql="SELECT * FROM `userinfo`";
 $res=mysqli_query($db,$sql);
 $rows=[];
 //判断查询是否成功
if($res&&mysqli_num_rows($res)>0) {
    //数组化数据
    while ($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
        $rows[]=$row;
    }
 }else
     echo '<h3>数据表为空,没有数据~~</h3>';
?>
<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <table class=" text-center table table-bordered" >
                <tr class="bg-primary">
                    <td>ID</td>
                    <td>姓名</td>
                    <td>邮箱</td>
                    <td>操作</td>
                </tr>
                <?php  foreach($rows as $row) : ?>
                <tr>
                    <td style="vertical-align:middle"><?php echo $row['id'] ?></td>
                    <td style="vertical-align:middle"><?php echo $row['name'] ?></td>
                    <td style="vertical-align:middle"><?php echo $row['email'] ?></td>
                    <td style="vertical-align:middle"> <a href="edit.php?id=<?php echo $row['id'] ?>" class="btn btn-primary" role="button" >编辑</a>
                        <a href="choose.php?id=<?php echo $row['id'] ?>"  class="btn btn-danger" role="button" >删除</a></td>
                </tr>
                <?php  endforeach;?>
            </table>
        </div>
    </div>
</div>



<?php include "public/footer.php";?>

查.png

改(数据)edit.php

<?php include 'public/header.php';
$pageTital='编辑信息';
//连接数据库
require 'public/connect.php';
//查询数据并判断是否成功
$sql="SELECT * FROM `userinfo` WHERE `ID`=".$_GET['id'] ;
$res=mysqli_query($db,$sql);
if($res&&mysqli_num_rows($res)>0){
    $row=mysqli_fetch_array($res,MYSQLI_ASSOC);
}
//    print_r($row);
?>
    <!--设定表单-->
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <form class="form-horizontal" method="post" action="save.php?id=<?php echo $_GET['id'];?>">
                    <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($row['name'])?$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="邮箱" name='email' value="<?php echo isset($row['email'])?$row['email']:''?>">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default btn-primary btn-block">保存</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

    </div>
    <!--引入页脚-->
<?php include 'public/footer.php';?>

改.png

修改完后保存 save.php

<?php
$pageTitle='保存信息';
include "public/header.php";
//连接数据库

if ($_SERVER['REQUEST_METHOD']=='POST'){
    $error=[];
    if (empty($_POST['email'])){
        $error[]='请输入邮箱';
    }else{
        require 'public/connect.php';
        //更新数据并判断是否成功
        $sql="UPDATE userinfo SET `email`='{$_POST['email']}' WHERE `ID`='{$_GET['id']}'" ;
        if(mysqli_query($db,$sql)) {
            echo '<h3 style="color:green">成功更新'.mysqli_affected_rows($db).'条数据,新数据ID='.mysqli_insert_id($db).'</h3>';
        } else
            echo "<h3 style='color: red;'>更新失败".mysqli_error($db)."</h3>";

    }

}
?>
    <a href="view.php" class="btn btn-primary btn-lg active" role="button">确定并返回</a>


<?php include "public/footer.php";?>

改2.png

删除数据 

  1. 判断是否要删除 choose.php

<?php include 'public/header.php';?>
<script>
    var r=confirm('数据删除后不可恢复,确定删除吗?');
  if (r==true) {
      $.ajax({
          type:'POST',
          url:'delete.php',
          data:{id:<?php echo $_GET['id']?>},
          datastyle:'html',
          success:function (data,status) {
               alert(data)

          }
      })
  }else alert('取消删除')
</script>


    <a href="view.php" class="btn btn-primary btn-lg active" role="button">确定并返回</a>
<?php include "public/footer.php";?>

删.png

2.删除数据delete.php

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

$sql = "DELETE FROM userinfo WHERE id=".$_POST['id'];

if (mysqli_query($db, $sql)){
    echo "成功删了".mysqli_affected_rows($db)."条记录";
} else {
    echo '删除失败'.mysqli_error($db);
}

mysqli_close($db);
?>

删2.png

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议