I'm trying to make my login work, the problem is whenever i press Sign Up i get an error, as i see the line 112 is the }else{, so im wondering, is there a work around for the ELSE part on a foreach? Thanks!
Parse error: syntax error, unexpected 'else' (T_ELSE) in F:\xampp\htdocs\SocialMedia\first\index.php on line 112
This is my code:
if(isset($_POST['user_login']) && isset($_POST['password_login'])){
$user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['user_login']);
$password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password_login']);
$password_login_md5 = md5($password_login);
$sql = $databaseConnection->prepare('SELECT id,username,password, FROM users WHERE username = :user_login, password = ":password_login_md5"');
$sql->bindParam(':user_login', $user_login);
$sql->bindParam(':password_login_md5', $password_login_md5);
$sql->execute();
$userCount = $sql->rowCount();
foreach($userCount as $row){
if($row > 0){
$id = $row['id'];
}
$_SESSION["user_login"] = $user_login;
header("Location: index.php");
exit();
}else{
echo "That information is incorrect, try again";
}
}
?>
Thanks in advance!
EDIT: this is what i have now:
if(isset($_POST['user_login']) && isset($_POST['password_login'])){
$user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['user_login']);
$password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST['password_login']);
$password_login_md5 = md5($password_login);
$sql = $databaseConnection->prepare('SELECT id,username,password, FROM users WHERE username = :user_login, password = ":password_login_md5"');
$sql->bindParam(':user_login', $user_login);
$sql->bindParam(':password_login_md5', $password_login_md5);
$sql->execute();
$userCount = $sql->rowCount();
if($userCount){
foreach($userCount as $row){
if($row > 0){
$id = $row['id'];
}
}
}else{
echo "information incorrect";
}
}
No errors in there, just whenever i press Login i get this error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in F:\xampp\htdocs\SocialMedia\first\index.php:101 Stack trace: #0 F:\xampp\htdocs\SocialMedia\first\index.php(101): PDOStatement->execute() #1 {main} thrown in F:\xampp\htdocs\SocialMedia\first\index.php on line 101
And i think, im 100% sure that its my query...