PHP mySQL check if username and password are in the database

前端 未结 4 436
死守一世寂寞
死守一世寂寞 2020-12-10 23:49

I want my code to check if the username and password exist in my database. It does work hardcoded, but I want to do it with a database.Right now this is what I have:

4条回答
  •  青春惊慌失措
    2020-12-11 00:04

    Try this: (i have used mysql object oriented,if you want you can use other version)

    if(isset($_POST["name"], $_POST["password"])) {

            $name = $_POST["name"]; 
            $password = $_POST["password"];
    
    
            $select1 = "SELECT password FROM USERS WHERE username = '".$name."'";
    
            $result1=$conn->query($select1);
            $row1=$result1->fetch_assoc();
    
            $select2 = "SELECT username FROM USERS WHERE password = '".$password."'";
    
            $result2=$conn->query($select2);
            $row2=$result2->fetch_assoc();
    
    
            if($name == $row2["username"] && $password == $row1["password"]) 
            { 
                 $_SESSION["logged_in"] = true; 
                $_SESSION["naam"] = $name; 
    
            }
            else
            {
                echo'The username or password are incorrect!';
            }
    

提交回复
热议问题