How to insert into one table and after insert into another (with foreign key)?

-1

Good afternoon,

How can I make a simultaneous INSERT? I want you to insert the data from one table, insert the other one too!

Database: MYSQL

Insertion script:

<!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'];
		
		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){
			
			$informes = "INSERT INTO informes(informe, data)
			VALUES (:informe, :data)";
			
			$tsm = $conexao->prepare($informes);
			$tsm->bindValue(':informe', $informe);
			$tsm->bindValue(':data', $data);
			$lol = $tsm->execute();
			}
			
			if ($lol):
			
				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;
		
				?>
		

	</div>
</body>
</html>

Processes table:

Reportstable:

    
asked by anonymous 26.04.2018 / 20:45

1 answer

0

By PHP, you can get the last value entered:

$ultimo_valor_inserido = mysql_insert_id();

by SqlServer there is @@IDENTITY: o @@IDENTITY retorna o último ID inserido no banco.

You can also give an insert in the first table and then a select MAX to get the last ID and save in the 2 table.

    
26.04.2018 / 21:30