使用codeigniter更新表列

uxhixvfz  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(381)

嗨伙计们我´我试图用codeigniter更新表列,下面是我的代码。
控制器:

function search2()
{
  if ($_POST) {
      $Interno=$_POST['Interno'];
  }else{
    $Interno = '';
  }
  $this->db->select('empleados.Interno, empleados.Curp, empleados.Nombre, empleados.A_Paterno, empleados.A_Materno, cuentas.Clabe, cuentas.Banco, cuentas.Observaciones, cuentas.Status');
$this->db->from('empleados');
$this->db->join('cuentas',"cuentas.Interno = empleados.Interno AND cuentas.Status !='I'", 'Left');
$this->db->where('empleados.Interno', $Interno);
$q = $this->db->get();
$data = array();
$data['records'] = $q->result_array();
$this ->load -> view('main/indice', $data);  
}

function update_Status() 
        {
    $Interno= $this->input->post('Interno');
    $data = array(
    'Status' => $this->input->post('Inactivo')
       );
      if($this->consultas_M->update_Status($Interno, $data))
     {
       echo " update successful...";
       }
    else
      {
  echo "update not successful...";
     }  
   }

使用函数search2,我显示来自两个表的结果(这可能是问题所在),然后我想将表cuentas的状态更改为“i”(不活动,默认情况下是“a”活动)。所以我只想更新状态栏。但事实并非如此´我什么都不做。这是我的模型

function update_Status($Interno,$data)
    {
   $this->db->where('Interno', $Interno);
   $this->db->update('cuentas', $data);
    }

“interno”是表中的id,也是显示该id的输入名称。任何帮助都将不胜感激

qacovj5a

qacovj5a1#

希望这能帮助您:
您的控制器方法 update_Status 应该是这样的:

function update_Status() 
{
    $Interno = $this->input->post('Interno');
    $Status = $this->input->post('Inactivo');

    $data['status'] = ! empty($Status) ? $Status : 'I';
    if ( ! empty($Interno))
    {
        $updated = $this->consultas_M->update_Status($Interno, $data);
        if($updated)
        {
           echo " update successful...";
        }
        else
        {
            echo "update not successful...";
        }  
    }
    else
    {
        echo "Interno  not found ...";
    }    
}

而且,你应该回来 true 或者 falseupdate_Status 方法,取决于表中是否有任何更改
注: $this->db->affected_rows() :执行“写入”类型查询(插入、更新等)时,显示受影响的行数。
你的模型方法 update_Status 应该是这样的:

function update_Status($Interno,$data)
{
   $this->db->where('Interno', $Interno);
   $this->db->update('cuentas', $data);
   if ( $this->db->affected_rows() > 0 )
   {
      return TRUE;
   }
   else 
   {
     return FALSE;
   }
   /* OR you can simply return like this 
     return $this->db->update('cuentas', $data);
   */
}

相关问题