Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Fala Galera, gostaria de saber se é possivel colocar um insert e um update na mesma query ja tentei conforme abaixo e não deu certo.
$stmt = $conexao->prepare("
INSERT INTO registros (reg_nome, reg_email, reg_data_nascimento, reg_cpf, reg_rg, reg_telefone, reg_qtde_total, reg_id_viagem_fk, reg_valor_total, reg_data_compra)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
UPDATE viagens SET via_vagas = via_vagas - reg_qtde_total WHERE via_codigo = '".$codigo."' ");
Aqui é código completo:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$id = (isset($_POST["id"]) && $_POST["id"] != null) ? $_POST["id"] : "";
$nome = $_POST['nome'];
$email = $_POST['email'];
$dtnascimento= $_POST['data_nascimento'];
$d_for = explode('/', $dtnascimento);
$dia = $d_for[0];
$mes = $d_for[1];
$ano = $d_for[2];
$dtnascimento = $ano."-".$mes."-".$dia;
$cpf = $_POST['cpf'];
$rg = $_POST['rg'];
$telefone = $_POST['telefone'];
$v_qtde = $_POST [qtde];
$codigo = $_POST[codigo];
$v_total = number_format($_POST[v_total],2,',','.');
$data_compra = date('Y-m-d H:i:s');
} else if (!isset($id)) {
// Se não se não foi setado nenhum valor para variável $id
$id = (isset($_GET["id"]) && $_GET["id"] != null) ? $_GET["id"] : "";
$nome = NULL;
$email = NULL;
$dtnascimento = NULL;
$cpf = NULL;
$rg = NULL;
$telefone = NULL;
$v_qtde = NULL;
$codigo = NULL;
$v_total = NULL;
$data_compra = NULL;
}
try {
$conexao = new PDO("mysql:host=localhost; dbname=navj2", "root", "");
$conexao->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conexao->exec("set names utf8");
} catch (PDOException $erro) {
echo "Erro na conexão:" . $erro->getMessage();
}
if(isset($_POST['nome'])){
try {
$stmt = $conexao->prepare("
INSERT INTO registros (reg_nome, reg_email, reg_data_nascimento, reg_cpf, reg_rg, reg_telefone, reg_qtde_total, reg_id_viagem_fk, reg_valor_total, reg_data_compra)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bindParam(1, $nome);
$stmt->bindParam(2, $email);
$stmt->bindParam(3, $dtnascimento);
$stmt->bindParam(4, $cpf);
$stmt->bindParam(5, $rg);
$stmt->bindParam(6, $telefone);
$stmt->bindParam(7, $v_qtde);
$stmt->bindParam(8, $codigo);
$stmt->bindParam(9, $v_total);
$stmt->bindParam(10, $data_compra);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
$id = null;
$nome = NULL;
$email = NULL;
$dtnascimento = NULL;
$cpf = NULL;
$rg = NULL;
$telefone = NULL;
$v_qtde = NULL;
$codigo = NULL;
$v_total = NULL;
$data_compra = NULL;
} else {
echo "Erro ao tentar efetivar cadastro";
}
} else {
throw new PDOException("Erro: Não foi possível executar a declaração sql");
}
} catch (PDOException $erro) {
echo "Erro: " . $erro->getMessage();
}
}
?>Carregando comentários...