View ID in the form before registering php and mysql

Asked

Viewed 770 times

0

I’m making 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, then if I click yes, it goes to a new form of registration doc that when saving in the database has to take the id_entity

  • It remains to put what has already been done (source code, not everything, but the necessary). The way it is is too subjective to answer properly.

  • I put the code I made so far.

  • Robson if your code so far is NO gets complicated. Your question is very broad as there are several ways to do it. I advise you to research more about php and try to perform this application. Search about php header php, it will direct the user to another environment if necessary.

2 answers

3


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

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

This should be for example. Now we 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 It’s quiet, but it’s susceptible to writing, because there’s no guarantee that anyone will try to register simultaneously. Anyway I left the function to do (commented). So let’s go to the page set_user.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 is obtained the id of the last inserted entity (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 inclusion output of the file salvar_documento.php (all html content).

And finally the file salvar_documento.php, can "recognize" the variable $ultimo_id, because it was included (require) within the set_user.php file (where the variable was declared $ultimo_id). And a form was added, with a hidden field representing the id of the created entity. It was 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 the functions mysqli_* see at w3c.

  • Wonderful!! Thank you very much

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>
  • One remark, when adding more details, don’t post an answer, edit the question.

Browser other questions tagged

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