Dule95D Posted March 31, 2016 Share Posted March 31, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/ Share on other sites More sharing options...
ginerjm Posted March 31, 2016 Share Posted March 31, 2016 Are these spelled correctly? $resopnse["message"] = "User failed to update. Please try again"; } echoRespnse(200, $response); Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532656 Share on other sites More sharing options...
Dule95D Posted March 31, 2016 Author Share Posted March 31, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532657 Share on other sites More sharing options...
ginerjm Posted March 31, 2016 Share Posted March 31, 2016 Well, your misspelled function name is not a problem since you matched the name to the call. But what about the other line I posted? Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532658 Share on other sites More sharing options...
Dule95D Posted March 31, 2016 Author Share Posted March 31, 2016 (edited) 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 March 31, 2016 by Dule95D Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532659 Share on other sites More sharing options...
ginerjm Posted March 31, 2016 Share Posted March 31, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532660 Share on other sites More sharing options...
Dule95D Posted March 31, 2016 Author Share Posted March 31, 2016 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); }); Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532661 Share on other sites More sharing options...
ginerjm Posted March 31, 2016 Share Posted March 31, 2016 You don't check if your query actually executed - poor programming. did you turn on error checking? Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532662 Share on other sites More sharing options...
Dule95D Posted March 31, 2016 Author Share Posted March 31, 2016 You can see that i'm still learning and trying to make something new and improve my skills. I'm checking if query is executed in DbHandler class. I have posted error checking at the top of my code, but i'm not sure how should i use it. Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532663 Share on other sites More sharing options...
ginerjm Posted March 31, 2016 Share Posted March 31, 2016 If you posted my sample at the beginning of this script you will see errors as they occur Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532664 Share on other sites More sharing options...
Dule95D Posted March 31, 2016 Author Share Posted March 31, 2016 Sorry, but i can't get it to work. Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532665 Share on other sites More sharing options...
Solution Jacques1 Posted April 1, 2016 Solution Share Posted April 1, 2016 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532670 Share on other sites More sharing options...
Dule95D Posted April 1, 2016 Author Share Posted April 1, 2016 Solved it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/301127-restful-api/#findComment-1532683 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.