Update with two tables at the same time

Asked

Viewed 81 times

0

I’m doing an update to edit window and it’s not giving error, but it’s only working the address part, the beekeeper information part is not updating

Tabela endereçoBeekeeper table

<?php 
session_start();
require_once "conexao.php";


$nome_apicultor = $_POST['nome_apicultor'];
$sobrenome_apicultor = $_POST['sobrenome_apicultor'];
$email_apicultor = $_POST['email_apicultor'];
$cpf_apicultor = $_POST['cpf_apicultor'];
$rua_apicultor = $_POST['rua_apicultor'];
$numero_apicultor = $_POST['numero_apicultor'];
$bairro_apicultor = $_POST['bairro_apicultor'];
$cidade_apicultor = $_POST['cidade_apicultor'];
$complemento_apicultor = $_POST['complemento_apicultor'];
$telefone_apicultor = $_POST['telefone_apicultor'];
$estado_apicultor = $_POST['estado_apicultor'];
$codEndereco = $_POST['codEndereco'];
$codApicultor = $_SESSION["token"];

try {
    $prepared = $conexao_pdo->prepare("UPDATE `endereco` SET `endereco` = :rua_apicultor, `bairro` = :bairro_apicultor, `numero` = :numero_apicultor, `complemento` = :complemento_apicultor, `cidade` = :cidade_apicultor, `estado` = :estado_apicultor  where `cod` = :codEndereco;");
    $prepared->bindParam(":rua_apicultor", $rua_apicultor);
    $prepared->bindParam(":bairro_apicultor", $bairro_apicultor);
    $prepared->bindParam(":numero_apicultor", $numero_apicultor);
    $prepared->bindParam(":complemento_apicultor", $complemento_apicultor);
    $prepared->bindParam(":cidade_apicultor", $cidade_apicultor);
    $prepared->bindParam(":estado_apicultor", $estado_apicultor);
    $prepared->bindParam(":codEndereco", $codEndereco);
    $prepared->execute();
    $id_end = $conexao_pdo->lastInsertId();

    if($prepared->rowCount() > 0) {
        $prepared = $conexao_pdo->prepare("UPDATE `apicultor` SET `nome` = :nome_apicultor, `sobrenome` = :sobrenome_apicultor, `email` = :email_apicultor, `cpf` = :cpf_apicultor, `telefone` = :telefone_apicultor, `endereco_cod` = :endereco_cod  where `cod` = :codApicultor;");
        $prepared->bindParam(":nome_apicultor", $nome_apicultor);
        $prepared->bindParam(":sobrenome_apicultor", $sobrenome_apicultor);
        $prepared->bindParam(":email_apicultor", $email_apicultor);
        $prepared->bindParam(":cpf_apicultor", $cpf_apicultor);
        $prepared->bindParam(":telefone_apicultor", $telefone_apicultor);
        $prepared->bindParam(":codApicultor", $codApicultor);
        $prepared->bindParam(":endereco_cod", $id_end);
        $prepared->execute();

        if($prepared->rowCount() > 0) {
            header("Location: ../Perfil.php");
        }
        else {
            header("Location: ../Perfil.php");
        }
    }
    else {
        header("Location: ../Perfil.php");
    }

} catch(PDOException $e) {
    print $e->getMessages();
    die();
}
?>
  • Define a transaction with the two UPDATE commands, one for each table, that they will be executed atomic, that is, either the two UPDATES will be executed or neither of them will be.

1 answer

0


In order to help better place these two tables in your database in question. What if the endereco_cod that you passed for primary key put it in Where clause instead of editing it. Something like this:

UPDATE `apicultor` SET `nome` = :nome_apicultor, `sobrenome` = :sobrenome_apicultor, `email` = :email_apicultor, `cpf` = :cpf_apicultor, `telefone` = :telefone_apicultor  
where `endereco_cod` = :endereco_cod and `cod_Apicultor` = :codApicultor;

And one thing I’ve also noticed is that in your letter where in the two updates is written cod = :codEndereco; and the other cod = :codEndereco;, the cod then it would be the same in address and beekeeper?

  • I put the two tables in question

  • If you are using POO take a look at the documentation to make it simpler you will understand better: https://www.php.net/manual/en/book.pdo.php Why the way you put the code I don’t know if you can use functions that I would recommend. Proucure something better about the MVC standard if you don’t understand.

Browser other questions tagged

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