I have a platform that has one page to add person and another page has to add a plan to a person. That is, you first create a person, and only then can you add a plan. My problem is to connect a plan with a person. I in the plan table have the foreign key of the person table. Now on the plan page I needed to refer to the person's Id somewhere just do not know where.
The plan code is like this, where I can call the foreign key (person) in the middle of it all since the plan is inserted in two distinct tables but the foreign key appears in the tblplano?
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
{
header('location:index.php');
}
else{
if(isset($_POST['add']))
{
$concretizaobj=$_POST['PerConObj'];
$objdefinidos=$_POST['ObjDef'];
$objatingidos=$_POST['ObjAting'];
$totalalcancados=$_POST['PerAlc'];
$datainicio=$_POST['DataInicial'];
$datafim=$_POST['DataFinal'];
$avalexp=$_POST['AvalExp'];
$sql = "INSERT INTO tblplano (PerConObj, ObjDef, ObjAting, PerAlc, DataInicial, DataFinal, AvalExp, ) VALUES(:concretizaobj, :objdefinidos, :objatingidos, :totalalcancados, :datainicio, :datafim, :avalexp)";
$query = $dbh->prepare($sql);
$query->bindParam(':concretizaobj', $concretizaobj, PDO::PARAM_STR);
$query->bindParam(':objdefinidos', $objdefinidos, PDO::PARAM_STR);
$query->bindParam(':objatingidos', $objatingidos, PDO::PARAM_STR);
$query->bindParam(':totalalcancados', $totalalcancados, PDO::PARAM_STR);
$query->bindParam(':datainicio', $datainicio, PDO::PARAM_STR);
$query->bindParam(':datafim', $datafim, PDO::PARAM_STR);
$query->bindParam(':avalexp', $avalexp, PDO::PARAM_STR);
$query->execute();
$id1 = $dbh->lastInsertId();
for($i = 0; $i < 12; $i++) {
$avinicial=$_POST['AvInicial'][$i];
$meta=$_POST['Meta'][$i];
$avintercalar=$_POST['AvIntercalar'][$i];
$avfinal=$_POST['AvFinal'][$i];
$resultadouni=$_POST['Resultado'][$i];
$sql = "INSERT INTO tblobjetivos(tblplano_plano_id, AvInicial, Meta, AvIntercalar, AvFinal,Resultado) VALUES(:id1, :avinicial, :meta, :avintercalar, :avfinal, :resultadouni)";
$query = $dbh->prepare($sql);
$query->bindParam(':id1', $id1, PDO::PARAM_STR);
$query->bindParam(':avinicial', $avinicial, PDO::PARAM_STR);
$query->bindParam(':meta', $meta, PDO::PARAM_STR);
$query->bindParam(':avintercalar', $avintercalar, PDO::PARAM_STR);
$query->bindParam(':avfinal', $avfinal, PDO::PARAM_STR);
$query->bindParam(':resultadouni', $resultadouni, PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
if($lastInsertId)
{
$msg="PII Adicionado Com Sucesso!";
}
else
{
$error="Erro, tente novamente";
}}}
?>
I do not know if it is easy to understand but it is as follows, the person table is made up of personal information, all people have a plan and each plan has several goals. But for me to warn that the plan is for a person should I refer in the ID code of the right person? How can I do this?