的index.php
在这个服务中,我想通过POST方法在数据库中插入细节,但它给了我一些错误,所以请帮我解决它.. 这是我的代码 -
app->post('/createprovider', function () use ($app) {
verifyRequiredParams(array('fullname', 'email', 'mobile', 'password','business_name', 'email_work', 'phone_work', 'address', 'latitude', 'longitude', 'category'));
$response = array();
$fullname = $app->request->post('fullname');
$email = $app->request->post('email');
$mobile = $app->request->post('mobile');
$password = $app->request->post('password');
$business_name = $app->request->post('business_name');
$email_work = $app->request->post('email_work');
$phone_work = $app->request->post('phone_work');
$address = $app->request->post('address');
$latitude = $app->request->post('latitude');
$longitude = $app->request->post('longitude');
$category = $app->request->post('category');
$db = new DbOperation();
$res = $db->createprovider($fullname, $email, $mobile, $password, $business_name, $email_work, $phone_work, $address, $latitude, $longitude, $category);
if ($res == 0) {
$response["error"] = false;
$response["message"] = "You are successfully registered";
echoResponse(201, $response);
} else if ($res == 1) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registereing";
echoResponse(200, $response);
} else if ($res == 2) {
$response["error"] = true;
$response["message"] = "Sorry, this student already existed";
echoResponse(200, $response);
}
});
这是dboperation.php中的函数
public function createprovider($fullname,$email,$mobile,$pass,$business_name,$email_work,$phone_work,$address,$latitude,$longitude, $category){
if (!$this->isProviderExists($email)) {
$password = md5($pass);
//$apikey = $this->generateApiKey();
$stmt = $this->con->prepare("INSERT INTO nesbaty_provider(fullname, email, mobile, password, business_name, email_work, phone_work, address, latitude, longitude, category) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssss", $fullname, $email, $mobile, $password, $business_name, $email_work, $phone_work, $address, $latitude, $longitude, $category);
$result = $stmt->execute();
$stmt->close();
if ($result) {
return 0;
} else {
return 1;
}
} else {
return 2;
}
}
答案 0 :(得分:0)
PDO的execute()函数在成功时返回true
,在失败时返回false
。
您在createprovider
函数内的if语句中否定了这一点,从而引入了混淆。
所以你基本上已经和块相矛盾了,结果你的整体逻辑被破坏了,但你的代码需要做一些改变。
这应该可以正常工作:
$app->post(...
...
if ($res == 1) {
$response["error"] = false;
$response["message"] = "You are successfully registered";
echoResponse(201, $response);
} else if ($res == 0) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registereing";
echoResponse(200, $response);
} else if ($res == 2) {
$response["error"] = true;
$response["message"] = "Sorry, this student already existed";
echoResponse(200, $response);
}
...
然后在createprovider()
内:
...
$stmt->close();
if ($result) {
return 1;
} else {
return 0;
}
...
还有更多" RESTful"实施你不应该使用代码" 2XX"对于错误。