Update in two tables with the same function

Asked

Viewed 66 times

0

I have a function that makes a UPDATE I wonder if with the same function I can do more than one UPDATE for example in 2 tables in db. Is there any possibility ? I would like to know the syntax also if it exists.

Code:

public function editar(Prog $prog){
    $this->conn->beginTransaction();
    try {
        $stmt = $this->conn->prepare(
            'UPDATE ESP353Prog SET NrProg = :NrProg, DtProg = :DtProg, NrPlaca = :NrPlaca, NrPlacaCarreta = :NrPlacaCarreta, 
            NrPlacaReboque2 = :NrPlacaReboque2,  DsMotorista = :DsMotorista, DsOrigem = :DsOrigem, DsDestino = :DsDestino, QtPesoTtl = :QtPesoTtl
            WHERE ID = :ID');
        $stmt->bindValue(':ID', $prog->getid(), PDO::PARAM_INT);
        $stmt->bindValue(':NrProg', $prog->getprog(), PDO::PARAM_INT);
        $stmt->bindValue(':DtProg', $prog->getdataopr(), PDO::PARAM_INT);
        $stmt->bindValue(':NrPlaca', $prog->getplaca(), PDO::PARAM_INT);
        $stmt->bindValue(':NrPlacaCarreta', $prog->getcarreta(), PDO::PARAM_INT);
        $stmt->bindValue(':NrPlacaReboque2', $prog->getreboque(), PDO::PARAM_INT);
        $stmt->bindValue(':DsMotorista', $prog->getmot(), PDO::PARAM_STR);
        $stmt->bindValue(':DsOrigem', $prog->getorig(), PDO::PARAM_STR);
        $stmt->bindValue(':DsDestino', $prog->getdest(), PDO::PARAM_STR);
        $stmt->bindValue(':QtPesoTtl', $prog->getpesottl(), PDO::PARAM_INT);
        $stmt->execute();
        $this->conn->commit();
    }
    catch(Exception $e) {
        $this->conn->rollback();
    }
}
  • Yes it is possible, the hockey will change are the table name and fields. Have an example in that reply.

  • 2

    You will have to change a lot in the method, such as the SQL mount and the bindValue, when I was starting in PHP I created a basic crud library, just download, run . sql and open the index on localhost to read the introduction: https://github.com/LeonardoVilarinho/MinPDO

  • You must be looking for http://php.net/manualen/pdo.begintransaction.php

No answers

Browser other questions tagged

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