How to insert data into related tables using PDO

0

I did a lot of research, but I do not know how to apply.

    
asked by anonymous 24.04.2018 / 15:53

1 answer

0

See an example of how to enter a neighborhood:

public function insert (Bairro $bairro){
     $this->connection =  null;
     $teste = 0;
     $this->connection = new ConnectionFactory();
     $this->connection->beginTransaction();
     try{
         $query = "INSERT INTO bairro 
                   (CD_BAIRRO, NM_BAIRRO, CD_CIDADE) 
                   VALUES 
                   (NULL, :bairro, :cidade)";
         $stmt = $this->connection->prepare($query);
         $stmt->bindValue(":bairro", $bairro->getNmBairro(), PDO::PARAM_STR);
         $stmt->bindValue(":cidade",$bairro->getCidade()->getCdCidade(), PDO::PARAM_INT);
         $stmt->execute();
         $teste =  $this->connection->lastInsertId();
         $this->connection->commit();
         $this->connection =  null;
     }catch(PDOException $exception){
         echo "Erro: ".$exception->getMessage();
     }
     return $teste;
}

I hope it helps to have a notion

    
24.04.2018 / 15:59