我该如何解决代码未定义的索引错误

时间:2019-04-13 19:30:27

标签: php slim

"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 />

当Im对代码使用post方法时,Null值将传递到mySQL数据库。为了解决此错误,我得到了一些使用isset函数的解决方案。使用时,我得到了不确定的变量错误。为了解决这个问题,我将变量设置为全局变量,甚至以为我没有出错,但是将Null值传递给了我的数据库。我不知道该怎么解决。

这是我如何创建用户的数据操作代码

 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;
                 }
         }

这是我的索引页代码,用于从用户读取数据

$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();

当用户传递日期,金额,原因值时。这些值应输入数据库。

0 个答案:

没有答案