store php log id

0

Good staff, my goal is to store the record id that was entered into the database

$sql = "insert into pacientes (Nome, idade, dataNascimento, estadoCivil, nacionalidade, naturalidade, morada, telemovel, niss, nif, situacaoEconomica, trabalho, salario, outros, possuiFamilia, familiarProximo, parentesco, moradaFamiliar, contactoFamiliar, inscritoCentroSaude, qual, medicoFamilia, contacto, sns, grupoSanguineo, antecendentesClinicos, informacoesAcompanhamento) values ('$nome', '$idade', '$dataNascimento', '$estadoCivil', '$nacionalidade', '$naturalidade', '$morada', '$telemovel', '$niss', '$nif', '$situacaoEconomica', '$trabalho', '$salario', '$outros', '$possuiFamilia', '$familiarProximo', '$parentesco', '$moradaFamiliar', '$contactoFamiliar', '$inseridoCentroSaude', '$qual', '$medicoFamilia', '$contacto', '$sns', '$grupoSanguineo', '$antecendentesClinicos', '$informacoesAcompanhamento')";

    $resultado = mysqli_query($ligacao,$sql);
    $numero = mysqli_affected_rows($ligacao);
    if($numero==1)
    {
        $codigo = mysqli_insert_id($ligacao);
        echo "<span style='position:absolute; margin-top:600px; margin-left:400px; color:green;'>Inserido com sucesso. </span>";

}

Here I can store the id of this record through mysqli_insert_id- $codigo = mysqli_insert_id($ligacao);

My problem is that the following I wanted to store the registry id but another query and I can no longer use the same form because my goal would be to insert in another table the registration id of a query and the registration id from another query

    $result = count($_POST["agregado"]);
    $agregado=$_POST["agregado"];
    $t=0;
    $m = 0;

    for($i=0; $i<$result; $i=$i+4)
    {

   $p=$i+1;
   $e=$p+1;
   $b = $e+1;

    $sql_1="insert into agregado (nome, parentesco, idade, bi) value('$agregado[$i]', '$agregado[$p]', '$agregado[$e]', '$agregado[$b]')";
    $resultado_1 = mysqli_query($ligacao, $sql_1);
    $sql_2 = "insert into utenteagregado (id_agregado, id_utente)  values ('1', '$codigo')";
    $resultado_2 = mysqli_query($ligacao, $sql_2);
}

    mysqli_close($ligacao);
}

Summarizing this line $sql_2 = "insert into utenteagregado (id_agregado, id_utente) values ('1', '$codigo')"; where you have the number 1 I want to insert the id of this query $sql_1="insert into agregado (nome, parentesco, idade, bi) value('$agregado[$i]', '$agregado[$p]', '$agregado[$e]', '$agregado[$b]')";

    
asked by anonymous 03.05.2015 / 12:34

1 answer

0
___ erkimt ___ store php log id ______ qstntxt ___

Good staff, my goal is to store the record id that was entered into the database

$sql_1="insert into agregado (nome, parentesco, idade, bi) value('$agregado[$i]', '$agregado[$p]', '$agregado[$e]', '$agregado[$b]')";
$resultado_1 = mysqli_query($ligacao, $sql_1);
$sql_2 = "insert into utenteagregado (id_agregado, id_utente)  values (last_insert_id(), '$codigo')";
$resultado_2 = mysqli_query($ligacao, $sql_2);

Here I can store the id of this record through mysqli_insert_id- %code%

My problem is that the following I wanted to store the registry id but another query and I can no longer use the same form because my goal would be to insert in another table the registration id of a query and the registration id from another query

$sql_1="insert into agregado (nome, parentesco, idade, bi) value('$agregado[$i]', '$agregado[$p]', '$agregado[$e]', '$agregado[$b]')";
$resultado_1 = mysqli_query($ligacao, $sql_1);
$sql_2 = "insert into utenteagregado (id_agregado, id_utente)  values (last_insert_id(), '$codigo')";
$resultado_2 = mysqli_query($ligacao, $sql_2);

Summarizing this line %code% where you have the number 1 I want to insert the id of this query %code%

    
______ ___ azszpr61090

Use the LAST_INSERT_ID () function

In your case:

%pre%

An important comment on official documentation :

  

The ID that was generated is maintained in the server on a   per-connection basis. This means that the value returned by the   AUTO_INCREMENT value generated   for most recent statement affecting an AUTO_INCREMENT column by that   client. This value can not be affected by other clients, even if they   generate AUTO_INCREMENT values of their own. This behavior   that each client can retrieve its own ID without concern for the   activity of other clients, and without the need for locks or   transactions.

and

  

LAST_INSERT_ID () returns   only automatically generated AUTO_INCREMENT values. If you store an   explicit value other than NULL or 0, it does not affect the value   returned by LAST_INSERT_ID ().

    
___
03.05.2015 / 12:43