skip to Main Content

For some reason my verify password is not working in my php. Not sure if it is the command or if I am missing something. I want it to not allow a user to login if the password is incorrect.

HTML and PHP below.

<HTML>

  <HEAD>
    <TITLE> Programming </TITLE>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <LINK REL="stylesheet" TYPE="text/css" href="homework2.css"> 

    </HEAD>
    <BODY>

      <!-- CSS for http://the7.dream-demo.com/ -->

      <div id="container">
        
        <div id="header">
          <div class="menuitem"> <a href="home.html">Home</a> </div>
          <div class="menuitem"><a href="products.html">Products</a></div>
          <div class="menuitem"><a href="cases.html">Case Studies</a></div>
          <div class="menuitem"><a href="pricing.html">Pricing</a></div>
          <div class="menuitem"><a href="aboutus.html">About Us</a></div>
        </div>

        <div id="bodycontent">
          
          <div id="banner">
           <div id="bannerleft"> <h1> We make you better athletes. Find out how! </h1> </div>
           <div id="signin"> 
             
             <form class="well form-inline" action="login.php" method="post">

              <input type="text" class="input-small" placeholder="Email"  name="email" >
              <input type="password" class="input-small" placeholder="Password" name="password">
              <br><br>

<!--
  If you do not want to use twitter bootstrap css then you should uncomment next 6 lines and uncomment the
  above 2 lines that provide input boxes

	    <label for="email">Email:</label>
	    <input type="text" name="email" id="email">
	    <br>
	    <label for="password">Password:</label>
	    <input type="password" name="password" id="password">
	    <br>
    -->

    <input type="submit" name="submit" id="logmein" value="Log In">
  </form>
  
</div>

</div>
<div id="featurestrip"> 

  
  <div id="signup">

    <form action="signup.php" method="post">

     
     <label for="firstname">Firstname:</label>
     <input type="text" name="signup-firstname" id="signup-firstname">
     <br>

     <label for="lastname">Lastname:</label>
     <input type="text" name="signup-lastname" id="signup-lastname">
     <br>

     <label for="email">Email:&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
     <input type="text" name="signup-email" id="signup-email">
     <br>
     <label for="password">Password:</label>
     <input type="password" name="signup-password" id="signup-password">
     <br>
     <label for="password">Reconfirm Password:</label>
     <input type="password" name="signup-repassword" id="signup-repassword">
     <br><br>
     <input type="submit" name="signmeup" id="signmeup" value="Sign Me Up!">
   </form>

 </div>

 <div id="featureright"> <p>Sign up and find out more on how we can help. Pricing starts at $19.95 a month. </p>
  <p><h3>Premium service starts at $49.95.</h3></p>

</div>   


</div>
<div id="corefeatures"> 



 <img height="200px" src="http://www.hockeymanitoba.ca/wp-content/uploads/2013/02/ltad-model.jpg">
</div>

<div id="testimonials"> Testimonial
  <img height="200px" src="http://www.neuroexplosion.com/storage/development%20model%20jpeg.jpg?__SQUARESPACE_CACHEVERSION=1305662626397">

  <img height="200px" src="http://www.phecanada.ca/sites/default/files/physical_literacy/LTAD_FMS.jpg">
</div>
       <!--
       <div id="portfolio"> Portfolio</div>
       <div id="skills"> Skills</div>
     -->
   </div>
   
   <div id="footer">Copyright Notice. All Rights Reserved. 2014</div>

 </div>

 
</BODY> 
</HTML>

PHP

    <?php



$mysql_hostname = 'localhost';
$mysql_user = 'username';
$mysql_password = 'password';
$mysql_database = 'db_users2015';

$connect = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die ("Couldn't connect");

echo "<BR>Connection Successful";

  //to put data into database
  //select database
  $db_selected= mysql_select_db($mysql_database, $connect) 
  or die ("Couldn't connect to the database");

  $email= $_POST['email'];
  $password= $_POST['password'];

$sql = "SELECT COUNT (*) FROM users WHERE email= '{$_POST['email']}' AND password= '{$_POST['password']}'";
$sql_result = mysql_query($sql);

if(["email"]==$email &&["password"]==$password)
   echo ("Login Successful.");

else{
    die("Wrong Password.");
  }


$sql = "SELECT COUNT(*) FROM users WHERE email= '{$_POST['email']}'";
$sql_result = mysql_query($sql);
if (mysql_result($sql_result, 0)<1)
 {
die("<BR>Email address not found");
}

else{
    echo "Login Successful!";
}




?>

2

Answers


  1. You’ve selected on your query only the COUNT(*) from database where email and password is the same you’ve received from the form, not the columns to compare, and you too forgot mysql_fetch_assoc to fetch the row from database, try this:

    $sql = sprintf("SELECT COUNT(*) as qty FROM users WHERE email= '%s' and password = '%s'", mysql_real_escape_string($_POST['email']), mysql_real_escape_string($_POST['password']));
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    
    if ($row['qty']) {...
    
    Login or Signup to reply.
  2. I’m not getting, why you are using 2 separate query for checking user. But, it’s your requirement, so i posted my answer according to the question.

    <?php
    $mysql_hostname = 'localhost';
    $mysql_user = 'username';
    $mysql_password = 'password';
    $mysql_database = 'db_users2015';
    
    $connect = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
    or die ("Couldn't connect");
    
    echo "<BR>Connection Successful";
    
    $db_selected= mysql_select_db($mysql_database, $connect) or die ("Couldn't connect to the database");
    
    $email= $_POST['email'];
    $password= $_POST['password'];
    
    $QueryEmail = mysql_query("SELECT * FROM users WHERE email= '$email'");
    $CountResultEmail=mysql_num_rows($QueryEmail);
    
    if($CountResultEmail==1)
    {
        echo "Correct Email";
    
        $QueryPassword=mysql_query("SELECT * FROM users WHERE email= '$email' AND password='$password'");
        $CountResultPassword=mysql_num_rows($QueryPassword);
        if($CountResultPassword==1)
        {
            echo "Login Successful";
        }
        else
        {
            echo "Wrong Password";
        }
    }
    else
    {
        echo "Wrong Email";
    }
    ?>
    

    You can check Email & Password in one Query. You can use this as well.

    <?
    .
    .
    $email= $_POST['email'];
    $password= $_POST['password'];
    
    $QueryResult = mysql_query("SELECT * FROM users WHERE email= '$email' AND password='$password'");
    $CountResult=mysql_num_rows($QueryResult);
    
    if($CountResult==1)
    {
        echo "Login Successfull";
    }
    else
    {
        echo "Wrong Credentials";
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search