Display ID in form before registering php and mysql

0

I'm doing a sign-up screen, but 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, then if I click on yes, it goes to a new doc registration form that when saving in the database has to take the id_entity

    
asked by anonymous 24.12.2017 / 19:26

2 answers

3

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

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

This should serve as an example. Now it is necessary the form to register users, 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>

Show the next value of id_entity is quiet, but overwriting is suspect because there is no way to ensure that no one tries to register at the same time. Anyway I left the function to do (commented). So let's go to the set_user.php page:

<?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 to insert new document). It also has the functions ob_get_clean () , ob_start () , ob_end_flush which are used to "print" the output of the save_document.php file (all html content).

Finally, the file save_documento.php , can "recognize" the variable $ultimo_id , since it was included in the file set_user.php (where% ). And a form was added, with a hidden field representing the created entity id. It looks 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 to the mysqli _ * functions, see w3c .

    
24.12.2017 / 21:34
0
   <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>
    
24.12.2017 / 20:06