"Spend Added Successfully"<br />
<b>Notice</b>: Undefined index: date in <b>C:\xampp\htdocs\mobile\public\index.php</b> on line <b>17</b><br />
<br />
<b>Notice</b>: Undefined index: reason in <b>C:\xampp\htdocs\mobile\public\index.php</b> on line <b>18</b><br />
<br />
<b>Notice</b>: Undefined index: amount in <b>C:\xampp\htdocs\mobile\public\index.php</b> on line <b>19</b><br />
When Im using post method for this code, Null values are passing to mySQL database. In order to solve this error I got some solutions saying to use isset function. When used that I got undefined variable error. In order to solve this I made the variables global, even thought I'm not getting error's but Null values are being passed to my database. I don't know how to solve this.
This is my data operations code of how to create a user
public function createSpending($date, $reason, $amount){
$stmt = $this->con->prepare("INSERT INTO spending(date, reason,amount) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $date, $reason, $amount);
if($stmt->execute()){
return Spend_Added;
}else{
return Spend_notAdded;
}
}
This is my index page code to read the data from the user
$app->post('/addSpend', function(Request $request, Response $response){
$request_data = $request->getParsedBody();
$date = $request_data['date'];
$reason = $request_data['reason'];
$amount = $request_data['amount'];
$db = new DbOperations;
$result = $db->createsSpending($date, $reason, $amount);
if($result == Spend_Added){
$response->write(json_encode('Spend Added Successfully'));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(201);
}else if($result == Spend_notAdded){
$response->write(json_encode('Spend Adding Failed'));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(422);
}
});
$app->run();
when a user passes values of date, amount, reason. These values should be entered into database.