• 技术文章 >后端开发 >php教程

    PHP实现数据库的增删查改

    不言不言2018-04-18 14:08:08原创1535
    这篇文章主要介绍了PHP实现数据库的增删查改 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下


    本文用到:jquery、tp框架

    TP_3.2.2/Application/Home/Controller/StuController.class.php

    <?php
    /**
     * Created by PhpStorm.
     * User: root
     * Date: 2018/4/17
     * Time: 16:32
     */
    
    namespace Home\Controller;
    
    
    use Think\Controller;
    
    class StuController extends Controller
    {
    
      public  function StuShow(){
            $this->display("school/stu");
        }
    
       public function getdata(){
          $Studata = M('stu');
          $data['id']='';
          $data['name']=I('get.name');
          $data['age']=I('get.age');
          $data['num']=I('get.num');
          $data['address']=I('get.add');
          $Studata->add($data);
          $this->success("正在。。。",U('Stu/showdata'));
        }
        public function showdata()
        {
            $Studata = M('stu');
            $data=$Studata->select();
            $this->assign('info',$data);
            $this->display('school/showdata');
        }
        public function del(){
            $id = I('get.id');
            $Studata = M('stu');
            $bool = $Studata->where(['id'=>$id])->delete();
            if($bool){
                echo 1;
            }else{
                echo 0;
            }
        }
        public function updata()
        {
            $id = I('get.id');
            $Studata = M('stu');
            $data = $Studata->where(['id'=>$id])->find();
            $this->assign('data',$data);//传递数据到前端页面
            $this->display("school/upshowdata");
        }
    
        public function updatadeal()
        {
            $Studata = M('stu');
            $id = I('get.id');
            $data['name']=I('get.name');
            $data['age']=I('get.age');
            $data['num']=I('get.num');
            $data['address']=I('get.add');
            $bool = $Studata->where(['id'=>$id])->save($data);
            if($bool){
                $this->showdata();
            }else{
                echo 0;
            }
        }
    }



    TP_3.2.2/Application/Home/View/school/showdata.html


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>数据展示界面</title>
    </head>
    <body id="content">
    <center>
        <h2>学生信息展示</h2>
    <table border="1">
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>学号</th>
        <th>籍贯</th>
        <th>操作</th>
        <th>操作</th>
    <foreach name="info" item="vo" >
        <tr>
            <td>{$vo['id']}</td>
            <td>{$vo['name']}</td>
            <td>{$vo['age']}</td>
            <td>{$vo['num']}</td>
            <td>{$vo['address']}</td>
            <td><a href="javascript:void(0)" class="del" where="{$vo['id']}">删除</a></td>
                <td><a href="javascript:void(0)" class="up" where="{$vo['id']}">修改</a></td>
        </tr>
    
    </foreach>
    </table>
    </center>
    </body>
    </html>
    
    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    <script>
        $('.del').click(function () {
            var where = $(this).attr('where');
            $.ajax({
                type: "get",
                url: "{:U('Stu/del')}?id="+where,
                success: function(msg){
                    if(msg==1){
                        alert('删除成功');
                        location.href('showdata');
                    }else {
                        alert('删除失败');
                    }
                }
            });
        })
    
        $('.up').click(function () {
            var where = $(this).attr('where');
            location.href('updata?id='+where);
            // $.ajax({
            //     type: "get",
            //     url: "{:U('Stu/updata')}?id="+where,
            //     success: function(msg){
            //         $('#content').html(msg);
            //     }
            // });
        })
    </script>

    TP_3.2.2/Application/Home/View/school/stu.html

    <!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>
    </head>
    <body>
    <form action="{:U('Stu/getdata')}" method="get">
        <br>
        名字: <input type="text" name="name">
        <br>
        年龄: <input type="text" name="age">
        <br>
        学号:<input type="text" name="num">
        <br>
        籍贯:<input type="text" name="add">
        <br>
        <input type="submit" value="提交">
        <br>
    </form>
    
    </body>
    </html>



    TP_3.2.2/Application/Home/View/school/stu.html


    <!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>
    </head>
    <body>
    <form action="{:U('Stu/updatadeal')}" method="get">
        <input type="hidden" value="{$data['id']}" name="id">
        <br>
        名字: <input type="text" name="name" value="{$data['name']}">
        <br>
        年龄: <input type="text" name="age" value="{$data['age']}">
        <br>
        学号:<input type="text" name="num" value="{$data['num']}">
        <br>
        籍贯:<input type="text" name="add" value="{$data['address']}">
        <br>
        <input type="submit" value="提交">
        <br>
    </form>
    
    </body>
    </html>

    执行在浏览器里面输入:http://127.0.0.1:90/TP_3.2.2/index.php/Home/Stu/stushow

    注意:端口一般是80,由于我修改过配置文件所以是90





    点击删除

    修改:


    相关推荐:

    PHP实现数据采集的方法

    以上就是PHP实现数据库的增删查改 的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:php 数据库 实现
    上一篇:php 文件上传 配置最大值 下一篇:PHP实现生成推广海报的方法
    大前端线上培训班

    相关文章推荐

    • 你必须了解PHP中什么是抽象类和抽象方法• 怎样使用PHP中的spl_autoload_register() 和 __autoload() 函数?• PHP中怎样去判断对象是否属于一个类?• 五分钟带你看PHP中的接口interface声明与应用(实例详解)• PHP中怎样完成Cookie的创建、读取和删除?

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网