Php 编译错误可以';t在代码点火器中的写入上下文中使用方法返回值
我得到了一个在数据库中插入数据的模型,但问题是,其他字段不是必需的,所以我这样做了Php 编译错误可以';t在代码点火器中的写入上下文中使用方法返回值,php,codeigniter-3,hmvc,Php,Codeigniter 3,Hmvc,我得到了一个在数据库中插入数据的模型,但问题是,其他字段不是必需的,所以我这样做了 public function insert(){ if($this->input->post('MNAME') = NULL){ $mname = "n/a"; }else{ $mname = $this->input->post('MNAME'); } if($this->input->post('EXT
public function insert(){
if($this->input->post('MNAME') = NULL){
$mname = "n/a";
}else{
$mname = $this->input->post('MNAME');
}
if($this->input->post('EXTENSION') = NULL){
$ext = "n/a";
}else{
$ext = $this->input->post('EXTENSION');
}
if($this->input->post('EMAIL') = NULL){
$email = "n/a";
}else{
$email = $this->input->post('EMAIL');
}
if($this->input->post('CELLNO') = NULL){
$cell = "n/a";
}else{
$cell = $this->input->post('CELLNO');
}
if($this->input->post('AGENCY_NO') = NULL){
$agency = "n/a";
}else{
$agency = $this->input->post('AGENCY_NO');
}
$input = array(
'ID_NUM' => $this->uri->segment(3),
'FNAME' => $this->input->post('FNAME' ),
'SURNAME' => $this->input->post('SURNAME' ),
'DO_BIRTH' => $this->input->post('DO_BIRTH' ),
'POBIRTH' => $this->input->post('POBIRTH' ),
'SEX' => $this->input->post('SEX' ),
'CIVILSTAT' => $this->input->post('CIVILSTAT' ),
'ID_NAT' => $this->input->post('ID_NAT' ),
'HEIGHT' => $this->input->post('HEIGHT' ),
'WEIGHT' => $this->input->post('WEIGHT' ),
'BLOOD_TYPE' => $this->input->post('BLOOD_TYPE' ),
'RES_ADD' => $this->input->post('RES_ADD' ),
'RES_ZIP' => $this->input->post('RES_ZIP' ),
'PERM_ADD' => $this->input->post('PERM_ADD' ),
'PERM_ZIP' => $this->input->post('PERM_ZIP' ),
'MNAME' => $mname,
'EXTENSION' => $ext,
'EMAIL' => $email,
'CELLNO' => $cell,
'AGENCY_NO' => $agency,
'DATE_CREATED' => date("Y-m-d")
);
$insert = $this->db->insert($this->table,$input);
return $insert;
}
但问题是,我得到了这个错误不能在写上下文中使用方法返回值
说它在第108行。第108行是我模型的第一个if。我的错误是什么?有没有更短的代码
您在IF语句中只声明了1个=
,以备将来注意:匹配中应该是2个=
我为您重新构造了它,使您的代码更具可读性和简短
// Checking if they're null and appending n/a if they're
$example = array('MNAME','EXTENSION','EMAIL', 'CELLNO', 'AGENCY_NO');
$new = array();
foreach ($example as $_ex)
{
$check = $this->input->post($_ex);
if ($check == null)? array_push($new, 'n/a') : array_push($new, $check);
}
然后替换为以下内容(请记住数组中索引的起始位置为0):
我希望这能有所帮助。如果所有条件都无效,您将尝试为方法调用分配空值。您的语句需要两个
==
来检查匹配检查:这是您需要实现它的正确方法。
// Inside your DB insert query
'MNAME' => $new[0],
'EXTENSION' => $new[1],
'EMAIL' => $new[2],
'CELLNO' => $new[3],
'AGENCY_NO' => $new[4],