+ 1
About Php database connection
Can anyone suggest me how Can I connect login form to mysql database bin xampp server
1 Antwort
+ 3
Do you have a connection object yet. If not make one.
$db = new PDO("mysql:host=127.0.0.1;dbname=name", "root", "");
I assume that your html code is ready so lets catch the username and password fields.
$name = $_POST["username"];
$password = $_POST["password];
Query to find the username from your database
$query = $db->prepare("SELECT password FROM users WHERE username=:username");
$query->bindParam(":username", $username);
if($query->execute()) {
$row = $prepare->fetch();
if(password_verify($password, $row["password"])) {
$_SESSION["login] = username;
} else {
echo "User not found";
}
}
the password_verify() function works together with password_hash() function which you should have used on registration form
To make things easier you could make the session variable an array to store all user information.
"SELECT * FROM users WHERE username=:username"
$_SESSION["login"] = array("id" => $row["id"], "username" => $row["username"], "email" => $row["email"]);