How to call a foreign key?

Asked

Viewed 81 times

0

Guys I have a platform that has a page to add person and another page has to add a plan to a person. That is, first creates a person, and only then is it possible to add a plan. My problem is connecting 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 I just don’t know where.

The Code of the plan is thus, where I can call the foreign key (person) in the middle of all this since the plan is inserted in two separate tables but the foreign key appears in 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 don’t know if you’re easy to understand, but here’s the thing, the person table is made up of personal information, everyone has a plan and each plan has various goals. But to let me know that the plan is for a person I must reference the right person’s ID code? How can I do this?

  • You have to create a select that contains people(id) on the plans page, but why don’t you use a person reference to the plan?

  • @Lucasgauna I have a foreign key referring to the person inside the plan. But in php code I don’t know how to use it

  • Can send the bank (structure of these two) just to get a better sense of how it is?

No answers

Browser other questions tagged

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