PHP和HTML检查用户名是否在数据库中

时间:2019-03-22 12:29:05

标签: php html phpmyadmin

我正在尝试解决与PHP有关的问题。 到目前为止,我已经创建了一个html登录表单login.html和一个PHP文件login-insert.php。我无法检测使用phpmyadmin创建的数据库中是否存在用户名。我对使用PHP编码非常陌生,因此不胜感激

请参考PHP和html代码https://liveweave.com/rAE7Cy

的链接

2 个答案:

答案 0 :(得分:1)

步骤1     创建文件config.php

<?php
   define('DB_SERVER', 'localhost:3036');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', 'rootpassword'); //modify it as per your password
   define('DB_DATABASE', 'database'); //modify it as per your databse
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

第2步     创建另一个文件login.php

<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['uname']);
      $mypassword = mysqli_real_escape_string($db,$_POST['psw']); 

      $sql = "SELECT id FROM admin WHERE username = '$myusername' and password = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("username");
         $_SESSION['login_user'] = $myusername;

         header("location: home.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>

答案 1 :(得分:0)

使用扩展名login.php而不是login.html更改文件名,然后在同一页面中通过ajax发送变量值

<?php

    // starting the session
    session_start();

    // checking if username and password entered of not
    if(isset($_POST['userid']) && isset($_POST['userpass'])){
        $server = 'server_name';
        $user = 'server_username';
        $pass = 'server_password';
        $db = 'database_name';

        $conn = new mysqli($server, $user, $pass, $db);

        $userid = base64_encode($_POST['userid']);
        $userpass = base64_encode($_POST['userpass']);
        $useremail = $_POST['userid'];

        $q = "SELECT * FROM table_name WHERE UserID='$userid' AND UserPWD='$userpass'";  // Fetching data from table
        $d = mysqli_query($conn, $q);
        if(mysqli_num_rows($d) == 1){
            while ($t = mysqli_fetch_assoc($d)) {
                $_SESSION['login_status'] = 1; // Creating A login status variable in case of logout make this variable value 0
                echo '1';
            }
        }else{
            echo '2';
        }
        mysqli_close($conn);
    }else{  //if username and password not entered
        ?>
            <input type="text" class="userid" placeholder="Username">
            <input type="password" class="userpass" placeholder="Password">
            <a href="#" class="login_btn ">login</a>

            <script>

                $('.login_btn').on('click',function(){
                    var userid = $('.userid').val();
                    var userpass = $('.userpass').val();
                    if(userid != ""){
                        if(userpass != ""){
                            $.ajax({
                                type:'post',
                                data:{
                                    userid:userid,
                                    userpass:userpass,
                                },
                                url:'login.php',  // Sending variable to same page

                                success:function(e){
                                    if(e == 1){ // Userid and password is correct
                                        window.location = 'index.php';
                                    }else if(e == 2){
                                        alert("Invalid Username and password");
                                    }else{
                                        console.log(e);
                                    }
                                }
                            })
                        }else{
                            alert("Password Required");
                        }
                    }else{
                        alert("Username Required");
                    }
                })
            </script>
        <?php        
    }

?>