我如何使用mysqli创建单个用户登录系统

时间:2016-11-26 19:18:32

标签: php mysqli

好吧所以我一直在寻找一个简单的登录系统,我看了很多视频,发现他们都在使用MYSQL,根据我的webhost,我需要使用MYSQLi所以我的代码发布在下面,有人可以帮助我,每次我按下登录它只刷新页面,什么都不做

<?php


  $error=''; //Variable to Store error message;
  if(isset($_POST['submit'])){
    if(empty($_POST['user']) || empty($_POST['pass'])){
        $error = "Username or Password is Invalid";
    }
    else
    {
        //Establishing Connection with server by passing server_name, user_id and pass as a patameter
      $host = "198.91.81.8";
      $user = "iishnoii_admin";
      $pass = "password";
      $db = "iishnoii_seclog";

      $con = mysqli_connect($host, $user, $pass, $db);
        //sql query to fetch information of registerd user and finds user match.
        $query = mysqli_query($conn, "SELECT * FROM users WHERE password='$pass' AND username='$user'");

        $rows = mysqli_num_rows($query);
        if($rows == 1){
            header("Location: welcome.php"); // Redirecting to other page
        }
        else
        {
            $error = "Username of Password is Invalid";
        }
        mysqli_close($conn); // Closing connection
    }

  }
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>IIShNoII</title>
  </head>
  <body>
    <div id="login-form">
      <form method="post" action="index.php">
        Username: <input type="test" name="username" /> <br /> <br />
        Password: <input type="password" name="password" /> <br /> <br />
        <input type="submit" name="submit" value="Log In" />
    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

在您的表单中,字段的名称为usernamepassword,但是当您尝试使用$_POST在PHP中检索它们时,您将其称为user并且pass