Below is my code for making a user login the page but when I tried to submit the form it does not check database and directly gives me the else output i.e invalid ID or password I am not getting any error reports too though error reporting is turned on I am not sure where am I going wrong here.
<?php
if(isset($_POST['loginsubmit'])=='Login')
{
$email=$_POST['emaillogin'];
$pass=$_POST['passlogin'];
$pass=md5($pass);
$email = stripslashes($email);
$pass = stripslashes($pass);
$email = mysql_real_escape_string($email);
$pass = mysql_real_escape_string($pass);
require_once "database.php";
$sql = "SELECT * FROM user_log WHERE email = '$email' and password='$pass'";
$loginresult=mysql_query($sql);
$row=mysql_fetch_array($loginresult);
$rowcnt=mysql_num_rows($loginresult);
if($rowcnt==1)
{
session_start();
$_SESSION['email']=$row['email'];
$_SESSION['mobile']=$row['mobile'];
$_SESSION['fname']=$row['fname'];
$_SESSION['lname']=$row['lname'];
echo " <script>
window.location = '../';
</script>";
}
else
{
echo " <script>
alert('Invalid Login ID or Password....');
window.location = '../';
</script>";
}
I've even tried to echo wheather I am getting email and password properly from form and yes I was getting but issue starts from below line I guess:
$sql = "SELECT * FROM user_log WHERE email = '$email' and password='$pass'";
EDIT:FORM ADDED BELOW
<form action="" method="POST">
<input type="email" id="email" placeholder="[email protected]" name="emaillogin" required>
<input type='password' id='password' name='passlogin' placeholder='Password here' required>
<input type="submit" id="continuesubmitemail" name="loginsubmit" value="Login">
</form>
isset() is used to check variable is set or not
You can check your condition as
if(isset($_POST['loginsubmit']) && $_POST['loginsubmit']=='Login')
Also write session_start(); at the top of your page
Use while loop to get data
$rowcnt=mysql_num_rows($loginresult);
if($rowcnt==1)
{
while($row=mysql_fetch_array($loginresult))
{
$_SESSION['email']=$row['email'];
$_SESSION['mobile']=$row['mobile'];
$_SESSION['fname']=$row['fname'];
$_SESSION['lname']=$row['lname'];
}
echo " <script>
window.location = '../';
</script>";
}
And finally stop using mysql it is deprecated . You can use PDO or mysqli
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With