I am having some trouble using functions to check if user account already exist in the backend. I created two functions: one to check if user account already exist and the other to create account.
Can someone enlighten me...? What is wrong with the code here?
<?php
try{
$username = 'web';
$password = '1234';
$username_signup = $_POST['username_signup'];
$password_signup = $_POST['password_signup'];
$hash = crypt($_POST['password'], '$3a$08$2'); // salt
$connection = new PDO ('mysql:host=localhost;dbname=tongue', $username, $password);
$connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
function check(c){
$statement = $connection->prepare('
SELECT email FROM user
WHERE email=:username;
');
$statement -> bindParam(':username', c, PDO::PARAM_STR, 127);
$check = $statement -> execute();
return $check;
};
function create(a,b){
$statement = $connection->prepare('
INSERT INTO user (email, hash)
VALUES (:username, :hash);
');
$statement -> bindParam(':username', a, PDO::PARAM_STR, 127);
$statement -> bindParam(':hash', b, PDO::PARAM_STR);
$statement -> execute();
}
check($username_signup);
if ($check==0){
create($username_signup, $hash);
header("Location=index.php");
exit();
} else {
header("Location=sign_up.php?error=1");
exit();
}
$connection = null;
} // try{}
catch(PDOException $e) {
echo $e->getMessage();
}
?>