php接收用户输入和对数据库得增删改查

  public function test()
    {
        $id=$_GET["id"];
        return $id;
    }

    public function get(){
        // 获取一个值,如果没有就获取第二个作为默认值
        // laravel中echo只能输出字符串
        echo Input::get("id","10086");
    }

    public function all(){
        // 获取所有参数
        $all = Input::all();

        // dump+die
        dd($all);
    }

    public function has(){
        // 判断id是否存在
        $has = Input::has("id");
       if ($has){
           echo "存在";
       } else{
           echo "不存在";
       }
    }
    public function except(){
        // 获取除了指定参数之外得值
        dd(Input::except(["id"]));
    }
    public function only(){
        // 获取指定参数,返回数组
        dd(Input::only(["name"]));
    }
    /**
     * 获取用户输入,可以接受post也可以接受get
     */
    public function getParam(){
        // 获取所有用户输入
        $all1 = Input::all();

        // 获取单个用户输入
        $get1 = Input::get("name");

        // 获取指定几个用户输入
        $only = Input::only(["name"]);

        // 判断输入得某个值是否存在
        $has = Input::has("name");
    }

    public function add(){
        $table = DB::table("test1");
        // 二维数组批量插入,一位数组单条添加
        $result = $table -> insert([
            [
                'name' => '马冬梅',
                'age' => 16,
                'address' => '地址',
                'sex' => '女'
            ],
            [
                'name' => '马冬梅1',
                'age' => 16,
                'address' => '地址',
                'sex' => '女'
            ]]
        );
     // 插入一条数据并返回id
        $result = $table -> insertGetId(
            [
                'name' => '马冬梅21',
                'age' => 16,
                'address' => '地址',
                'sex' => '女'
            ]
        );
        dd($result);
    }

    public function del(){
         DB::table("test1")->where("id","=","1")->delete();
    }

    public function select1(){
        // 手写sql
        $all = DB::select("select name,age from test1");
        dd($all);
    }

    public function opration(){
        // 手写增删改sql
        DB::statement("insert into test1 (name,age) values('胡琪',223)");
    }

    public function update(){
        // 修改test1表中id等于1得name为张三风
        $table = DB::table("test1");
        $result = $table -> where("id","=","1") -> update([
            "name"=>"张三丰"
        ]);

        // 每次加1
        DB::table("test1")->increment("age");

        // 每次加5
        DB::table("test1")->increment("age",5);
        return $result;
    }

    public function select(){
        // 查询test1表中所有得数据
         $all = DB::table("test1")->get();

         // 循环打印
         foreach ($all as $key => $value){
             echo "id是:{$value -> id} name是{$value -> name}";
         }

         // where条件查询。and之间用->where->where
        // or之间用->orWhere()->orWhere()
        $get = DB::table("test1")->where("id","=","1")->get();

         // 获取与之匹配得第一条
        $one = DB::table("test1")->where("id","=","1")->first();
         dd($one);

         // 获取指定得一个字段
        $value1 = DB::table("test1")->where("id","=","1")->value("name");
        dd ($value1);
        //获取指定列
        $values = DB::table("test1")->select("name","age")->get();
        return $values;
        // orderBy得使用
        $values = DB::table("test1")->select("name","age")->orderBy("age","desc")->get();
        return $values;
        // 跳过前两条显示两条
        $values1 = DB::table("test1")->select("name","age")->limit(2)->offset(2)->get();
        return $values1;
    }

2.给导包起别名(config包中app.php得aliasess)

 

posted @ 2022-01-10 16:47  动力起点  阅读(141)  评论(0编辑  收藏  举报