我正在构建一个使用Phil Sturgeon 的 CodeIgniter 的 RESTful 服务器作为 RESTful API 的 android 应用程序。
当 android 应用程序发出 POST 请求以注册用户facebook oauth data
时,当它到达服务器端时会调用以下方法。它可以工作,但如果其中一个或多个optional params
为空,它会将 a 插入0
到我的数据库中。
我该如何防止这种情况?我更喜欢它根本不输入任何内容或为空。
function fb_register_post(){
if($this->get_request_method() != "POST"){
$this->response('',406);
}
$oauth_email = $this->input->post('OAUTH_EMAIL');
$oauth_uid = $this->input->post('OAUTH_UID');
$oauth_provider = $this->input->post('OAUTH_PROVIDER');
$first_name = $this->input->post('FIRST_NAME');
$last_name = $this->input->post('LAST_NAME');
if(!empty($oauth_provider) and !empty($oauth_uid) and !empty($oauth_email) and !empty($first_name) and !empty($last_name)){
if(filter_var($oauth_email, FILTER_VALIDATE_EMAIL)){
$new_member_insert_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $oauth_email,
'OAUTH_EMAIL' => $oauth_email,
'OAUTH_PROVIDER' => $oauth_provider,
'OAUTH_UID' => $oauth_uid,
//OPTIONAL DATA
'gender' => $this->post('GENDER'),
'hometown' => $this->post('HOMETOWN'),
'bio' => $this->post('BIO'),
'birthday' => $this->post('BIRTHDAY')
);
$this->load->model('membership_model');
$data['user'] = $register = $this->membership_model->oauth_register($new_member_insert_data);
$this->response($data, 200);
}
}else{
$message = array('message' => 'FAIL');
$this->response($message, 201);
}
$message = array('message' => 'FAIL!');
$this->response($message, 200); // 200 being the HTTP response code
}
被调用的模型函数是:
function oauth_register($new_member_insert_data)
{
$insert = $this->db->insert('users', $new_member_insert_data);
if($insert){
$UID = $new_member_insert_data['OAUTH_UID'];
$q = $this->db->query("SELECT * FROM users WHERE OAUTH_UID = $UID LIMIT 1 ") or die(mysql_error());
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
else
{
return false;
}
}