如何codeigniter使用API​​更新表 [英] how to update table using api in codeigniter

查看:180
本文介绍了如何codeigniter使用API​​更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被堵在codeigniter使用API​​更新表行,
我已经读的 code tutsplus

但没有spesific做到这一点,
所以我想通过自己和卡住了: - (

网址:

<$p$p><$c$c>http://localhost/work/bnilife/v1/signup/user/post?nopol=a1b2c3d4e5&username=agus&password=kucingtikus&captcha=c12ds

这里的JSON respon:

  {
错误:错误:-(
}

我的控制器看起来像下面这样:

 公共职能user_post()
  {
    date_default_timezone_set('亚洲/雅加达');
    $ datestring =%Y-%M-%D%H:%I:%S;
    $停留的时间=();
    $日期时间= mdate($ datestring,$时间);    $数据=阵列(
            诺卜醇'=&GT; $这个 - &GT;输入 - &GT; get_post('诺卜醇),
            用户名= GT; $这个 - &GT;输入 - &GT; get_post(用户名),
            '密码'=&GT; SHA1($这个 - &GT;输入 - &GT; get_post('密码'))
            'created_at'=&GT; $日期时间,
            '的updated_at'=&GT; $日期时间
    );    $结果= $这个 - &GT; signup_m-&GT; signup_insert($的数据);
    如果($结果){
      $这个 - &GT;响应($的数据,200);
    }其他{
      $这个 - &GT;响应(阵列('错误'=&GT;'错误:-(),400);
    }
  }

我的模型:

 公共职能signup_insert($数据){  //查询检查用户名是否已经存在与否
  $条件=诺卜醇=。 '。 $数据['诺卜醇']。 ';
  $这个 - &GT; DB-化合物其中($条件);
  $这个 - &GT; DB-GT&;更新(用户,$数据); }

有没有什么不对或misstype,
感谢你们
我是新的这个东西。


解决方案

您可以检查codeigniter文档是如何工作数据库方法的 HTTP://www.$c$cigniter.com/userguide3/database

 公共职能signup_insert($数据){
  $这 - &GT; DB-化合物其中('诺卜醇',$数据['诺卜醇']);
  返回$这个 - &GT; DB-GT&;更新(用户,$数据);
}

在你的情况你需要并返回否则你无法使用方法$结果,因为这将等于NULL。


  • 检查和CI表单验证库,你不验证输入数据(甚至逃跑)它可能会产生问题。

  • 重要的是,你应该写正确的方法名: signup_insert 插入不是更新

I'm stuck at updating table row using api in codeigniter, i already have read tutorial from code tutsplus

but there's no spesific to do it, so i tried by myself and got stuck :-(

url request:

http://localhost/work/bnilife/v1/signup/user/post?nopol=a1b2c3d4e5&username=agus&password=kucingtikus&captcha=c12ds

Here's the json respon:

{
"error": "error :-( "
}

My Controller look like this below:

public function user_post()
  {
    date_default_timezone_set('Asia/Jakarta');
    $datestring ="%Y-%m-%d %H:%i:%s";
    $time       =time();
    $datetime   =mdate($datestring, $time);

    $data = array (
            'nopol'   => $this->input->get_post('nopol'),
            'username'   => $this->input->get_post('username'),
            'password'   => sha1($this->input->get_post('password')),
            'created_at' => $datetime,
            'updated_at' => $datetime
    );

    $result = $this->signup_m->signup_insert($data);
    if($result) {
      $this->response($data, 200);
    } else {
      $this->response(array('error' => 'error :-( '),400);
    }
  }

My model:

  public function signup_insert($data) {

  // Query to check whether username already exist or not
  $condition = "nopol=" . "'" . $data['nopol'] . "'" ;
  $this->db->where($condition);
  $this->db->update('user', $data); }

Is there any something wrong or misstype, thank you guys i'm new at this stuff.

解决方案

You can check codeigniter documentation how are working Database methods http://www.codeigniter.com/userguide3/database

public function signup_insert($data) {
  $this->db->where('nopol',$data['nopol']);
  return $this->db->update('user', $data); 
}

In your case you need and return else you can't use the method as $result as it will be equal to NULL..

  • Check and CI Form Validation library as you don't validate your input data (even escaped) it may generate problems.
  • And importantly, you should write proper method names: signup_insert should INSERT not UPDATE.

这篇关于如何codeigniter使用API​​更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆