html - Username and Password trouble in PHP -
i trying create login form php cannot login. incorrect password/username instead. have triple checked credentials in database , correct. please help
here code
<!doctype html> <html> <head> <meta charset="utf-8"> <title>login</title> <link rel="stylesheet" href="css/style.css" /> </head> <body> <?php require('db.php'); session_start(); // if form submitted, insert values database. if (isset($_post['username'])){ $username = $_post['username']; $password = $_post['password']; $username = stripslashes($username); $username = mysqli_real_escape_string($connection,$username); $password = stripslashes($password); $password = mysqli_real_escape_string($connection,$password); //checking user existing in database or not $query = "select * `backuser` username='".$username."' , password='".md5($password)."'"; $result = $connection->query($query) or die(mysqli_error()); $rows = mysqli_num_rows($result); if($rows==0){ $_session['username'] = $username; header("location: index.php"); // redirect user index.php }else{ echo " <div class='form'><h3>username/password incorrect.</h3><br/>click here <a href='login.php'>login</a></div>"; } }else{ ?> <div class="form"> <h1>log in</h1> <form action="" method="post" name="login"> <input type="text" name="username" placeholder="username" required /> <input type="password" name="password" placeholder="password" required /> <input name="submit" type="submit" value="login" /> </form> </div> <?php } ?> </body> </html>
you have error in logic. change if($rows==0)
if($rows!=0)
. if user in db, should have @ least 1 record returned , mysqli_num_rows()
should return number >= 1
.
Comments
Post a Comment