0
"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.

  • 1
    Please provide the `print_r` or `var_dump` output of `$request_data`. Depending on how your request looks like, `$request_data` can be an object or an array because it uses `json_decode()` in case of JSON (e.g. if form is sent via AJAX with JSON). – Johannes Apr 13 '19 at 19:56
  • @Johannes When I tried to print value of the variables I'm getting Undefined variable error – Katta Omkareshwar Apr 13 '19 at 20:04
  • Just add `print_r($request_data);` and send the output please. – Johannes Apr 13 '19 at 21:10
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Apr 13 '19 at 21:33

0 Answers0