0

Hi guys I´m trying to update table column by using codeigniter, this is my code below.

Controller:

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

With function search2, I display results from two tables (that might be the problem) then I want to change the status of table cuentas to 'I' (inactive it is 'A' active by default). So I just want to update that Status column. But it doesn´t do anything. This is my model

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

'Interno' is the id from the table and also the input name where that id is displayed. Any help will be appreciated

8
  • did u get any error? Commented Jun 25, 2018 at 23:17
  • is your table is updated or not? Commented Jun 25, 2018 at 23:22
  • No, it doesn´t, it sends me to the else condition, "update not sucessful...", and I checked in mysql, and field is not being updated Commented Jun 25, 2018 at 23:23
  • make sure $data is not empty using print_r($data); in your controller and also $Interno Commented Jun 25, 2018 at 23:33
  • Yea both are empty Commented Jun 25, 2018 at 23:38

1 Answer 1

1

Hope this will help you :

Your controller method update_Status should be like this :

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

And moreover , you should return true or false from the update_Status method, based on there is any changes in your table or not

Note : $this->db->affected_rows() : Displays the number of affected rows, when doing “write” type queries (insert, update, etc.).

Your model method update_Status should be like this :

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);
   */
}
Sign up to request clarification or add additional context in comments.

8 Comments

now it shows error Parse error: syntax error, unexpected '$data' (T_VARIABLE), it says that is on this line in the controller, $data = array('Status' => $Status);
missing ; there, updated my answer do like this see there
Yea I spotted the ";" I tried both codes the one before and the one that you edited later, now it just shows me a blank page, it doesn´t show any error message or anything and I checked on mysql and column wasn´t updated.
this is because $Interno is empty, to check it add else part in controller see the updated answer
Do you think it has anything to do with the fact that, I´m displaying data from two different tables using left join, but anyways, "Interno" which is the ID is on both tables.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.