Answer the question
In order to leave comments, you need to log in
What could be the problem? The script that worked for 2 days does not work?
the script worked for 2 days, after the last check I went to drink tea for 3 hours, when I returned I found that it did not work. He got mad, created a new server, set up and uploaded a known working code, and nothing works anyway .. Maybe someone can tell or push in the right direction. Ubuntu server 16.04, mysql, php7, here is the code after which nothing happens.
<spoiler title=""><?php
//getting the dboperation class
require_once '../includes/DbOperation.php';
//function validating all the paramters are available
//we will pass the required parameters to this function
function isTheseParametersAvailable($params){
//assuming all parameters are available
$available = true;
$missingparams = "";
foreach($params as $param){
if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){
$available = false;
$missingparams = $missingparams . ", " . $param;
}
}
//if parameters are missing
if(!$available){
$response = array();
$response['error'] = true;
$response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing';
//displaying error
echo json_encode($response);
//stopping further execution
die();
}
}
//an array to display response
$response = array();
//if it is an api call
//that means a get parameter named api call is set in the URL
//and with this parameter we are concluding that it is an api call
if(isset($_GET['apicall'])){
switch($_GET['apicall']){
//the CREATE operation
//if the api call value is 'createhero'
//we will create a record in the database
case 'createhero':
//first check the parameters required for this request are available or not
isTheseParametersAvailable(array('name','realname','rating','teamaffiliation'));
//creating a new dboperation object
$db = new DbOperation();
//creating a new record in the database
$result = $db->createHero(
$_POST['name'],
$_POST['realname'],
$_POST['rating'],
$_POST['teamaffiliation']
);
//if the record is created adding success to response
if($result){
//record is created means there is no error
$response['error'] = false;
//in message we have a success message
$response['message'] = 'Hero addedd successfully';
//and we are getting all the heroes from the database in the response
$response['heroes'] = $db->getHeroes();
}else{
//if record is not added that means there is an error
$response['error'] = true;
//and we have the error message
$response['message'] = 'Some error occurred please try again';
}
break;
//the READ operation
//if the call is getheroes
case 'getheroes':
$db = new DbOperation();
$response['error'] = false;
$response['message'] = 'Request successfully completed';
$response['heroes'] = $db->getHeroes();
break;
//the UPDATE operation
case 'updatehero':
isTheseParametersAvailable(array('id','name','realname','rating','teamaffiliation'));
$db = new DbOperation();
$result = $db->updateHero(
$_POST['id'],
$_POST['name'],
$_POST['realname'],
$_POST['rating'],
$_POST['teamaffiliation']
);
if($result){
$response['error'] = false;
$response['message'] = 'Hero updated successfully';
$response['heroes'] = $db->getHeroes();
}else{
$response['error'] = true;
$response['message'] = 'Some error occurred please try again';
}
break;
//the delete operation
case 'deletehero':
//for the delete operation we are getting a GET parameter from the url having the id of the record to be deleted
if(isset($_GET['id'])){
$db = new DbOperation();
if($db->deleteHero($_GET['id'])){
$response['error'] = false;
$response['message'] = 'Hero deleted successfully';
$response['heroes'] = $db->getHeroes();
}else{
$response['error'] = true;
$response['message'] = 'Some error occurred please try again';
}
}else{
$response['error'] = true;
$response['message'] = 'Nothing to delete, provide an id please';
}
break;
}
}else{
//if it is not api call
//pushing appropriate values to response array
$response['error'] = true;
$response['message'] = 'Invalid API Call';
}
//displaying the response in json structure
echo json_encode($response);
Answer the question
In order to leave comments, you need to log in
The thing is that you pass the name of the function to apicall, but you don’t catch it anymore.
To catch this, add to the switch statement:
default:
$response = 'undefined api';
break;
available
will be equal totrue
Maybe something like this? I haven't checked the code, so if there's something wrong, please fix it already...
function isTheseParametersAvailable($params){
//assuming all parameters are available
$missingparams = array();
foreach($params as $param){
if(!isset($_POST[$param]) || !strlen($_POST[$param]))
$missingparams[] = $param;
}
if(count($missingparams)){
$response['error'] = true;
$response['message'] = 'Parameters ' . implode(", ", $missingparams) . ' missing';
exit(json_encode($response));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question