如何修复PHP中的参数错误?

时间:2022-01-09 07:50:49

This is my controller:

这是我的控制器:

public function admin_debit(){    $date= date('Y-m-d H:i:s');    $trans_type=2;    $amount=$this->input->post('amount');     $total_amount = array_sum($amount);    $json=json_encode($_POST);      $user=$this->session->userdata('sess_pt_userid');    $data=array(                   'dated_on'=>$date,                   'amount'=>$total_amount,                    'userid'=>$user,                    'purpose'=>$json,                    'trans_type'=>$trans_type                 ); $response = $this->Petty_model->debit_insert($data); //add flash data          if($response)         {           $this->session->set_flashdata('success', 'Cash Debited Successfully ');           redirect('Petty_controller/debitview');         }     else       {         $this->session->set_flashdata('error', 'Something worng. Error!!');         redirect('Petty_controller/debitview');       }      }

Here an error occurred in my code:

我的代码中出现了一个错误:

array_sum() expects parameter 1 to be array, null given.

array_sum()期望参数1为数组,给定null。

I get an another error which says:

另一个错误是

Column 'amount' cannot be null

列'amount'不能为空

3 个解决方案

#1


1  

 $amount=$this->input->post('amount');  $total_amount = array_sum($amount);

here the $amount is null because from the input it receives nothing. and array_sum requires an array to be operated upon. instead it gets null, thus the exception occures

这里的$金额为null,因为从输入中它什么都得不到。array_sum需要对数组进行操作。相反,它会得到空值,因此出现异常。

#2


1  

just check if post data of amount is array

只需要检查数据的post数据是否为数组。

$total_amount=0;if($this->input->post('amount') && is_array($this->input->post('amount')){ $total_amount = array_sum($this->input->post('amount'));}$date= date('Y-m-d H:i:s');$trans_type=2;$json=json_encode($_POST);  $user=$this->session->userdata('sess_pt_userid');$data=array(               'dated_on'=>$date,               'amount'=>$total_amount,                'userid'=>$user,                'purpose'=>$json,                'trans_type'=>$trans_type             );$response = $this->Petty_model->debit_insert($data);

#3


1  

If your amount like '1,2,3' this then you need set this in array

如果你的数量像“1,2,3”,那么你需要把它设置成数组。

$amount = '1,2,3';$amount = explode(",",$amount);$total_amount = array_sum($amount);

#1


1  

 $amount=$this->input->post('amount');  $total_amount = array_sum($amount);

here the $amount is null because from the input it receives nothing. and array_sum requires an array to be operated upon. instead it gets null, thus the exception occures

这里的$金额为null,因为从输入中它什么都得不到。array_sum需要对数组进行操作。相反,它会得到空值,因此出现异常。

#2


1  

just check if post data of amount is array

只需要检查数据的post数据是否为数组。

$total_amount=0;if($this->input->post('amount') && is_array($this->input->post('amount')){ $total_amount = array_sum($this->input->post('amount'));}$date= date('Y-m-d H:i:s');$trans_type=2;$json=json_encode($_POST);  $user=$this->session->userdata('sess_pt_userid');$data=array(               'dated_on'=>$date,               'amount'=>$total_amount,                'userid'=>$user,                'purpose'=>$json,                'trans_type'=>$trans_type             );$response = $this->Petty_model->debit_insert($data);

#3


1  

If your amount like '1,2,3' this then you need set this in array

如果你的数量像“1,2,3”,那么你需要把它设置成数组。

$amount = '1,2,3';$amount = explode(",",$amount);$total_amount = array_sum($amount);