我有一個函數,檢查用戶數據是否存在於數據庫中,並返回用戶的電子郵件ID。如果它不存在,那麼它會插入並且應該返回插入的用戶電子郵件。插入數據庫後立即返回插入的數據數據
部分我的功能
function checkUser($userdata){
$oauth_uid = $userdata->id;
$email = $userdata->emailAddress;
$check = $this->db->query("SELECT * FROM $this->userTable WHERE oauth_uid = '".$oauth_uid."' AND email = '".$email."'");
if(mysqli_num_rows($check) > 0){
$result = $check->fetch_array(MYSQLI_ASSOC);
$query = "UPDATE $this->userTable SET fname = '".$userdata->firstName."', lname = '".$userdata->lastName."', email = '".$userdata->emailAddress."', location = '".$userdata->location->name."', country = '".$userdata->location->country->code."', picture_url = '".$userdata->pictureUrl."', profile_url = '".$userdata->publicProfileUrl."', modified = '".date("Y-m-d H:i:s")."' WHERE id = ".$result['id'];
$this->db->query($query);
return $result['id']; //this works it returns email of user if exists
}else{
$query = "INSERT INTO
$this->userTable(oauth_provider,oauth_uid,fname,lname,email,location,country,picture_url,profile_url,created,modified)
VALUES('linkedin','".$userdata->id."','".$userdata->firstName."','".$userdata->lastName."','".$userdata->emailAddress."','".$userdata->location->name."','".$userdata->location->country->code."','".$userdata->pictureUrl."','".$userdata->publicProfileUrl."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
$this->db->query($query);
$id = $this->db->insert_id;
$check = $this->db->query("SELECT * FROM $this->userTable WHERE oauth_uid = '".$id."'");
if(mysqli_num_rows($check) > 0){
$result = $check->fetch_array(MYSQLI_ASSOC);
return $result['id']; //this doesnt work. It inserts the data into database but doesn't return anything.
}
}
}
的的if語句正常工作。它檢查用戶是否存在,然後返回電子郵件。
else部分有效。它將用戶插入數據庫,但不返回電子郵件 我該如何完成這項工作。
謝謝。
可能重複的[PHP腳本中的增量值不工作](http://stackoverflow.com/questions/40573476/incrementing-value-in-php-script-not-working) – e4c5
認爲欺騙是PDO,它是仍然有效。在插入之前,您不需要SELECT並且您沒有使用預準備語句 – e4c5
您可以[** SQL注入**](https://www.owasp.org/index.php/SQL_Injection)。您需要使用準備好的語句,而不是將變量連接到查詢中。請參見[如何防止PHP中的SQL注入?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1)。 –