我将这个PHP文件存储在服务器中。它为数据库创建了一个新用户。注册成功但响应消息始终为NULL。
这是我的register.php文件,其中我发布值
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (($_POST['name']) && ($_POST['surname']) && ($_POST['email']) && ($_POST['password']) && ($_POST['telephone'] && ($_POST['country']) ) )
{
// receiving the post params
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$password = $_POST['password'];
$telephone = $_POST['telephone'];
$country = $_POST['country'];
// check if user is already existed with the same email
if ($db->isOwnerExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already exists with " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeOwner($name, $surname, $email, $password, $telephone, $country);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["oid"] = $user["oid"];
$response["user"]["name"] = $user["name"];
$response["user"]["surname"] = $user["surname"];
$response["user"]["country"] = $user["country"];
$response["user"]["email"] = $user["email"];
$response["user"]["password"] = $user["password"];
$response["user"]["telephone"] = $user["telephone"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters are missing!";
echo json_encode($response);
}
?>
还有storeOwner函数
public function storeOwner($name, $surname, $email, $password, $telephone, $country) {
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$stmt = $this->conn->prepare("INSERT INTO owner (oid, name, surname, country, email, password, salt, telephone) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("isssssss", $oid, $name, $surname, $country, $email, $encrypted_password, $salt, $telephone);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM owner WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->bind_result($user['oid'], $user['name'], $user['surname'], $user['country'], $user['email'], $user['password'], $user['salt'], $user['telephone']);
while ($stmt->fetch()) {
//printf("%s %s\n", $email, $password);
}
$stmt->close();
return $user;
} else {
return false;
}
}
输出类似于
{ “错误”:假的, “UID”:空, “用户”:{ “名”:空, “姓”:空, “国”:空, “电子邮件”:空, “密码”:空, “电话”:空}}
为什么每个字段都为空?
答案 0 :(得分:6)
当您提取用户时,您当前正在使用该方法的响应覆盖绑定结果:
$user = $stmt->bind_result($user['oid'], ...);
方法$stmt->bind_result()
返回一个布尔值(成功时为true,错误时为false)。所以你的代码首先设置值,当它完成时,它会用方法的结果(布尔值)覆盖它们。
应该是:
$user = []; // You should create the array before using it.
$stmt->bind_result($user['oid'], ...);