3
I am sending six fields of a form to insert in my Mysql but only the first three are going correctly, I need to insert the first three and in sequence the other three, but I have this difficulty in understanding how to do.
The variables are these?
$IdCandidato = $_SESSION['IdCandidato'];
$IdArea1 = $_POST['Area1'];
$IdCargo1 = $_POST['Cargo1'];
$Experiencia1 = $_POST['Experiencia1'];
$Area2 = $_POST['Area2'];
$Cargo2 = $_POST['Cargo2'];
$Experiencia2 = $_POST['Experiencia2'];
$DataHoraAtualizacao = date('Y-m-d H:i:s');
The variables IdCandidato and DataHoraAtualizacao I’m recovering at the time of the recording attempt.
I’ll put an image to explain better, the form is this:
The table structure is like this:
CREATE TABLE `crrAreaExpectativa` (
    `IdAreaExpectativa` INT(10) NOT NULL AUTO_INCREMENT COMMENT 'Código da area expectativa',
    `IdCandidato` INT(10) NOT NULL COMMENT 'Código do candidato',
    `Opcao` TINYINT(1) NOT NULL COMMENT 'Opção: 1 ou 2',
    `IdArea` INT(10) NOT NULL COMMENT 'Código da área',
    `IdCargo` INT(10) NOT NULL COMMENT 'Código do cargo',
    `DataHoraAtualizacao` DATETIME NOT NULL COMMENT 'Data/Hora da atualização',
    PRIMARY KEY (`IdAreaExpectativa`, `IdCandidato`),
    INDEX `IDXOpcao` (`Opcao`),
    INDEX `IDXIdArea` (`IdArea`),
    INDEX `IDXIdCargo` (`IdCargo`),
    INDEX `FK_AreaExpectativa_Candidato` (`IdCandidato`),
    CONSTRAINT `FK_AreaExpectativa_Candidato` FOREIGN KEY (`IdCandidato`) REFERENCES `crrCandidato` (`IdCandidato`) ON UPDATE NO ACTION ON DELETE CASCADE
)
COMMENT='Área e cargo onde o candidato deseja trabalhar'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=14
;
What I’m trying to do is this:
try {
    // INICIALIZA A TRANSAÇÃO
    $conexao->beginTransaction();
        try {                
            // INSERT 3 PRIMEIROS CAMPOS
            $sql1 = 'INSERT INTO `crrAreaExpectativa` (`IdCandidato`, `Opcao`, `IdArea`, `IdCargo`, `DataHoraAtualizacao`) VALUES (?,?,?,?,?)';
            $stm = $conexao->prepare($sql1);
            $stm->bindValue(1, $IdCandidato, PDO::PARAM_INT);
            $stm->bindValue(2, $Experiencia1, PDO::PARAM_INT);
            $stm->bindValue(3, $IdArea1, PDO::PARAM_INT);
            $stm->bindValue(4, $IdCargo1, PDO::PARAM_INT);
            $stm->bindValue(5, $DataHoraAtualizacao, PDO::PARAM_STR);
            $retorno = $stm->execute(); 
            // INSERT 3 ÚLTIMOS CAMPOS
            $sql2 = 'INSERT INTO `crrAreaExpectativa` (`IdCandidato`, `Opcao`, `IdArea`, `IdCargo`, `DataHoraAtualizacao`) VALUES (?,?,?,?,?)';
            $stm = $conexao->prepare($sql2);
            $stm->bindValue(1, $IdCandidato, PDO::PARAM_INT);
            $stm->bindValue(2, $Experiencia2, PDO::PARAM_INT);
            $stm->bindValue(3, $IdArea2, PDO::PARAM_INT);
            $stm->bindValue(4, $IdCargo2, PDO::PARAM_INT);
            $stm->bindValue(5, $DataHoraAtualizacao, PDO::PARAM_STR);
            $retorno = $stm->execute(); 
            $conexao->commit();
        } catch (PDOException $erro) {  
                // DESFAZ TRANSAÇÃO ATUAL
                $conexao->rollBack();           
                $erro = $erro->getMessage();
                $retorno = array('codigo' => '0', 'mensagem' => ' Erro ao inserir o registro. Tente novamente [2]. ::$erro');
                $conexao = null;        
                // RETORNO DE MENSAGEM
                echo json_encode($retorno);
                exit();             
        }
} catch (Exception $e) {    
    // DESFAZ TRANSAÇÃO ATUAL
    $conexao->rollBack();   
    $retorno = array('codigo' => '0', 'mensagem' => ' Ocorreu um erro na gravação, tente novamente [5]. ::$erro');              
    $conexao = null;        
    // RETORNO DE MENSAGEM
    echo json_encode($retorno);
    exit();     
}
$retorno = array('codigo' => '1', 'mensagem' => ' Registro inserido com sucesso');  
// FECHA CONEXÃO
$conexao = null;        
// RETORNO DE MENSAGEM
echo json_encode($retorno);
exit();
						
Damn the one about the drug test really got me, thanks @Talesperes for the great help and look sharp, thanks.
– adventistapr