PHP-Login脚本无效

时间:2017-02-09 12:12:17

标签: php

PHP脚本应该接收两个变量:用户名和密码,但它没有这样做,它总是“echo”:“缺少输入”。 我试图回应这两个变量,但没有回应,我认为这意味着它们没有被初始化。

这是剧本:

require_once ('connect.php');
$username= $_POST['username'];
$password= $_POST['password'];
if(isset($_POST['username']) && isset($_POST['password'])) {
    if(!empty($username) && !empty($password)) {
        $query = "Select * from merchant where username='$username' and password = '$password' ";
        $r = mysqli_query($con, $query);
        if(mysqli_query($con,$query)) {
            echo "Welcome";

            mysqli_close($con);
        }
        else {
            echo "Wrong password or username";
            mysqli_close($con);
        }
    }
    else {
        echo "you must type both inputs";
    }
}
else {
    echo "missing input";
}

我尝试使用Postman和HTML页面发送帖子数据,但两者都返回相同的内容:“缺少输入”

这是我使用的HTML

<form action="mlog.php" method="post">
 <input type="textbox" name="username" value="username" />
  <input type="textbox" name="password" value="password" />
  <input type="submit" name="login" value="submit" />
  </form>

2 个答案:

答案 0 :(得分:0)

检查是否点击了登录按钮,然后检查用户名和密码是否为空,如果没有,则将变量分配给它们。

<?php
if(!empty($_POST['username']) && !empty($_POST['password'])) {
        $username= $_POST['username'];
        $password= $_POST['password'];
        $query = "Select * from merchant where username='$username' and password = '$password' ";
        $r = mysqli_query($con, $query);
        if($r) {
            echo "Welcome";
            //redirect

        }
        else {
            echo "Wrong password or username";
            mysqli_close($con);
        }
    }
    else {
        echo "you must type both inputs";
    }
}
?>

答案 1 :(得分:0)

<input type="text">

<form action="mlog.php" method="post">
 <input type="text" name="username" value="username" />
  <input type="text" name="password" value="password" />
  <input type="submit" name="login" value="submit" />
  </form>