Display ID in form before registering php and mysql

I'm doing a registration screen, only I wanted the system to already display the ID (id_entity) that will be registered. When saving, it will ask if I want to register documents, alas if I click yes, it goes to a new doc registration form that when saving in the database has to take id_entity

Author: Robson Freitas, 2017-12-24

2 answers

Let's imagine a table, called an entity, already created in your bank. So we have:

+-----------------------------------------------+
|id_entidade integer primary key auto_increment |
|-----------------------------------------------|
|nome varchar(50)                               |
+-----------------------------------------------+

This should serve for the example. Now you need the form to register users, let's call form_usuario.php:

<form action="set_user.php" method="post">                                       
    <div class="form-body">
    <h3 class="box-title"><i class="icon-people p-r-10"></i>Dados Principais</h3>
    <hr>

    <div class="row">
        <div class="col-md-6">
        <div class="form-group">
            <label class="control-label">*Nome</label>
            <input type="text" id="nome" name="nome" class="form-control" placeholder="">
           </div>
        </div>
        <!--/span-->
        <div class="col-md-6">
        <div class="form-group">
            <label class="control-label">Sobrenome</label>
            <input type="text" id="sobrenome" name="sobrenome" class="form-control" placeholder="">
            </div>
        </div>
        <!--/span-->
    </div>

    <div>
        <!--Campo para redirecionar automaticamente caso queira salvar novo documento-->
        <input type="checkbox" name="novo_documento"> Salvar novo documento 
    </div>
    <input type="submit">
</form>

Shows the next value of id_entity is quiet, but is susceptible to overwriting, as there is no way to guarantee that no one will try to register simultaneously. Anyway I left the function to do (commented). So let's go to the set_user page.php:

<?php
cadastrarUsuario();
//registra novo usuario
function cadastrarUsuario(){
    $conexao = mysqli_connect('127.0.0.1', 'root', '', 'teste');
    $status = mysqli_query($conexao, 
    "insert into entidade (nome) values ('". $_POST['nome'] . "')");
    if($status === true){
        //redireciona para o formulario de cadastrar documento
        if(isset($_POST['novo_documento'])){
            ob_get_clean();
            ob_start();
            $ultimo_id = mysqli_insert_id($conexao);
            require_once __DIR__ . '/salvar_documento.php';
            ob_end_flush();


        }else{
            echo 'salvo com sucesso';       
        }
    }
    else{
        echo 'erro ao salvar: ' . mysqli_error($conexao);
    }
}
//função para encontrar o proximo id_entidade na tabela entidade
function proximoId(){
    //dados de conexão com o banco, na ordem, host, usuario, senha, e nome do banco utilizado
    $conexao = mysqli_connect('127.0.0.1', 'root', '', 'teste');
    //lista informações da tabela entidade (estrutura), uma delas refere-se ao proximo auto_increment
    $tabela_entidade = mysqli_query($conexao, "SHOW TABLE STATUS LIKE 'entidade'");
    $proximo_id = mysqli_fetch_assoc($tabela_entidade)['Auto_increment'];
    return $proximo_id;
}

On this page the id of the last inserted entity is obtained (for when going to the insert new document page). Also has the functions ob_get_clean () , ob_start(), ob_end_flush which are used to "print" the output of the file inclusion save_document.php (all html content).

And finally the file save_document.php , can "recognize" the variable $ultimo_id, because it was included (require) inside the set_user file.php (where the variable $ultimo_id was declared). And a form was added, with a hidden field representing the ID of the created entity. It looked like this:

<!--Outro formulario-->
<form action="algumapagina.php" method="post">                                       
    <div class="form-body">
    <h3 class="box-title"><i class="icon-people p-r-10"></i>Salvar documento</h3>
    <hr>

    <!--Recupera o id da ultima entidade salva-->
    <label>O id da entidade criada é <?php echo $ultimo_id; ?></label>
    <input type="hidden" name="id_entidade" value="<?php echo $ultimo_id; ?>">
    <div class="row">
        <div class="col-md-6">
        <div class="form-group">
            <label class="control-label">*Nome</label>
            <input type="text" id="nome" name="nome" class="form-control" placeholder="">
           </div>
        </div>
        <!--/span-->
        <div class="col-md-6">
        <div class="form-group">
            <label class="control-label">Sobrenome</label>
            <input type="text" id="sobrenome" name="sobrenome" class="form-control" placeholder="">
            </div>
        </div>
        <!--/span-->
    </div>

</form>

Suppose all files are in the same directory.

For a reference of functions mysqli_* see in w3c.

 3
Author: Juven_v, 2017-12-24 20:34:46
   <form action="set_user.php" method="post">                                       
                                    <div class="form-body">
                                        <h3 class="box-title"><i class="icon-people p-r-10"></i>Dados Principais</h3>
                                        <hr>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label class="control-label">*Nome</label>
                                                    <input type="text" id="nome" name="nome" class="form-control" placeholder="">
                                                   </div>
                                            </div>
                                            <!--/span-->
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label class="control-label">Sobrenome</label>
                                                    <input type="text" id="sobrenome" name="sobrenome" class="form-control" placeholder="">
                                                    </div>
                                            </div>
                                            <!--/span-->
                                        </div>
 0
Author: Robson Freitas, 2017-12-24 19:06:41