2015-04-06 29 views
1

使用API​​更新表我卡在使用API​​ CI中, 我已經閱讀code tutsplus如何在笨

教程,但沒有spesific做更新錶行, 所以我想我自己並卡住了:-(

網址:

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

這裏的JSON respon:

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

我的控制器看起來像下面這樣:

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); 
    } 
    } 

我的模型:

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); } 

有沒有什麼不對或misstype, 感謝你們 我是新的這個東西。

+0

1)。你真正想要做什麼2)你已經在你的模型中編寫了'//查詢來檢查用戶名是否已經存在',而不是檢查你是直接更新你的字段3)'if($ result)'它沒有有任何意義 – 2015-04-06 11:19:51

回答

1

您可以檢查笨文檔如何工作數據庫方法http://www.codeigniter.com/userguide3/database

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

在你的情況,你需要和返回否則你無法使用方法$結果,因爲這將等於NULL ..

  • 檢查和CI表單驗證庫,因爲您不驗證您的輸入數據(甚至轉義)它可能會產生問題。
  • 重要的是,您應該編寫正確的方法名稱:signup_insert應該INSERT而不是UPDATE
+0

謝謝你的解釋,它的工作原理。花花公子。再次感謝。 @svetlio – 2015-04-07 02:07:20

0

'nopol' => $this->input->get_post('nopol') in this change to 'nopol' => $this->input->get_post('no_polis'),

也$這 - > DB->其中($條件)$條件沒有限定。

0

像@svetlio說, 我添加一些解決方案,如果諾卜醇是typo(misstype)或null。它會返回false, ,所以我添加一些代碼來做到這一點。 這樣下面:

if ($this->db->affected_rows() === 1) { 
    return true; 
    } else { 
    return false; 
    } 
    } 

所以它不會像:

return $this->db->update('user', $data);