How to register a foreign key that is on another screen in php?

Asked

Viewed 47 times

-1

How do I save the user’s id_user who is logged in, and the address class’s id_address in the denunciation table?

OBS: class primary keys are automatic in the database

Login class:

<!DOCTYPE html>
<?php
$conexao = mysqli_connect("127.0.0.1", "root", "");
mysqli_select_db($conexao, "projeto");
?>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>Cadastro</title>
    </head>
    <body>
        <script language="javascript">
            function sucesso() {
                setTimeout("window.location='telaLogin.php'", 2000);
            }

        </script>
        <?php
        $nome = $_POST["nome"];
        $username = $_POST["username"];
        $password = $_POST["password"];
        $password = md5($password);
        $email = $_POST["email"];

        $inserir = "INSERT INTO usuario (nome, username, password, email) 
        VALUES ('$nome', '$username', '$password', '$email');";
        mysqli_query($conexao, $inserir) or die (mysqli_error($conexao));
        echo"Usuario cadastrado com sucesso. Redirecionando para tela de login em 2 segundos.";
        echo"<script language='javascript'>sucesso()</script";
        ?>
    </body>
</html> 

screen denounces:

<!DOCTYPE html>
<?php
    $conexao = mysqli_connect("127.0.0.1", "root", "");
    mysqli_select_db($conexao, "projeto");
?>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>Cadastro</title>
    </head>
    <body>
    <script language="javascript">
            function sucesso(){
                setTimeout("window.location='telaInicial2.php'", 2000);
            }
            
        </script>
    <?php
        $descricaoProblema = $_POST["descricaoProblema"];
        $dicaSolucao = $_POST["dicaSolucao"];
            
        $inserir = "INSERT INTO denuncia (descricaoProblema, dicaSolucao, Usuario_id, Endereco_id, Status_id) 
        VALUES ('$descricaoProblema', '$dicaSolucao', '1');";
        mysqli_query($conexao, $inserir) or die (mysqli_error($conexao));
        echo"Denuncia cadastrada com sucesso. Redirecionando para tela inicial em 1 segundos.";
        echo"<script language='javascript'>sucesso()</script";
        ?>
    </body>
</html> 

Address class:

<!DOCTYPE html>
<?php
    $conexao = mysqli_connect("127.0.0.1", "root", "");
    mysqli_select_db($conexao, "projeto");
?>
<html lang="pt-br">
    <head>
        <meta charset="utf-8">
        <title>Cadastro</title>
    </head>
    <body>
    <script language="javascript">
            function sucesso(){
                setTimeout("window.location='telaDenuncia.php'", 2000);
            }
            
        </script>
    <?php
        $local = $_POST["local"];
        $estado = $_POST["estado"];
        $cidade = $_POST["cidade"];
        $bairro = $_POST["bairro"];
        $rua = $_POST["rua"];
        $telefone = $_POST["telefone"];
            
        $inserir = "INSERT INTO endereco (local, estado, cidade, bairro, rua, telefone) 
        VALUES ('$local', '$estado', '$cidade', '$bairro', '$rua', '$telefone');";
        mysqli_query($conexao, $inserir) or die (mysqli_error($conexao));
        echo"Endereco cadastrado com sucesso. Redirecionando para cadastro da denuncia em 2 segundos.";
        echo"<script language='javascript'>sucesso()</script";
        ?>
    </body>
</html>

Sql scripts:

create table usuario(
idUsuario serial,
nome varchar(60) not null,
username varchar(60) not null,
password varchar(200) not null,
email varchar(20) not null,
PRIMARY KEY (idUsuario) 
)Engine=InnoDB;

alter table usuario add constraint unique_username unique(username);

create table endereco(
idEndereco serial,
local varchar(60) not null,
estado varchar(40) not null,
cidade varchar(40) not null,
bairro varchar(60) not null,
rua varchar(60) not null,
telefone varchar(15) not null,
PRIMARY KEY(idEndereco)
)Engine=InnoDB;

create table denuncia(
idDenuncia serial,
descricaoProblema varchar(200),
dicaSolucao varchar(200),
Usuario_id BIGINT UNSIGNED ,
Endereco_id BIGINT UNSIGNED ,
Status_id int(20),
FOREIGN KEY (Usuario_id)REFERENCES usuario (idUsuario),
FOREIGN KEY (Endereco_id)REFERENCES endereco (idEndereco),
FOREIGN KEY (Status_id)REFERENCES status (idStatus) 
)Engine=InnoDB;

1 answer

0

The ID generated by the last query, if it is AUTO_INCREMENT, is returned by mysqli_insert_id, thus:

mysqli_query($conexao, $inserir)
$id = mysqli_insert_id($conexao);

Another way, if using the object mysqli, would be using the insert_id:

$query = "INSERT INTO Tabela VALUES (1)";
$mysqli->query($query);
$id = $mysqli->insert_id

Then you can use this ID in the next query you want to need it as Foreign key

More details and examples in php documentation: https://www.php.net/manual/en/mysqli.insert-id.php

  • And how do I call this attribute $id in another class?

  • will need to pass by parameter if the commands of insert are in separate classes

  • They are in separate classes, can you show me how to do this in the login and denunciation classes? I searched in several places and could not solve. I’m a beginner in php

  • Follow the project link below in git --> https://github.com/Matheus-maas/SistemaDenuncia001

Browser other questions tagged

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