Json总是从PHP服务器返回null,但是,我的登录,重置和更改密码返回一个值。任何人都可以请帮助并告诉我如何找出JSON从PHP服务器返回null值的原因。
数据进入数据库,然而,当JSON从php服务器返回null值时,应用程序崩溃。我从Android端进行了调试,可以看到JSON总是返回一个空值,但是,这些字段确实有值,数据会进入数据库。
index.php
<?php
/*
PHP API for Login, Register Requests and for Email Notifications.
*/
require_once 'mailer.php';
if (isset($_POST['tag']) && $_POST['tag'] != '')
{
// Include Database handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $_POST['tag'], "success" => 0, "error" => 0);
// check for tag type
switch($_POST['tag'])
{
case 'register':
// Request type is Register new user
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$uname = $_POST['uname'];
$password = $_POST['password'];
// check if user is already existed
if ($db->isUserExisted($email))
{
// user is already existed - error response
$response["error"] = 2;
$response["error_msg"] = "User already existed";
}
else if(!$db->validEmail($email))
{
$response["error"] = 3;
$response["error_msg"] = "Invalid Email Id";
}
else
{
// store user
$user = $db->storeUser($fname, $lname, $email, $uname, $password);
if ($user)
{
// user stored successfully
$response["success"] = 1;
$response["user"]["fname"] = $user["firstname"];
$response["user"]["lname"] = $user["lastname"];
$response["user"]["email"] = $user["email"];
$response["user"]["uname"] = $user["username"];
$response["user"]["uid"] = $user["unique_id"];
$response["user"]["created_at"] = $user["created_at"];
$subject = "Registration";
$message = "Hello $fname,\n\nYou have successfully registered to our service.\n\nRegards,\nAdmin.";
$name = $user['firstname'] . ' ' . $user['lastname'];
send_email($subject, $message, $email, $name);
}
else
{
// user failed to store
$response["error"] = 1;
$response["error_msg"] = "JSON Error occured in Registartion";
}
}
break;
default:
$response["error"] = 3;
$response["error_msg"] = "JSON ERROR";
}
echo json_encode($response);
}
else
{
echo "BradVisor Login API";
}
?>
DB_Functions.PHP
/**
* Adding new user to mysqli database
* returns user details
*/
public function storeUser($fname, $lname, $email, $uname, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysqli_query($this->db,"INSERT INTO `users`(`unique_id`, `firstname`, `lastname`, `username`, `email`, `encrypted_password`, `salt`, `created_at`) VALUES('$uuid', '$fname', '$lname', '$uname', '$email', '$encrypted_password', '$salt', NOW())") or die(mysqli_error($db));
// check for successful store
if ($result) {
// get user details
$uid = mysqli_insert_id($this->db); // last inserted id
$result = mysqli_query($this->db, "SELECT * FROM users WHERE uid = $uid") or die(mysqli_error($this->db));
// return user details
return mysqli_fetch_array($result);
} else {
return false;
}
}