Insert data if another INSERT is successfully executed

Asked

Viewed 37 times

2

I want you to enter the data in table.2 IF the first entry, in table.1, is successfully executed.

all_processo.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
	<title>Cadastro de Processos</title>
	<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="css/custom.css">
	<style type="text/css">
	<!--
.loader {
	margin: 0 auto;
  border: 12px solid #000000;
  border-radius: 50%;
  border-top: 7px solid black;
  border-right: 7px solid grey;
  border-bottom: 7px solid black;
  border-left: 7px solid grey;
  width: 50px;
  height: 50px;
  -webkit-animation: spin 2s linear infinite;
  animation: spin 1s linear infinite;
}

@-webkit-keyframes spin {
  0% { -webkit-transform: rotate(0deg); }
  100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.center1 {
	text-align: center;
}
<!---->
</style>
</head>
<body>
	<div class='container box-mensagem-crud'>
		<?php 
		require 'conexao.php';

		$conexao = conexao::getInstance();


		$acao  = (isset($_POST['acao'])) ? $_POST['acao'] : '';

	
		$numero = $_POST['numero'];
		$assunto = $_POST['assunto'];
		$requerente = $_POST['requerente'];
		$status = $_POST['status'];
		$informe = $_POST['informe'];
		$data = $_POST['data'];
		$processo_id = (isset($_GET['processo_id']));
		
		if ($acao == 'incluir'):
		
 
 
		$processos = "INSERT INTO processos(numero, assunto, requerente, status)
			VALUES(:numero, :assunto, :requerente, :status)";


			$stm = $conexao->prepare($processos);
			$stm->bindValue(':numero', $numero);			
			$stm->bindValue(':assunto', $assunto);
			$stm->bindValue(':requerente', $requerente);
			$stm->bindValue(':status', $status);
			$retorno = $stm->execute();
			
			if ($retorno == 1){
				
				$lastid = $conexao->lastInsertId();
			
			$informes = "INSERT INTO informes(informe, data, processo_id)
			VALUES (:informe, :data, :processo_id = $lastid)";
			
			$tsm = $conexao->prepare($informes);
			$tsm->bindValue(':informe', $informe);
			$tsm->bindValue(':data', $data);
			$tsm->bindValue(':processo_id', $processo_id);
			$lol = $tsm->execute();
			
			}else{
				if ($retorno == 0){
				
				echo "<div class='' role='alert'>Erro nos informes!</div>";
				echo "<div class=''</div>";

			echo "<meta http-equiv=refresh content='3;URL=registrarprocesso.php'>";
			}
			}
			
			if ($lol):
			
				echo "teste";
				echo "<div class='center1' role='alert'>Processo inserido com sucesso, aguarde você está sendo redirecionado...</div> ";
				echo "<div class='loader'</div>";
		    else:
		    	echo "<div class='center1' role='alert'>Erro ao inserir processo, aguarde!</div>";
				echo "<div class='loader'</div>";
			endif;

			echo "<meta http-equiv=refresh content='4;URL=listarprocessos.php'>";
			
			
		endif;

?>

table 1:

inserir a descrição da imagem aqui

table 2:

inserir a descrição da imagem aqui

  • What’s happening at the moment? Just enter the first one? See the POST content var_dump($_POST);

1 answer

0


2nd INSERT only if the first happens:

<?php
    require 'conexao.php';
    $conexao = conexao::getInstance();
    $acao  = (isset($_POST['acao'])) ? $_POST['acao'] : '';
    $numero = $_POST['numero'];
    $assunto = $_POST['assunto'];
    $requerente = $_POST['requerente'];
    $status = $_POST['status'];
    $informe = $_POST['informe'];
    $data = $_POST['data'];
    $processo_id = (isset($_GET['processo_id']));
    if ($acao == 'incluir'){
        $processos = "INSERT INTO processos(numero, assunto, requerente, status)VALUES(:numero, :assunto, :requerente, :status)";
        $stm = $conexao->prepare($processos);
        $stm->bindValue(':numero', $numero);
        $stm->bindValue(':assunto', $assunto);
        $stm->bindValue(':requerente', $requerente);
        $stm->bindValue(':status', $status);
        if($stm->execute()){
            $lastid = $conexao->lastInsertId();
            //************** aqui entram os valores que faltam *******************
            $informes = "INSERT INTO informes(de_setor, para_setor, informe, data, informante, processo_id)VALUES (:de_setor, :para_setor, :informe, :data, :informante, :processo_id)";
            $tsm = $conexao->prepare($informes);
            $tsm->bindValue(':de_setor', $de_setor);
            $tsm->bindValue(':para_setor', $para_setor);
            $tsm->bindValue(':informe', $informe);
            $tsm->bindValue(':data', $data);
            $tsm->bindValue(':informante', $informante);
            $tsm->bindValue(':processo_id', $lastid);
            if($tsm->execute()){
                echo "teste";
                echo "<div class='center1' role='alert'>Processo inserido com sucesso, aguarde você está sendo redirecionado...</div> ";
                echo "<div class='loader'</div>";
            }else{
                echo "<div class='center1' role='alert'>Erro ao inserir processo, aguarde!</div>";
                echo "<div class='loader'</div>";
            }
        }else{
            echo "<div class='' role='alert'>Erro nos informes!</div>";
            echo "<div class=''</div>";
            echo "<meta http-equiv=refresh content='3;URL=registrarprocesso.php'>";
        }
        echo "<meta http-equiv=refresh content='4;URL=listarprocessos.php'>";
    }
?>

There was also an error in the second INSERT and in the bind_param().

If the values of de_setor, para_setor e informante already exist, from another query, pass the values here, or do a query to bring the values.

  • 1

    Keep inserting only the first insertion =/ To stuck in it a few weeks ago, I don’t know how to proceed

  • of the one in the var_dump($tsm->execute())

  • 1

    bool(false) , which means?

  • no inserted, problem in query then

  • Table name ta right?

  • I already found the error, the table fields are marking as Não pode Ser NulL, and you’re trying to insert into the table missing data

  • 1

    Ué? I’m completing every form... how weird

  • Table 2 has 6 "mandatory" fields, because they are Nulo = Não, and you’re only trying to insert 3

  • Missing values for "de_setor, para_setor and informant"

  • 1

    are type int de_sector, para_sector and informant are pulled straight from database.

  • but you have to come on INSERT because if he doesn’t accept the record, only the AUTO_INCREMENT that fills alone there in the table

  • and type GET?

  • you said they are pulled from the database right? So just add these values there, I will edit the answer for you see where they will be

  • i did as edited, filled in the variables ( $de_sector = (isset($_GET['de_sector']); $para_setor = (isset($_GET['para_setor']); $informe = $_POST['informe']; $data = $_POST['data']; $informante = (isset($_GET['informante']);) but none of this changed the result, still not inserting the second table

  • but the values are being passed by GET? and vc using POST in code? Is one or the other

  • I am very slow & #Xa; , even if I can’t, I thank you for your time, thank you very much!

  • I who thank you for being able to help, and it is so, sometimes it is a point that was wrong that gnt breaks his head to find

Show 12 more comments

Browser other questions tagged

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