我正在努力在我的应用程序中添加otp验证。问题是,当我单击注册时,我的应用程序或rest api将生成新代码,但是当存在使用时,应用程序将不会再次生成它,并且我的数据库中没有更新。我在本节中没有真正的经验,但是这是我开发的,因为您可以看到在没有更新的情况下我已添加了代码中的Update stmt,但毕竟这也没有工作。
以下是创建用户的过程:
注册=>如果不存在,请创建otp并将电话和otp添加到数据库=>完整表格=>转到应用。
注册=>如果存在,请检查电话,更新otp =>转到应用
以下是代码:
功能:
function checkExistsUser($phone)
{
$stmt = $this->conn->prepare("SELECT * FROM User WHERE Phone=?");
$stmt->bind_param("s",$phone);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) {
$stmt->close();
return true;
}else{
$stmt->close();
return false;
}
}
public function verifyOtp($otp)
{
$stmt = $this->conn->prepare("SELECT otp FROM User WHERE otp= ?");
$stmt->bind_param("i",$otp);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0) {
$stmt->close();
return true;
}else{
$stmt->close();
return false;
}
public function registerNewOtp($phone,$otp)
{
$stmt = $this->conn->prepare("INSERT INTO User(Phone,otp) VALUES (?, ?)");
$stmt->bind_param("si", $phone,$otp);
$result = $stmt->execute();
$stmt->close();
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM User WHERE Phone = ?");
$stmt->bind_param("s", $phone);
$stmt->execute();
$stmt2 = $this->conn->prepare("UPDATE User SET otp WHERE Phone = ?");
$stmt2->bind_param("i", $updateotp);
$result = $stmt2->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else
return false;
}
register.php
$response = array();
if(isset($_POST['phone']))
{
$phone = $_POST['phone'];
$otp = rand(100000, 999999);
if($db->checkExistsUser($phone))
{
$db->updateOtp($phone, $otp);
echo json_encode($response);
$response["otp_created"] = "User already existed with " . $phone . " New Code IS $otp";
sendSms($phone, $otp);
}
else
{
//Create new User
$user = $db->registerNewOtp($phone,$otp);
if($user)
{
$response['phone'] = $user["Phone"];
echo json_encode($response);
// send sms
sendSms($phone, $otp);
}
else
{
$response["error_msg"] = "Unkown error Occuired in registration!";
echo json_encode($response);
}
}
}
else{
$response["error_msg"]= "Required parameter (phone) is missing!";
echo json_encode($response);
}
UpdateOtp函数:
public function updateOtp($phone, $otp)
{
$stmt = $this->conn->prepare("UPDATE `User` SET `otp` = ? WHERE Phone = ?") or die($this->conn->error);
$stmt->bind_param("si", $phone,$otp);
$result = $stmt->execute();
return $result;
}
现在我得到[]的回应。
我的更改:
Functions :
public function getOtpRecord($phone)
{
$stmt = $this->conn->prepare("SELECT otp FROM User WHERE Phone=?");
$stmt->bind_param("s",$phone);
if($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else
return NULL;
}
public function updateOtp($phone)
{
$otp = rand(100000, 999999);
$stmt = $this->conn->prepare("UPDATE `User` SET `otp` = $otp WHERE Phone = ?") or die($this->conn->error);
$stmt->bind_param("s", $phone);
$result = $stmt->execute();
return $result;
}
In register :
if($db->checkExistsUser($phone))
{
if ($db->updateOtp($phone))
{
$user = $db->getOtpRecord($phone);
if($user)
{
$otp = $user["otp"];
echo json_encode($response);
sendSms($phone, $otp);
}
else
{
$response["error_msg"] = "User does not exists!";
echo json_encode($response);
}
$response["otp_created"] = "User already existed with " .$phone . " New Code IS $otp";
}
}
随着我的更改,数据库将更新,并且短信将以正确的otp发送,但我得到了[]响应。
答案 0 :(得分:0)
您的更新内容放置在错误的位置。
if($db->checkExistsUser($phone))
{
$db->updateOtp($phone, $otp)
// etc
}
else
{
//Create new User
$user = $db->registerNewOtp($phone,$otp);
// etc
}
创建一个更新方法并移动您的更新代码。另外,更新语句实际上并没有更新任何内容!更改为:
"UPDATE User SET otp = ? WHERE Phone = ?"
即您需要绑定otp值!