国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

mysqli小案例

Original 2019-01-12 17:39:59 284
abstract:本章通過學(xué)習(xí)mysqli的操作,封裝了mysqli的公共方法,并通過方法實現(xiàn)增刪改查,代碼如下:mysqli.php<?php     //數(shù)據(jù)庫訪問鏈接     $db = array(         

本章通過學(xué)習(xí)mysqli的操作,封裝了mysqli的公共方法,并通過方法實現(xiàn)增刪改查,代碼如下:

mysqli.php

<?php

    //數(shù)據(jù)庫訪問鏈接
    $db = array(
        'charset' => 'utf8',
        'port' => 3306,
        'type' => 'mysql',
        'host' => '127.0.0.1',
        'user' => 'root',
        'pass' => 'root',
        'name' => 'test'
    );
    //調(diào)用的函數(shù)方法
    $eidtName=isset($_GET["edit"])?$_GET["edit"]:"";
    if($eidtName=="")
    {
        return;
    }
    //獲取提交過來的參數(shù)
    $arr=is_array($_POST)?$_POST:[];

    if(count($arr)>0)
    {
        //調(diào)用具體的方法并返回
        if(function_exists($eidtName))
        {
            return $eidtName($arr);
        }

    }


   //連接方法
   function connect($db){
      # 連接數(shù)據(jù)庫
      $conn = @mysqli_connect($db['host'],$db['user'],$db['pass'],$db['name'],'3306');
      if(!$conn){
         # 連接錯誤,拋出異常
         exit('數(shù)據(jù)庫報錯: '.mysqli_connect_error());
      }
      return $conn;
   }
   //統(tǒng)計數(shù)量
   function count_number($db,$table,$where){
      $sql = "SELECT COUNT(*) AS count_number FROM ".$table." WHERE ".$where;
      $return = mysqli_query($db,$sql);
      $return = mysqli_fetch_assoc($return);
      # 關(guān)閉數(shù)據(jù)庫
      mysqli_close($db);
      return $return['count_number'];
   }
   //查詢指定條件的單個數(shù)據(jù)
   function find_one($db,$table,$filed,$where){
      $sql = "SELECT ".$filed." FROM ".$table.' WHERE '.$where.' LIMIT 1';
      $return = mysqli_query($db,$sql);
      $return = mysqli_fetch_assoc($return);
      # 關(guān)閉數(shù)據(jù)庫
      mysqli_close($db);
      return $return;
   }
   //根據(jù)排序類型查詢單條數(shù)據(jù)
   function find($db,$table,$where,$order){
      $sql = "SELECT * FROM ".$table.' WHERE '.$where;
      if($order){
         $sql .= ' ORDER BY '.$order;
      }
      $sql .= 'LIMIT 1';
      $return = mysqli_query($db,$sql);
      $return = mysqli_fetch_assoc($return);
      # 關(guān)閉數(shù)據(jù)庫
      mysqli_close($db);
      return $return;
   }
   //查詢所有數(shù)據(jù)
   function select($db,$table,$where,$order,$limit){
      $sql = "SELECT * FROM ".$table.' WHERE '.$where;
      if($order){
         $sql .= ' ORDER BY '.$order;
      }
      if($limit){
         $sql .= ' LIMIT '.$limit;
      }
      $return = mysqli_query($db,$sql);
      if($return){
         while ( $row = mysqli_fetch_assoc($return)) {
            $rows[] = $row;
         }
         mysqli_free_result($return);
      }
      # 關(guān)閉數(shù)據(jù)庫
      mysqli_close($db);
      return $rows;
   }

   //獲取演員
   function getYanYuan($uid)
    {
        global $db;
        $con=connect($db);
        $res=find_one($con,'user','`name`,`phone`,`weight`,`height`','`uid`='.$uid['uid']);

        echo json_encode(array('info'=>$res));

    }

   //插入演員
   function insertYanYuan($arr)
    {
        global  $db;
        $res=false;
        $sql="INSERT INTO `user` SET ";
        if(is_array($arr))
        {
            $arr['add_time']=time();
            foreach ($arr as $key=>$value)
            {
                if($key!='uid')
                {
                    $sql .= "`" . $key . "`='" . $value . "',";
                }
            }
            //去掉最后的,并增加;
            $sql=rtrim($sql,',').";";
            if(execSQL($db,$sql,'insert'))
            {
                $res=true;
            }
            else
            {
               $res=false;
            }
        }
        else
        {
            $res=false;
        }

        if($res){
            echo json_encode(array('code'=>1,'text'=>'添加成功!'));
        }else{
            echo json_encode(array('code'=>0,'text'=>'添加失??!'));
        }

    }


    //更新演員
    function updateYanYuan($arr)
    {
        global  $db;
        $res=false;
        $sql="UPDATE `user` SET ";
        if(is_array($arr))
        {
            $arr['add_time']=time();
            $where='';
            foreach ($arr as $key=>$value)
            {
                if($key=='uid')
                {
                    $where=' where uid='.$value;
                    continue;
                }
                $sql.="`".$key."`='".$value."',";

            }
            //去掉最后的
            $sql=rtrim($sql,',');

            $sql.=$where.';';


            if(execSQL($db,$sql,'update'))
            {
                $res=true;
            }
            else
            {
                $res=false;
            }
        }
        else
        {
            $res=false;
        }

        if($res){
            echo json_encode(array('code'=>1,'text'=>'修改成功!'));
        }else{
            echo json_encode(array('code'=>0,'text'=>'修改失?。?#39;));
        }

    }

//刪除演員
function deleteYanYuan($arr)
{
    global  $db;
    $res=false;
    $sql="DELETE FROM  `user` WHERE `uid` in (".$arr['uid'].")";

    if(execSQL($db,$sql,'delete'))
    {
        $res=true;
    }
    else
    {
        $res=false;
    }


    if($res){
        echo json_encode(array('code'=>1,'text'=>'刪除成功!'));
    }else{
        echo json_encode(array('code'=>0,'text'=>'刪除失?。?#39;));
    }

}


    //
/**
 * @param $db
 * @param $sql
 * @param $method
 * @return bool|int|mysqli_result|string
 */
   function execSQL($db,$sql,$method)
    {
        $con=connect($db);
        //執(zhí)行操作 增、刪、改 操作
        $res=mysqli_query($con,$sql);
        //如果是插入操作,則替換為返回所受影響的函數(shù)
        if(strtolower($method)=="insert")
        {
            //執(zhí)行成功才獲取所受影響的函數(shù)
            if($res){
                $res = mysqli_insert_id($con);
            }
        }
        //關(guān)閉數(shù)據(jù)庫
        mysqli_close($con);
        return $res;
    }
?>

indexPage.php

<?php

require_once 'mysqli.php';

$con=connect($db);

$rows=select($con,'user','1=1','uid',20);

?>

<!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">
    <title>日本女演員表</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="static/js/jquery.js"></script>
    <script src="static/js/bootstrap.min.js"></script>
    <style type="text/css">

 h2
 {
            color: #666;
 }
        #tab
 {
            text-align: center;
 }
        #tab th,#tab td,#tab caption,#divTitle
 {
            text-align: center;
 padding: 8px;
 }
        #divBtn
 {
            text-align: left;
 width: 80%;
 margin: 0px auto;
 }

    </style>
</head>
<body>
<?php if (count($rows) > 0) :?>
 <div id="divTitle"><h2>日本女演員表</h2></div>
    <div id="divBtn">
        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#exampleModal" data-whatever="add">添加</button>
        <button type="button" class="btn btn-success btn-lg" data-toggle="modal" data-target="#exampleModal" data-whatever="update" id="editYanYuan">修改</button>
        <button type="button" class="btn btn-danger btn-lg" id="delYanYuan">刪除</button>
    </div>
    <table id="tab" border="1" cellspacing="0" align="center" width="80%">
        <caption></caption>
        <tr bgcolor="#add8e6">
            <th><input type="checkbox" id="mulSelect"/></th>
            <th>ID</th>
            <th>姓名</th>
            <th>手機號</th>
            <th>胸圍</th>
            <th>身高</th>
            <th>添加時間</th>
        </tr>

 <?php foreach ($rows as $row) : ?>
 <tr>
                <td><input type="checkbox" name="check<?php echo $row['uid'] ?>" value="<?php echo $row['uid'] ?>"/></td>
                <td><?php echo $row['uid'] ?></td>
                <td><?php echo $row['name'] ?></td>
                <td><?php echo $row['phone'] ?></td>
                <td><?php echo $row['weight'] ?></td>
                <td><?php echo $row['height'] ?></td>
                <td><?php echo date('Y-m-d H:i:s',$row['add_time']) ?></td>
            </tr>
 <?php endforeach; ?>
 </table>
 <?php else :?>
 <h2 style="color:red">未能加載數(shù)據(jù)</h2>
 <?php endif; ?>


<!--    模態(tài)框-->
 <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel"></h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group" style="display: none">
                            <label for="userID" class="control-label">ID:</label>
                            <input type="text" class="form-control" id="userID">
                        </div>
                        <div class="form-group">
                            <label for="userName" class="control-label">姓名:</label>
                            <input type="text" class="form-control" id="userName">
                        </div>
                        <div class="form-group">
                            <label for="userPhone" class="control-label">手機號:</label>
                            <input type="text" class="form-control" id="userPhone">
                        </div>
                        <div class="form-group">
                            <label for="userWeight" class="control-label">胸圍:</label>
                            <input type="text" class="form-control" id="userWeight">
                        </div>
                        <div class="form-group">
                            <label for="userHeight" class="control-label">身高:</label>
                            <input type="text" class="form-control" id="userHeight">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button>
                    <button type="button" class="btn btn-primary" id="sureID"></button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script>
 $(function(){
        // 全選按鈕的勾選和取消
 $("#mulSelect").on('click', function() {
            $("input:checkbox").prop("checked", $(this).prop('checked'));
 })
        // 全選后,如果子選項有一個取消勾選,則去除全選
 $("input:checkbox").on('click', function() {
            //當(dāng)選中的長度等于checkbox的長度的時候,就讓控制全選反選的checkbox設(shè)置為選中,否則就為未選中
 if($("input:checkbox").length === $("input:checked").length) {
                $("#mulSelect").prop("checked", true);
 } else {
                $("#mulSelect").prop("checked", false);
 }
        })

        $("#editYanYuan").on('click', function() {
            if($('input:checked').length==0)
            {
                alert("請選擇需要修改的演員");
 return;
 }
            if($('input:checked').length>1)
            {
                alert("每次只能修改一個演員");
 return;
 }
        })

        $("#delYanYuan").on('click', function() {
            if($('input:checked').length == 0)
            {
                alert("請勾選需要刪除的數(shù)據(jù)");
 return;
 }

            var uid='';
 $.each($('input:checkbox:checked'), function () {
                uid += $(this).val()+",";
 });

 uid=uid.substring(0,uid.length-1);


 $.ajax({
                type : "post",
 url : "mysqli.php?edit=deleteYanYuan",
 dataType : "json",
 data:{
                    uid  : uid
                },
 success : function(result){
                    alert(result.text);
 location.reload();
 },
 error:function(XMLHttpRequest){
                    alert(XMLHttpRequest.status);
 // alert(XMLHttpRequest.readyState);
 alert('添加失敗,請重試!');
 }
            })

        })


        // 模態(tài)框加載事件
 $('#exampleModal').on('show.bs.modal', function (event) {

            var button = $(event.relatedTarget);
 var recipient = button.data('whatever');    //存放點擊按鈕是添加還是修改

            //不符合條件則直接關(guān)閉
 if(recipient!="add")
            {
                if ($('input:checked').length == 0 || $('input:checked').length > 1) {
                    $(this).close();
 }
            }

            var modal = $(this);
 var type=(recipient=="add"?"添加":"修改");
 modal.find('.modal-title').text(type+"日本女演員");  //修改模態(tài)框表頭
            // modal.find('.modal-body input').val(recipient);
 modal.find('.modal-footer button[class="btn btn-primary"]').html(type);

 if(recipient!="add")
            {
                var uid = "0";
 $.each($('input:checkbox:checked'), function () {
                    uid = $(this).val();
 });

 $.ajax({
                    type: "post",
 url: "mysqli.php?edit=getYanYuan",
 dataType: "json",
 data: {
                        uid: uid
                    },
 success: function (result) {
                        var info = result.info;
 $("#userName").val(info.name);
 $("#userPhone").val(info.phone);
 $("#userWeight").val(info.weight);
 $("#userHeight").val(info.height);
 $("#userID").val(uid);


 },
 error: function (XMLHttpRequest) {
                        //alert(XMLHttpRequest.status);
                        // alert(XMLHttpRequest.readyState);
 alert('添加失敗,請重試!');
 }
                })
            }
        })


        //模態(tài)框確認(rèn)按鈕點擊事件
 $("#sureID").on('click', function() {
            var edit=$(this).html();

 var url="";
 if(edit=="添加")
            {
                url = "mysqli.php?edit=insertYanYuan";
 }
            if(edit=="修改")
            {
                url="mysqli.php?edit=updateYanYuan";
 }

            $.ajax({
                type : "post",
 url : url,
 dataType : "json",
 data:{
                    name  : $("#userName").val(),
 phone : $("#userPhone").val(),
 weight : $("#userWeight").val(),
 height: $("#userHeight").val(),
 uid:$("#userID").val()
                },
 success : function(result){
                    alert(result.text);
 location.reload();
 },
 error:function(XMLHttpRequest){
                    alert(XMLHttpRequest.status);
 // alert(XMLHttpRequest.readyState);
 alert('添加失敗,請重試!');
 }
            })

        });




 })


</script>

效果圖:

QQ截圖20190112173540.jpg

QQ截圖20190112173552.jpg

Correcting teacher:查無此人Correction time:2019-01-12 18:00:12
Teacher's summary:完成的不錯呀,特別是增加了選擇框,不錯,舉一反三。繼續(xù)加油。

Release Notes

Popular Entries