即使插入了数据,MySqli语句也会返回false

时间:2015-02-19 06:36:34

标签: php mysqli slim

这是我简单的超薄应用程序。下面的这个函数处理插入。

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。

1 个答案:

答案 0 :(得分:0)

而不是"返回SELFIE_CREATED_SUCCESSFULLY;" 尝试

return true;