这是我的Login.php
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
if(empty($_POST['username']) || empty($_POST['password'])) {
$response["success"] = 0;
$response["message"] = "Please fill in the login details!";
die(json_encode($response));
}
$query = "SELECT email, password, position FROM user WHERE email = :email ";
$query_params = array(':email' => $_POST['username'],);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one to product JSON data:
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
//This will be the variable to determine whether or not the user's information is correct.
//we initialize it as false.
$validated_info = false;
$login_ok = false;
//fetching all the rows from the query
$row = $stmt->fetch();
if ($row) {
//if we encrypted the password, we would unencrypt it here, but in our case we just
//compare the two passwords
if ($_POST['password'] === $row['password']) {
$login_ok = true;
}
// If the user logged in successfully, then we send them to the private members-only page
// Otherwise, we display a login failed message and show the login form again
if ($login_ok) {
$response["success"] = 1;
$response["message"] = "Login Successful!";
$response["posts"] = array();
foreach ($row as $rerow) {
$row = array(
$post["position"] = $rerow["position"]
);
array_push($response["posts"], $post);
}
die(json_encode($response));
}
else {
$response["success"] = 0;
$response["message"] = "Invalid Credentials!";
die(json_encode($response));
}
}
}
else {
?>
<h1>Login</h1>
<form action="login.php" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />
Password:<br />
<input type="password" name="password" placeholder="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>
<?php
}
?>
我不确定哪个地方我没有放近位。你能帮我看看吗?我不知道在哪里放近距离。在将我的数据检索到我的android时,是否与我的状况有关?如果是,请说出我知道我写的语法是否正确。
答案 0 :(得分:1)
缺少if (!empty($_POST)) {
if (!empty($_POST)) {
//code...
} else {
?>
<h1>Login</h1>
<form action="login.php" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />
Password:<br />
<input type="password" name="password" placeholder="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>
<?php
}
} // <---- this is missing
答案 1 :(得分:0)
如果可以的话,我想告诉你,你的代码有点混乱。压痕可以挽救你的生命,让人记住。
无论如何,如果您删除代码的第五行行,我发现没有错误:
if (!empty($_POST)) {
这未关闭。 if ($row) {
在登录标题标题上方的else
之前关闭。事实上,如果数据库没有返回任何结果,它会显示登录页面。
无论如何,第五行是不必要的,因为在它下方你可以控制特定的变量。您不必检查完整的$_POST
数组。