【发布时间】:2015-02-19 06:36:33
【问题描述】:
这是我的简单纤薄应用程序。下面的这个函数处理插入。
public function createSelfie($user_id, $image, $latitude, $longitude) {
$stmt = $this->conn->prepare("INSERT INTO user_selfie(image, latitude, longitude, user_id) VALUES(?,?,?,?)");
$stmt->bind_param("sssi", $image, $latitude, $longitude, $user_id);
$result = $stmt->execute();
$stmt->close();
if ($result) {
// we were successful in storing the selfie now we need to manipulate the 'user_egg' table values
return SELFIE_CREATED_SUCCESSFULLY;
}
else {
// selfie failed to create
return NULL;
}
}
为此,路线如下:
$app->post('/selfie/create', function() use ($app) {
verifyRequiredParams(array('image', 'latitude', 'longitude', 'user_id'));
// reading post params
$image = $app->request()->post('image');
$latitude = $app->request()->post('latitude');
$longitude = $app->request()->post('longitude');
$user_id = $app->request()->post('user_id');
$response = array();
$db = new DbHandler();
$res = $db->createSelfie($user_id, $image, $latitude, $longitude);
if ($res == SELFIE_CREATED_SUCCESSFULLY) {
$response['error'] = false;
$response["message"] = "Your selfie created successfully";
echoRespnse(201, $response);
}
else {
$response['error'] = true;
$response["message"] = "There was an error storing your selfie try again";
echoRespnse(200, $response);
}
});
现在有时它会正确响应,有时会为空。但是每次该语句确实被执行时,我都会看到插入的行。然后我很困惑为什么函数返回false。可能是什么原因?如果我们无法知道原因,使用 afftected_rows 而不是结果是一种好习惯吗?附带说明一下,我确实有一个 echoRespnse 方法,它 json 编码结果并将响应作为 json 发送。
【问题讨论】: