Jump to content

Restful Api


Go to solution Solved by Jacques1,

Recommended Posts

Hi everyone,

 

I'm new here and i'm also new with coding in PHP. I have written one Restful Api based on one tutorial and i need Restful Api for connecting my Android application to server. I need some help with making this to work. I need to update user details.

This is the code i'm using:

class DbHandler {

public function updateUser($id, $name) {
        $stmt = $this->conn->prepare("UPDATE users SET name = ? WHERE id = ?");
        $stmt->bind_param("is", $id, $name);
        $stmt->execute();
        $num_affected_rows = $stmt->affected_rows;
        $stmt->close();
        return $num_affected_rows > 0;

 }
}
 
This is preparation statement for updating a existing user.
 
Now here is a fragment of code from 
index.php
 where i'm calling all methods:
 
$app->put('/user/:id', 'authenticate', function($id) use($app) {


verifyRequiredParams(array('name'));


global $id;
$name = $app->request->put('name');


$db = new DbHandler();
$response = array();


$result = $db->updateUser($id, $name);
if ($result) {
$response["error"] = false;
$response["message"] = "User updated successfully";
} else {
$response["error"] = true;
$resopnse["message"] = "User failed to update. Please try again";
}
echoRespnse(200, $response);
});

When i try to send request, i'm getting only this:

{
  "error": true
}

 

 Any help would be appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/301127-restful-api/
Share on other sites

This is the function for echoing the response:

function echoRespnse($status_code, $response) {
    $app = \Slim\Slim::getInstance();
    // Http response code
    $app->status($status_code);

    // setting response content type to json
    $app->contentType('application/json');

    echo json_encode($response);
}

I'm not sure what did you mean exactly? 

Link to comment
https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532657
Share on other sites

I can see i have misspelled function, but i can't change it right now. I'm using text editor and i don't have refactoring option there. 

Well i'm not worried because it's not displaying the error message too. I'm worried because i can't update a user. I guess that returning value somewhere is null and maybe because of that i'm getting always error = true;

 

Is my method in DbHandler class okay?

Edited by Dule95D
Link to comment
https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532659
Share on other sites

Your question didn't say what was wrong. I assume because you stored the message in a non-existent var that the problem was simply a missing message.

 

You could turn on php error checking and see if you any other typos that are causing your failure. See my signature.

Link to comment
https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532660
Share on other sites

The problem is that i'm not getting successful result while i'm sending a request. I'm getting always else condition that there is some error. The method i have posted, should update user details = name. 

 

For example i have another method which is doing a great job and it is almost the similar code and it is working:

$app->put('/groups/:id', 'authenticate', function($group_id) use($app) {
            // check for required params
            verifyRequiredParams(array('group_name', 'status'));

            global $user_id;            
            $group = $app->request->put('group_name');
            $status = $app->request->put('status');

            $db = new DbHandler();
            $response = array();

            // updating group
            $result = $db->updateGroup($user_id, $group_id, $group, $status);
            if ($result) {
                // group updated successfully
                $response["error"] = false;
                $response["message"] = "Group updated successfully";
            } else {
                // group failed to update
                $response["error"] = true;
                $response["message"] = "Group failed to update. Please try again!";
            }
            echoRespnse(200, $response);
        });
Link to comment
https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532661
Share on other sites

  • Solution

The order of your parameters is wrong: In the bind_param() call, you assume it's (ID, name), but in the query it's actually (name, ID). So you need

$stmt->bind_param("si", $name, $id);

Using the number of affected rows as an error condition is also a bad idea, because it's perfectly normal for an UPDATE query to have no effect (e. g. when a previous process has already updated the value). At best, you'd include the affected rows as informational data. To get real errors, enable error reporting for MySQLi before you establish the database connection:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Now you'll get a mysqli_exception whenever a query fails. If you leave the exception alone (meaning: you don't catch it), PHP will automatically emit a 500 response code to signal an error.

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532670
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.