Jump to content

Dule95D

Members
  • Content Count

    12
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Dule95D

  • Rank
    Member
  1. Now it's working, but the converted string from byte is not decoded and stored in folder uploads. Any suggestions? I mean image which is converted to string, is not decoded at insert statement and stored in folder uploads which i have created.
  2. So i should add one more parameter in my function? And then insert passed parameter. But how will i passed url path as parameter?
  3. public function uploadImage($user_id, $image, $status) { $num_affected_rows = 0; $upload_dir = __DIR__.'/uploads'; if ($stmt = $this->conn->prepare("UPDATE users SET image = '$upload_dir', status = ? WHERE id = ?")) { $stmt->bind_param("sii", $image, $status, $user_id); // here i'm getting an error $result = $stmt->execute(); $num_affected_rows = $stmt->affected_rows; $stmt->close(); if ($result) { file_put_contents($actualpath, base64_decode($image)); } } else { var_dump($this->conn->error); } return $num_affected_rows > 0; } Okay, i have changed that. But now i'm getting a new error: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement
  4. I have fixed that and now i'm getting this error: <b>Fatal error</b>: Call to a member function bind_param() on boolean in on this line: $stmt->bind_param("sii", $image, $status, $id);
  5. Hello again PHP freaks, I have an android application which is using API i'm creating for connecting to the server. Now i'm stuck with uploading profile image to the existing record. So this is the activity process of application. User register ----> New record is stored in database Now user wants to upload profile image, so the record should to be updated ----> get the id of the user that wants to upload profile image, and upload it. This is the code what i have done so far: public function uploadImage($user_id, $image, $status) { $path = "uploads/$user_id.png"; $actualpath = "http://localhost:8081/timster/uploads/$path"; $stmt = $this->conn->prepare("UPDATE users SET image = $actualpath, status = ? WHERE id = ?"); file_put_contents($path, base64_decode($image)); $stmt->bind_param("sii", $image, $status, $id); $stmt->execute(); $num_affected_rows = $stmt->affected_rows; $stmt->close(); return $num_affected_rows > 0; } And here i'm sending a PUT request for updating user record: $app->put('/image/:id', 'authenticate', function($user_id) use ($app) { // check for required params verifyRequiredParams(array('image', 'status')); global $user_id; $image = $app->request->put('image'); $status = $app->request->put('status'); $db = new DbHandler(); $response = array(); $result = $db->uploadImage($user_id, $image, $status); if ($result) { $response["error"] = false; $response["message"] = "Image uploaded successfully"; } else { $response["error"] = true; $response["message"] = "Image failed to upload"; } echoRespnse(200, $response); }); This is the error message i'm getting: file_put_contents(uploads/1.png): failed to open stream: No such file or directory
  6. Sorry, but i can't get it to work.
  7. 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.
  8. 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); });
  9. 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?
  10. 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?
  11. 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.
×
×
  • 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.