Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使表正确更新数据库中的otp代码?_Php_Mysql - Fatal编程技术网

Php 如何使表正确更新数据库中的otp代码?

Php 如何使表正确更新数据库中的otp代码?,php,mysql,Php,Mysql,我正在我的应用程序中添加otp验证。问题是当我点击注册我的应用程序或rest api时,会生成新代码,但当使用存在时,应用程序不会再次生成代码,并且数据库中不会有任何更新。我在这一部分没有什么经验,但这是我开发的,正如你所看到的,在代码中有一个更新stmt,我在没有更新的情况下添加了它,但毕竟这也不起作用 以下是创建用户时发生的事情: 注册=>如果不存在,创建otp并将电话和otp添加到数据库=>完成表单=>转到应用程序 注册=>如果存在,检查手机,更新otp=>转到应用程序 代码如下: 功能:

我正在我的应用程序中添加otp验证。问题是当我点击注册我的应用程序或rest api时,会生成新代码,但当使用存在时,应用程序不会再次生成代码,并且数据库中不会有任何更新。我在这一部分没有什么经验,但这是我开发的,正如你所看到的,在代码中有一个更新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发送,但我收到[]响应。

您的更新内容放错了位置

if($db->checkExistsUser($phone))
{
    $db->updateOtp($phone, $otp)
    // etc
}
else
{
    //Create new User
    $user = $db->registerNewOtp($phone,$otp);
    // etc
}
创建更新方法并移动更新代码。另外,update语句实际上并没有更新任何内容!将其更改为:

"UPDATE User SET otp = ? WHERE Phone = ?" 

i、 e.您需要绑定otp值

您正在检查用户是否存在,在此条件范围内,您不会在数据库中注册任何新记录!您只发送了SMS@hassan嗯,我在这方面没有太多经验,你能告诉我应该添加什么或者如何纠正我的错误吗?我试图创建另一个函数来更新otp,并在checkuserexist中调用它来更新它,但仍然无法工作。感谢您的回复。我已经更新了我的问题,我已经按照你说的做了。现在我在rest api中获得[],但数据库仍然没有更改。请检查我自己的更改。我可以更新它并收到短信,但我得到了[]的回复。你说
echo-json\u-encode($response)
但是$response在前面声明为空的arrayJeasus,我已经用
$response[“otp”]=$user[“otp”]修复了该部分
我得到了success echo,现在如何在
sendSms($phone,$user)
中使用
$user[“otp”]
?这是一个单独的问题;-)你能把我的答案标为正确吗?至于下一位,只需让sendSms方法接受3个参数,但将第三个参数默认为null<代码>函数sendSms($user,$phone,$otp=null)