Login through loginza

I am trying to allow my users to log in by email or by username. Currently, only the username is accepted. My working SQL looks like this:

$sql = "SELECT * FROM customers WHERE login = '".$username."' AND password = '".$password."'"; 

information:

  • $username is $_POST['username']
  • $password is md5($_POST['password'])

Now I would like to distribute it to an email address that a client can enter in his profile. My SQL looks like this:

 $sql = "SELECT * FROM customers WHERE (login = '".$username."' OR email = '".$username."') AND password = '".$password."'"; 

I will check this SQL with:

 $result = mysql_query($sql); mysql_num_rows($result) 

But at the moment this does not work. If I use OR in my SQL, mysql_num_rows returns 0 . What could be the problem? Or is there another and better way to achieve this?

+6
source share
5 answers

Try it: Can the username have a username or email address on the right? Therefore, first check to see if the "@" character is in the username string.

it

 if(stripos($username,'@') !== FALSE){ $sql = "SELECT * FROM customers WHERE email = '".$username."' AND password = '".$password."'"; }else{ $sql = "SELECT * FROM customers WHERE login = '".$username."' AND password = '".$password."'"; } 

Thus, if the condition is successful, the letter will be checked. Username will be verified

0
source

Try this, I'm sure it will work:

$ sql = "SELECT * FROM customers WHERE login = '". $ username. "'| email ='". $ username. "And password = '". $ password. "'";

And to determine when the password is incorrect, or only login credentials:

add this:

if (mysql_num_rows ($ result) == 0) {echo 'Invalid username or password!';}

0
source

Maybe the problem is with characters?

 SELECT * FROM `customers` WHERE (LOWER(`login`)='" . strtolower($username) . "' OR LOWER(`email`)='" . strtolower($username) . "') AND `password`='" . $password . "'" 
-1
source
  $sql = "SELECT * FROM customers WHERE EXISTS(select 1 from customers where login = '".$username."' OR email = '".$username."') AND password = '".$password."'"; 

Try this, I hope your problem is resolved.

-1
source

You can just use 2 queries and or in the condition

 $query=mysql_query("select * from customers where user='$username' and password='$password'"); $query1=mysql_query("select * from customers where email='$username' and password='$password'"); if(mysql_num_rows($query)==1 || mysql_num_rows($query1)==1) { //YOUR CODE } else { //YOUR CODE } 
-1
source

Source: https://habr.com/ru/post/944504/


All Articles