Update two tables at the same time (Update)

0

Hi, I have a problem updating two tables at the same time

  

Appointment in block

/*Si se ejecuta el update*/
        if($this->miembro->editar_miembro($editar_miembro, $id_usuario)) {
            /*Redireccionamos al listado de usuarios e indicamos que se actualizó correctamente*/
            $this->session->set_flashdata('success', 'Registro editado correctamente');
            redirect(base_url()."index.php/perfil");
        }//./if
  

Model

public function editar_miembro($datos, $id_usuario)
{
    /*Indicamos el usuario a editar*/
    $this->db->where('id_usuario', $id_usuario);

    /*Ejecutamos el update en la base de datos*/
    $this->db->update('usuario', $datos);

    return TRUE;

} // /editar_carrera

Only orita I'm passing the user's data, only I'm updating the user's data and I do not send the company data because I do not know how to build the query

as you realize, the user has as fk id_empresa. I hope you can help me.

    
asked by Javier fr 22.06.2017 в 16:00
source

2 answers

3

What you can do, as long as you are relating correctly the queries, is to execute a single UPDATE query , relating the fields, that will make changes > in two tables simultaneously .

This means that they are not two queries but one , therefore, if you already have them written you should look for the form of re-write it in one.

(I'll show you an example since you did not share the SQL statements).

Structures and data (similar to the ones you shared)

Query:

UPDATE
empresa
INNER JOIN
usuario
ON
usuario.id_empresa = empresa.id_empresa
SET
empresa.nombre_representante = 'ValorModificado1', usuario.campo_amodificar = 'ValorModificado2'
WHERE
empresa.id_empresa = 1

Result:

If you better specify the two queries (the one that modifies company and the one that modifies user ), we can help you assemble the single query.

    
answered by 22.06.2017 в 17:22
0

It works as long as there are values in both . If for example you have a relation 1a1 of the tables: Users-> data_factura you can make an update of all your data if they exist in both . The update does not work if there is only user data and there are not yet two billing information for that user in the other table.

    
answered by 10.01.2019 в 12:11