简体   繁体   中英

Fill in the login field with your username OR email

I built a register and login system on my website. Now I asked myself a question that I cannot answer. I have three inputs on my registration page:

  1. Username
  2. Email
  3. Password

I would now like to have two inputs on my login page instead. The first should be for password and the second for email OR username. I now wonder how it is possible to do this with the MySQL database?

I am still very new to PHP. I hope someone can answer, I haven't found anything on Stack Overflow.

If you have a two fields called "username" and "email" in your database, and you want to check if the user exists based on the user having potentially entered either one of these values, then you could write a query something like this:

SELECT *
FROM users
WHERE username = ? OR email = ?

which would return any row where the parameter value matches the content of either the username or the email column.

(Adjust for your exact table and column names, obviously.)

  1. First when you post the form with 2 fields try to validate if input given is of type email or not
  2. Here is how you do it https://www.php.net/manual/en/filter.examples.validation.php
  3. now you know which column against the value is to be passed
  4. if the username is not of type email use username column
  5. if username is of type email use email column instead.

Prepare your query accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM