I'm fairly new to php and mySQL. I'm writing a program where I need to occupy and free spots, the way I did it is I made a table called "spaces" it has the "id" and "occupied_by"(users who occupy this spot) as attributes. I set the deafult value for "occupied_by" to NULL, so this way I will know if the space is occupied or not. I wrote the code that occupies Spots and it works just fine. But I keep getting errors on the code the empties them
in the constrants.php file I have these
define ('SUCCESS', 301);
define ('FAIL', 302);
define ('FREE', 303);
then in the operations.php file I have this code
public function freeSpot($id){
if(!$this->isOccupied($id)){
return FREE;
}
else {
$stmt = $this->con->prepare("UPDATE spaces SET occupied_by = NULL WHERE id = ?");
$stmt->bind_param("ss", $occupiedBy, $id);
if ($stmt->execute()){
return SUCCESS;
}else{
return FAIL;
}
}
}
private function isOccupied($id){
$query = $this->con->prepare("SELECT occupied_by FROM spaces WHERE (id = ?) LIMIT 1");
$query->bind_param("s", $occupiedBy);
$query->bind_result($occupy);
$query->execute();
$query->store_result();
return $query->num_rows > 0;
}
and in the index.php file which I use to check the code on POSTMAN I have this code:
$app->post('/freeSpot/{id}', function (Request $request, Response $response) {
$pSpaceId = $request->getAttribute('id');
$requestData = $request->getParsedBody();
// $occupiedBy = $requestData['occupied_by'];
$db = new DbOperations();
$result = $db->freeSpot($id);
$responseData = array();
if ($result == SUCCESS) {
$responseData['error'] = false;
$responseData['message'] = 'spot emptied successfully';
$response->write(json_encode($responseData));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(200);
} else if($result == FAIL) {
$responseData['error'] = true;
$responseData['message'] = 'Could not empty';
$response->write(json_encode($responseData));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(200);
}
else if($result == FREE){
$responseData['error'] = true;
$responseData['message'] = 'spot is already empty';
$response->write(json_encode($responseData));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(200);
}
return $response
->withHeader('Content-type', 'application/json')
->withStatus(422);
});
Whatever id I enter it keeps telling me that the "spot is already empty" while it is not. What's wrong?