I’ve almost completed this Android App project before I realized that I need to upload the products with pictures. Subscribers will have to register the product with image and I’ll have to upload it using REST API in android studio.
I just started using Slim Framework and it is just the best for me and I want to learn more about it. I’m just a beginner.
Below is my code without images and please I need help to learn how to add images to it.
Below is my “DbOperationsProperties.php”
<?php
class PropertyDetails{
private $con;
function __construct(){
require_once dirname(__FILE__) . '/DbConnect.php';
$db = new DbConnect;
$this->con = $db->connect();
}
// --------------------------------------------------------------------
public function createUserPrDt($personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription){
if(!$this->isPlotNumberExist($plotNumber)){
$stmt = $this->con->prepare("INSERT INTO property_register_propdetails (personalIdCode, propertyType, propertyLocation,
plotNumber, propertySize, propertyTitle, propertyPurpose, propertyNetPrice,
propertyDescription) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssss", $personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription);
if($stmt->execute()){
return USER_CREATED;
}else{
return USER_FAILURE;
}
}
return USER_EXISTS;
}
// ---------------------------------------------------------------------
public function getAllUsersPrDt(){
$stmt = $this->con->prepare("SELECT id, personalIdCode, propertyType, propertyLocation, plotNumber, propertySize,
propertyTitle, propertyPurpose, propertyNetPrice, propertyDescription
FROM property_register_propdetails;");
$stmt->execute();
$stmt->bind_result($id, $personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription);
$users = array();
while($stmt->fetch()){
$user = array();
$user['id'] = $id;
$user['personalIdCode']=$personalIdCode;
$user['propertyType'] = $propertyType;
$user['propertyLocation'] = $propertyLocation;
$user['plotNumber']=$plotNumber;
$user['propertySize'] = $propertySize;
$user['propertyTitle'] = $propertyTitle;
$user['propertyPurpose'] = $propertyPurpose;
$user['propertyNetPrice'] = $propertyNetPrice;
$user['propertyDescription'] = $propertyDescription;
array_push($users, $user);
}
return $users;
}
// ----------------------------------------------------------------------
public function getUserPrDtByPlotNumber($plotNumber){
$stmt = $this->con->prepare("SELECT id, personalIdCode, propertyType, propertyLocation, plotNumber, propertySize,
propertyTitle, propertyPurpose, propertyNetPrice, propertyDescription
FROM property_register_propdetails WHERE plotNumber = ?");
$stmt->bind_param("s", $plotNumber);
$stmt->execute();
$stmt->bind_result($id, $personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription);
$stmt->fetch();
$user = array();
$user['id'] = $id;
$user['personalIdCode']=$personalIdCode;
$user['propertyType'] = $propertyType;
$user['propertyLocation'] = $propertyLocation;
$user['plotNumber']=$plotNumber;
$user['propertySize'] = $propertySize;
$user['propertyTitle'] = $propertyTitle;
$user['propertyPurpose'] = $propertyPurpose;
$user['propertyNetPrice'] = $propertyNetPrice;
$user['propertyDescription'] = $propertyDescription;
return $user;
}
// -------------------------------------------------------------
public function updateUsersPrDt($personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription, $id){
$stmt = $this->con->prepare("UPDATE property_register_propdetails SET personalIdCode = ?, propertyType = ?,
propertyLocation = ?, plotNumber = ?, propertySize = ?, propertyTitle = ?,
propertyPurpose = ?, propertyNetPrice = ?, propertyDescription = ? WHERE id = ?");
$stmt->bind_param("sssssssssi", $personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription, $id);
if($stmt->execute())
return true;
return false;
}
// -------------------------------------------------------------------
public function deleteUserPrDt($id){
$stmt = $this->con->prepare("DELETE FROM property_register_propdetails WHERE id = ?");
$stmt->bind_param("i", $id);
if($stmt->execute())
return true;
return false;
}
// -------------------------------------------------------------------------------
private function isPlotNumberExist($plotNumber){
$stmt = $this->con->prepare("SELECT id FROM property_register_propdetails WHERE plotNumber = ?");
$stmt->bind_param("s", $plotNumber);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
}
Below is my “index.php”
<?php
$app->post('/createuserprdt', function(Request $request, Response $response){
if(!haveEmptyParameters(array('personalIdCode', 'propertyType', 'propertyLocation', 'plotNumber', 'propertySize',
'propertyTitle', 'propertyPurpose', 'propertyNetPrice', 'propertyDescription'), $request, $response)){
$request_data = $request->getParsedBody();
$personalIdCode = $request_data['personalIdCode'];
$propertyType = $request_data['propertyType'];
$propertyLocation = $request_data['propertyLocation'];
$plotNumber = $request_data['plotNumber'];
$propertySize = $request_data['propertySize'];
$propertyTitle = $request_data['propertyTitle'];
$propertyPurpose = $request_data['propertyPurpose'];
$propertyNetPrice = $request_data['propertyNetPrice'];
$propertyDescription = $request_data['propertyDescription'];
$db = new PropertyDetails;
$result = $db->createUserPrDt($personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription);
if($result == USER_CREATED){
$message = array();
$message['error'] = false;
$message['message'] = 'Form subitted successfully';
$response->write(json_encode($message));
return $response
->withHeader('content-type', 'application/json')
->withStatus(201);
}else if($result == USER_FAILURE){
$message = array();
$message['error'] = true;
$message['message'] = 'Some error occurred';
$response->write(json_encode($message));
return $response
->withHeader('content-type', 'application/json')
->withStatus(422);
}else if($result == USER_EXISTS){
$message = array();
$message['error'] = true;
$message['message'] = 'Error...!!! Plot Number Already Exist';
$response->write(json_encode($message));
return $response
->withHeader('content-type', 'application/json')
->withStatus(422);
}
}
return $response
->withHeader('content-type', 'application/json')
->withStatus(422);
});
// ----------------------------------------------------------------
$app->get('/allusersprdt', function(Request $request, Response $response){
$db = new PropertyDetails;
$users = $db->getAllUsersPrDt();
$response_data = array();
$response_data['error'] = false;
$response_data['users'] = $users;
$response->write(json_encode($response_data));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
});
// ------------------------------------------------------------------
$app->get('/getuserprdt/{plotNumber}', function(Request $request, Response $response, array $args){
$plotNumber = $args['plotNumber'];
$db = new PropertyDetails;
$users = $db->getUserPrDtByPlotNumber($plotNumber);
$response_data = array();
$response_data['error'] = false;
$response_data['users'] = $users;
$response->write(json_encode($response_data));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
});
// -------------------------------------------------------------------
$app->put('/updateusersprdt/{id}', function(Request $request, Response $response, array $args){
$id = $args['id'];
if(!haveEmptyParameters(array('personalIdCode', 'propertyType', 'propertyLocation', 'plotNumber', 'propertySize',
'propertyTitle', 'propertyPurpose', 'propertyNetPrice', 'propertyDescription', 'id'), $request, $response)){
$request_data = $request->getParsedBody();
$personalIdCode = $request_data['personalIdCode'];
$propertyType = $request_data['propertyType'];
$propertyLocation = $request_data['propertyLocation'];
$plotNumber = $request_data['plotNumber'];
$propertySize = $request_data['propertySize'];
$propertyTitle = $request_data['propertyTitle'];
$propertyPurpose = $request_data['propertyPurpose'];
$propertyNetPrice = $request_data['propertyNetPrice'];
$propertyDescription = $request_data['propertyDescription'];
$id = $request_data['id'];
$db = new PropertyDetails;
if($db->updateUsersPrDt($personalIdCode, $propertyType, $propertyLocation, $plotNumber, $propertySize,
$propertyTitle, $propertyPurpose, $propertyNetPrice, $propertyDescription, $id)){
$response_data = array();
$response_data['error'] = false;
$response_data['message'] = 'User Update Successfully';
// $user = $db->getUserPrDtByPlotNumber($plotNumber);
// $response_data['user'] = $user;
$response->write(json_encode($response_data));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
}else{
$response_data = array();
$response_data['error'] = true;
$response_data['message'] = 'Please try again later';
// $user = $db->getUserPrDtByPlotNumber($plotNumber);
// $response_data['user'] = $user;
$response->write(json_encode($response_data));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
}
}
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
});
// ---------------------------------------------------------------------------
$app->delete('/deleteuserprdt/{id}', function(Request $request, Response $response, array $args){
$id = $args['id'];
$db = new PropertyDetails;
if($db->deleteUserPrDt($id)){
$response_data['error'] = false;
$response_data['message'] = 'User has been deleted';
}else{
$response_data['error'] = true;
$response_data['message'] = 'Please try again later';
}
$response->write(json_encode($response_data));
return $response
->withHeader('content-type', 'application/json')
->withStatus(200);
});
function haveEmptyParameters($required_params, $request, $response){
$error = false;
$error_params = '' ;
$request_params = $request->getParsedBody();
foreach($required_params as $param){
if(!isset($request_params[$param]) || strlen($request_params[$param])<=0){
$error = true;
$error_params .= $param . ', ';
}
}
if($error){
$error_detail = array();
$error_detail['error'] = true;
$error_detail['message'] = 'Required parameters ' . substr($error_params, 0, -2) . ' are missing or empty';
$response->write(json_encode($error_detail));
}
return $error;
}
$app->run();
BELOW is my “Constant.php”
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'stanbaize');
define('USER_CREATED', 101);
define('USER_EXISTS', 102);
define('USER_FAILURE', 103);
define('USER_AUTHENTICATED', 201);
define('USER_NOT_FOUND', 202);
define('USER_PASSWORD_DO_NOT_MATCH', 203);
define('PASSWORD_CHANGED', 301);
define('PASSWORD_DO_NOT_MATCH', 302);
define('PASSWORD_NOT_CHANGED', 303);
I use Postman to run my check. this code is working, and like I said, I need to add three to four pictures to the form. I really need help on this.