I'm trying to use the mysqli bind_param
function in my website for the process of registering users. I want to do two things:
If the user registers with the registration form I provide, set the users password in the database to their desired password (hashed and salted, of course);
if they use Facebook registration, set the password field to NULL.
I realize that I can set the default of the password column in the database to NULL and simply not specify it in the statement upon registering, but this problem also carries on to other aspects of the site where this would not be a practical resolution.
$password = null;
$stmt->bind_param('ssssss', $email, $password, $fullname,
$prof_image_url, $splash_image_url, $facebook_id);
For testing purposes, I set the variable $password
to null directly before the statement is called and it still occurs. When the query is executed, it results in a row with the password column empty rather than saying NULL when I view it via command line.
All help is greatly appreciated!
EDIT: Here's the structure of the password column... Field: password, Type: varchar(255), Null: YES, Key:, Default: NULL, Extra:
function user_add($mysqli, $email, $password, $fullname, $prof_image_url = null, $splash_image_url = null, $facebook_id = null) {
if ($mysqli == null) throw new Exception('Could not connect to database.');
if ($email == null || $email == '') throw new Exception('Email is required.', 1);
if ($password != null && $password == '') throw new Exception('Password is required.', 1);
if ($fullname == null || $fullname == '') throw new Exception('Name is required.', 1);
if ($prof_image_url == null) $prof_image_url = IMAGE_PATH . '/profile_default.png';
if ($splash_image_url == null) $splash_image_url = IMAGE_PATH . '/splash_bg_1.jpg';
$email = $mysqli->real_escape_string($email);
$password = $mysqli->real_escape_string($password);
$fullname = $mysqli->real_escape_string($fullname);
$facebook_id = $mysqli->real_escape_string($facebook_id);
$stmt = $mysqli->prepare('INSERT INTO users(`email`, `password`, `full_name`, `prof_image_url`, `splash_image_url`, `facebook_id`) VALUES(?, ?, ?, ?, ?, ?);');
if (!$stmt) {
throw new Exception('Could not connect to database.');
}
$stmt->bind_param('ssssss', $email, $password, $fullname, $prof_image_url, $splash_image_url, $facebook_id);
if (!$stmt->execute()) {
throw new Exception('Could not register user.', 1);
}
return $mysqli->insert_id;
}
And this is where I call the function:
function account_facebook_register($mysqli, $code){
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . FACEBOOK_APP_ID
. "&redirect_uri=" . urlencode(FACEBOOK_REDIRECT_URL)
. "&client_secret=" . FACEBOOK_APP_SECRET
. "&code=" . $code
. "&scope=email";
$response = @file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$token = $params['access_token'];
$graph_url = "https://graph.facebook.com/me?access_token=" . $token;
$user = json_decode(file_get_contents($graph_url));
if (!isset($user->email)){
$_SESSION['error'] = 'Invalid email on Facebook account.';
$_SESSION['error_level'] = 1;
header('Location: ' . WEB_ROOT . '/account/register.php');
die;
}
$user_exists = user_getByEmail($mysqli, $user->email);
if ($user_exists == null){
$mysqli = mysql_create();
$password = null;
$uid = user_add($mysqli, $user->email, $password, $user->name, 'https://graph.facebook.com/' . $user->id . '/picture?type=large', null, $user->id);
$token = facebook_get_long_token($token);
token_add($mysqli, $uid, 'facebook', $token, $user->id);
$_SESSION['fb_token'] = $token;
$_SESSION['uid'] = $uid;
$_SESSION['success'] = "Registration Successful!";
}else{
account_facebook_login($mysqli, $user_exists->uid);
}
}
The mysql_create function just sets up the database for accessing and works as intended. Thank you!
Answer: mysqli->real_escale_string makes null values empty. Check if variable is null before you escape it.