gpt4 book ai didi

php - 登录系统: $_POST ['username' ] and $_POST ['password' ] are always empty

转载 作者:行者123 更新时间:2023-11-29 07:40:19 25 4
gpt4 key购买 nike

首先我要说的是,我在 Google 上无休止地搜索帮助,并且在过去的 x 小时内确实花费了调试相同的错误,但我就是无法弄清楚。

我正在关注this tutorial关于如何为我的 Android 应用程序创建登录系统。当我在 Genymotion 上运行我的应用程序时,我可以输入我的登录凭据,但是一旦我点击登录按钮,我的应用程序就会崩溃。我在 Debug模式下再次运行我的应用程序,其原因是由于以下异常:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference

如果我错了,请纠正我,但我认为发生的情况是我的 JSONParser 正在尝试解析一个空对象。这可能是因为我的 PHP 文件总是返回空,我不知道为什么会这样。

 if(isset($_POST['username'])) {
$password=$_POST["username"];
}
if(isset($_POST['password'])) {
$password=$_POST["password"];
}

if (!empty($_POST))
{
if (empty($_POST['username']) || empty($_POST['password']))
{
// Create some data that will be the JSON response
$response["success"] = 0;
$response["message"] = "One or both of the fields are empty .";

//die is used to kill the page, will not let the code below to be executed. It will also
//display the parameter, that is the json data which our android application will parse to be
//shown to the users

die(json_encode($response));
}

$query = " SELECT * FROM login WHERE username = '$username'and password='$password'";

$sql1=mysql_query($query);
$row = mysql_fetch_array($sql1);

if (!empty($row))
{
$response["success"] = 1;
$response["message"] = "You have been sucessfully login";
die(json_encode($response));
}

else
{
$response["success"] = 0;
$response["message"] = "invalid username or password ";
die(json_encode($response));

}
}

else
{
$response["success"] = 0;
$response["message"] = " One or both of the fields are empty ";
die(json_encode($response));
}

最佳答案

我认为您忘记设置 JSON header 。如果您返回 JSON ,您需要像这样更改代码

$data = $response;
header('Content-Type: application/json');
echo json_encode($data);

关于php - 登录系统: $_POST ['username' ] and $_POST ['password' ] are always empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com