我在我的 .php 脚本中找不到错误。
我使用一个发送帖子请求的 android 应用程序来注册用户,包括唯一的设备 ID 和电子邮件。
发送数据时,我的 api 检查电子邮件或设备 ID 是否已存在,因此您无法注册两次。
第一次注册时,一切正常。当我尝试使用相同的电子邮件再次注册时,它也可以正常工作(得到正确的错误)。但是,如果我使用不同的电子邮件(但设备 ID 相同),我会收到错误的错误代码。
这是PHP代码:
else if ($tag == 'register') {
// Request type is Register new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$devid = $_POST['devid'];
// check if user already exists
if ($db->CheckUser($email)) {
// user already exists
$response["error"] = 2;
$response["error_msg"] = "User already exists";
echo json_encode($response);}
else if ($db->CheckDevice($devid)) {
// Device already exists
$response["error"] = 3;
$response["error_msg"] = "Device already exists";
echo json_encode($response);}
else {
// store user
$user = $db->storeUser($name, $email, $password, $devid);
if ($user) {
// user stored successfully
$response["success"] = 1;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["devid"] = $user["devid"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);}
else {
// user failed to store
$response["error"] = 1;
$response["error_msg"] = "Error occured in Registration";
echo json_encode($response);}
}
}
检查功能:
public function CheckUser($email) {
$result = mysql_query("SELECT email from users WHERE email = '$email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;}
else {
// user not existed
return false;}
}
public function CheckDevice($devid) {
$result = mysql_query("SELECT devid from users WHERE devid = '$devid'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
存储功能:
public function storeUser($name, $email, $password, $devid) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, devid, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', '$devid', NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
我正在发送
我应该得到这个错误:
$response["error"] = 3;
$response["error_msg"] = "Device already exists";
但我得到了这个:
$response["error"] = 1;
$response["error_msg"] = "Error occured in Registration";
不知何故,在调用CheckDevice
函数时它似乎返回了,false
尽管当我SELECT devid from users WHERE devid = '$devid'
在 phpMyAdmin 中手动使用时,我得到了一个true
.
然后他无法存储,因为设备 ID 必须是唯一的并且给了我错误(这是唯一合理的解释)。