I'm building an android app that uses Phil Sturgeon's RESTful Server for CodeIgniter as a RESTful API.
When the android app makes a POST request to register a user with facebook oauth data
the method below is called when it reaches server side. It works, but if one or more of the optional params
is empty it will insert a 0
into my database.
How do I prevent this? I'd much prefer it enters nothing at all or null.
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
}
The model function being called is :
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;
}
}