我对这个领域全新。我正在尝试使用Google OAuth来提取数据,但遗憾的是,我无法将我的用户名值插入到我的MySQL数据库中。其他语句工作正常并正确插入,但应插入用户名的语句不能按预期工作。我花了近4个小时试图解决它。我不知道该怎么做。
的index.php
<?php ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE); ?>
<?php
include_once("config.php");
include_once("includes/functions.php");
//print_r($_GET);die;
if(isset($_REQUEST['code'])){
$gClient->authenticate();
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirect_url, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) {
$userProfile = $google_oauthV2->userinfo->get();
//DB Insert
//$gUser->setApprovalPrompt ("auto");
$gUser = new Users();
// As of PHP 5.3.0
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],$userProfile['locale'],$userProfile['link'],$userProfile['picture'],$username);
$_SESSION['google_data'] = $userProfile; // Storing Google User Data in Session
header("location: feed.php");
$_SESSION['token'] = $gClient->getAccessToken();
} else {
$authUrl = $gClient->createAuthUrl();
}
$email = $_SESSION['google_data']['email'];
$user = strstr($email, '@', true);
$username = $user;
?>
的functions.php
<?php ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE); ?>
<?php
session_start();
class Users {
public $tableName = 'users';
function __construct(){
//database configuration
$dbServer = 'localhost'; //Define database server host
$dbUsername = 'root'; //Define database username
$dbPassword = ''; //Define database password
$dbName = 'livelor'; //Define database name
//connect databse
$con = mysqli_connect($dbServer,$dbUsername,$dbPassword,$dbName);
if(mysqli_connect_errno()){
die("Failed to connect with MySQL: ".mysqli_connect_error());
}else{
$this->connect = $con;
}
}
function checkUser($oauth_provider,$oauth_uid,$fname,$lname,$email,$gender,$locale,$link,$picture,$username){
$prevQuery = mysqli_query($this->connect,"SELECT * FROM $this->tableName WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
if(mysqli_num_rows($prevQuery) > 0){
$update = mysqli_query($this->connect,"UPDATE $this->tableName SET oauth_provider = '".$oauth_provider."', oauth_uid = '".$oauth_uid."' ,fname = '".$fname."', lname = '".$lname."', email = '".$email."', gender = '".$gender."', locale = '".$locale."', picture = '".$picture."', gpluslink = '".$link."', modified = '".date("Y-m-d H:i:s")."' WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
}else{
$insert = mysqli_query($this->connect,"INSERT INTO $this->tableName SET oauth_provider = '".$oauth_provider."', oauth_uid = '".$oauth_uid."', fname = '".$fname."', lname = '".$lname."', email = '".$email."', gender = '".$gender."', locale = '".$locale."', picture = '".$picture."', gpluslink = '".$link."', created = '".date("Y-m-d H:i:s")."', modified = '".date("Y-m-d H:i:s")."' , username='".$username."' ") or die(mysqli_error($this->connect));
}
$query = mysqli_query($this->connect,"SELECT * FROM $this->tableName WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$oauth_uid."'") or die(mysqli_error($this->connect));
$result = mysqli_fetch_array($query);
return $result;
}
}
答案 0 :(得分:3)
您的代码中存在两个主要问题。首先,使用无效的SQL语法。它是INSERT INTO table(column) VALUES(value)
,而不是SET
。据我所知,你的$username
没有定义。您正在尝试插入$username
,它将传递到函数中:
function checkUser($oauth_provider,$oauth_uid,$fname,$lname,$email,$gender,$locale,$link,$picture,$username){
// other code
$insert = mysqli_query($this->connect,"INSERT INTO $this->tableName ( oauth_provider, oauth_uid, fname, lname, email, gender, locale, picture, gpluslink, created, modified, username) VALUES( '".$oauth_provider."', '".$oauth_uid."', '".$fname."', '".$lname."', '".$email."', '".$gender."', '".$locale."', '".$picture."', '".$link."', '".date("Y-m-d H:i:s")."', '".date("Y-m-d H:i:s")."' , '".$username."')") or die(mysqli_error($this->connect));
// etc
}
跟踪函数调用
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],$userProfile['locale'],$userProfile['link'],$userProfile['picture'],$username);
将空$username
变量作为参数传递给函数 IS NOT DEFINING IT 。
我还没有看到$username
在此之前的任何地方被定义。
我希望这听起来不是很讽刺,但为了设置$username
,你必须在函数调用之前定义$username
或将设置字符串传递给函数。
$username = 'banana';
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],$userProfile['locale'],$userProfile['link'],$userProfile['picture'],$username);
OR
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],$userProfile['locale'],$userProfile['link'],$userProfile['picture'],'banana');
答案 1 :(得分:3)
更改此行: -
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],$userProfile['locale'],$userProfile['link'],$userProfile['picture'],$username);
到此: -
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],$userProfile['locale'],$userProfile['link'],$userProfile['picture'],strstr($userProfile['email'], '@', true));
答案 2 :(得分:0)
你的插入sql是错误的 - 检查正确的语法:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
答案 3 :(得分:0)
事实证明,INSERT INTO for MySQL有一种SET替代语法。谁知道。
好的,问题出在这里:
--This will return false, $username is not set
echo isset($username);
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],
$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],
$userProfile['locale'],$userProfile['link'],$userProfile['picture'],$username);
$_SESSION['google_data'] = $userProfile; // Storing Google User Data in Session
header("location: feed.php");
$_SESSION['token'] = $gClient->getAccessToken();
} else {
$authUrl = $gClient->createAuthUrl();
}
$email = $_SESSION['google_data']['email'];
$user = strstr($email, '@', true);
$username = $user;
您在分配之前尝试使用$ username变量。你需要做的是:
$email = $_SESSION['google_data']['email'];
$user = strstr($email, '@', true);
$username = $user;
--This will return true, $username is set
echo isset($username);
$gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],
$userProfile['family_name'],$userProfile['email'],$userProfile['gender'],
$userProfile['locale'],$userProfile['link'],$userProfile['picture'],$username);
$_SESSION['google_data'] = $userProfile; // Storing Google User Data in Session
header("location: feed.php");
$_SESSION['token'] = $gClient->getAccessToken();
} else {
$authUrl = $gClient->createAuthUrl();
}