UPDATE in another table when updating a record does not work - CODEIGNITER - MYSQL

Asked

Viewed 623 times

0

Hello, the code to update a record to another table does not work.
I need to update the table vendas when saving a record from the table lancamentos.

on the table lancamentos there is the column vendas_id, which contains the id related to an invoiced sale. When I edit the release, I need the idLancamentos column lancamentos_id on the table vendas.

The controller has the following function:

            if ($this->financeiro_model->edit('lancamentos',$data,'idLancamentos',$this->input->post('id')) == TRUE) {

                //$estornado = 0;
                $venda = $this->input->post('venda_id');
                $lancamento = $this->input->post('idLancamentos');

                $sql = "UPDATE vendas set lancamentos_id = lancamentos_id = ? WHERE idVendas = ?";
                $this->db->query($sql, array($lancamento, $venda));

                $this->atualizaSaldoEditar();
                echo json_encode(array('result'=> true));

                $this->session->set_flashdata('success','lançamento editado com sucesso!');
                redirect($urlAtual);
            } else {

                $this->session->set_flashdata('error','Ocorreu um erro ao tentar editar lançamento!');
                echo json_encode(array('result'=> false));
                redirect($urlAtual);
            }

To be more precise, I believe the problem is in the Code below:

$venda = $this->input->post('venda_id');
$lancamento = $this->input->post('idLancamentos');

$sql = "UPDATE vendas set lancamentos_id = lancamentos_id = ? WHERE idVendas = ?";
$this->db->query($sql, array($lancamento, $venda));

  • Dude, for starters, it doesn’t make any less sense that you’re the one vendas_id on the table lancamentos and a lancamentos_id on the table vendas. Second, it’s a mess of your column names, there are times you use Camel Case, ten hours you don’t...it’s not clear what’s real. Third, which error you receive, give some message or simply don’t update?

  • Hey, buddy, don’t take Camelcase wrong, 'cause I put in a quick one of what I need. The sense of having the sale in launches is in... When I register a sale, it does not yet belong to the launch, but when I FATURO this sale, There is included as Revenue in the launch, being that the value of the billed balance is aggregated to an account. If launched = Revenue = Positive balance | If launched = Expense = Negative balance

  • So, in case there’s a need to disrupt this launch. Paid to Estornado, I can assign this same status to the registration of the sale in question. And for that I need to assign the release ID to the sales table. E receives no error. I get the message that the record was saved successfully and the status in the table lancamentos is being amended, however the update in the sales table is not performed.

  • Post the content of this method: $this->atualizaSaldoEditar();

1 answer

1


Your error is in this update:

$sql = "UPDATE vendas set lancamentos_id = lancamentos_id = ? WHERE idVendas = ?";

In addition to containing syntax error using x = x = x, your lancamentos_id in querie is the column of your own table.

The right thing would simply be this:

$sql = "UPDATE vendas set lancamentos_id = ? WHERE idVendas = ?";
  • Yeah, I’ve tried it this way, but it wasn’t.

  • But it makes a mistake?

  • There’s no mistake, it’s like there’s no querie.

  • Generate the querie with the correct values and run straight to the bank to see if it works

  • Error Number: 1452 Cannot add or update a Child Row: a Foreign key Constraint fails (banco.vendas, CONSTRAINT fk_vendas_lancamentos1 FOREIGN KEY (lancamentos_id) REFERENCES lancamentos (idLancamentos) ON DELETE NO ACTION ON UPDATE NO ACTION) UPDATE sales set lancments_id = '50' WHERE idVendas = '1'

  • But it worked. I removed the foreign key restriction on the sales table and applied your correction. Thanks

  • Oops, I was gonna talk to you about that... I’m glad it worked out!

  • Friend, I need to ask you something. You know how to make two updates using syntax $sql = "UPDATE vendas set lancamentos_id = ? WHERE idVendas = ?";? Just for better understanding, something like: $sql = "UPDATE vendas set lancamentos_id = ? set faturado = ? WHERE idVendas = ?";

  • Would it be the case to create 2 updates or is it possible to do it in one? According to PHP documentation, I know there is a possibility to do it in just one, but in codeigniter, it changes a little.

  • It is possible in the PHP or in any framework perform a single method, but in practice you will have to perform two UPDATE in the database.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.