REST Api Structure可防止数据库访问

时间:2016-04-11 07:41:38

标签: php android mysql sql rest

我托管我的文件hostinger.in并正在实施REST架构。当我在没有REST的情况下实现代码时,我没有收到任何错误。但现在看来我无法访问MySql数据库。我缩小了当我尝试注册用户时可能出现的错误,而且似乎数据库无法访问。以下是代码:

  public function sendOtp($mobile, $model, $brand, $device, $fingerprint, $hardware){

    //Checking for User registration
    if (!$this->isUserExists($mobile)) {

       $otp = rand(100000, 999999);

        $stmt = $this->con->prepare("INSERT INTO user_register(mobile, registered_mobile_model, registered_mobile_brand, registered_mobile_device, registered_mobile_fingerprint, registered_mobile_hardware, otp) values(?, ?, ?, ?, ?, ?, ?)");

        $stmt->bind_param("sssssss", $mobile, $model, $brand, $device, $fingerprint, $hardware, $otp);

        $result = $stmt->execute();

        $stmt->close();

        //If statment executed successfully
        if ($result) {
            //Returning 0, otp sent
            return 0;
        } else {
            //Returning 1, failed to insert
            return 1;
        }

    } else {
        //user exists, update info and sent otp 


        $otp = rand(100000, 999999);

        //Crating an statement
        $stmt = $this->con->prepare("UPDATE user_register set otp = ?, registered_mobile_model = ?, registered_mobile_brand = ?, registered_mobile_device = ?, registered_mobile_fingerprint = ?, registered_mobile_hardware = ?  WHERE mobile = ?");

        //Binding the parameters
        $stmt->bind_param("sssssss", $otp, $model, $brand, $device, $fingerprint, $hardware, $mobile);

        //Executing the statement
        $result = $stmt->execute();

        //Closing the statement
        $stmt->close();

        //If statement executed successfully
        if ($result) {
            //Returning 0, user insert/update success, otp sent
            return 0;
        } else {
            //Returning 1, failed to insert
            return 1;
        }



    }
}


  private function isUserExists($mobile) {
    $stmt = $this->con->prepare("SELECT id from user_register WHERE mobile = ?");
    $stmt->bind_param("s", $mobile);
    $stmt->execute();
    $stmt->store_result();
    $num_rows = $stmt->num_rows;
    $stmt->close();
    return $num_rows > 0;
}

重新路由api调用的index.php代码:

     $app->post('/sendotp', function () use ($app) {

//Verifying the required parameters
verifyRequiredParams(array('mobile', 'model', 'brand', 'device', 'fingerprint', 'hardware'));

//Creating a response array
$response = array();

//reading post parameters
$mobile = $app->request->post('mobile');
$model = $app->request->post('model');
$brand = $app->request->post('brand');
$device = $app->request->post('device');
$fingerprint = $app->request->post('fingerprint');
$hardware = $app->request->post('hardware');

//Creating a DbOperation object
$db = new DbOperation();

$res = $db->sendOtp($mobile, $model, $brand, $device, $fingerprint, $hardware);

//If the result returned is 0 means success
if ($res == 0) {
    //Making the response error false
    $response["error"] = false;
    //Adding a success message
    $response["message"] = "OTP Sent";
    //Displaying response
    echoResponse(201, $response);

//If the result returned is 1 means failure
} else if ($res == 1) {
    $response["error"] = true;
    $response["message"] = "Oops! An error occurred while registering";
    echoResponse(200, $response);

}
});

0 个答案:

没有答案