我正在尝试为 Android 应用程序创建登录 php,我从教程中修改了此 php,问题是我过去总是得到“成功”= 0“消息”=“并非所有字段都已填充”的响应,所以我添加了一些 print_r 来看看问题出在哪里,现在我只得到这个结果 aimatosnintendo ,其中是用户名和密码的输入,所以它甚至没有到达 ifs,这是我的代码:
<?php
// array for JSON response
$response = array();
define('DB_USER', ""); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', ""); // database name
define('DB_SERVER', ""); // db server
// array for JSON response
$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE);
// check for post data
print_r ($_POST['username']);
print_r ($_POST['password']);
if(isset($_POST['username'],$_POST['password'])) {
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT *FROM login WHERE username = $username AND password = $password";
$result = $conn->query($sql) or die (mysqli_connect_error());
print_r ($username);
if (!empty($result)) {
// check for empty result
if (mysqli_num_rows($result) > 0) {
$result = mysqli_fetch_array($result);
$loginfo = array();
$loginfo["name"] = $result["name"];
$loginfo["username"] = $result["username"];
$loginfo["password"] = $result["password"];
$loginfo["phone"] = $result["phone"];
$loginfo["email"] = $result["email"];
$loginfo["license"] = $result["license"];
//$loginfo["expiration"] = $result["expiration"];
// success
$response["success"] = 1;
// user node
$response["logina"] = array();
array_push($response["logina"], $loginfo);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "Wronglogin";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "Wronglogin";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Not all fields are filled";
// echoing JSON response
echo json_encode($response);
}
?>
数据库用户密码服务器和名称都是正确的,为了安全起见,我只是在这篇文章中删除了它们,我有一个在数据库上注册信息的代码,它可以工作,但这个让我陷入困境
我期待 $response["success"] = 1;当用户名和密码与数据库匹配时
最佳答案
尝试
$sql = "SELECT * FROM login WHERE username = '". $username ."' AND password = '".$password."'";
关于PHP登录代码不从数据库读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39110756/