Fill in input text with user data from mysql

1

Good afternoon! I am creating a visitor registration form for commercials. I need to extract in an "input type=" text "as the default value: The commercial agent's code, (it is not the same as the client's id)

This is the form:                           

            <div class="row">
              <div class="col-25">
                <label for="agente">Agente</label>
              </div>
              <div class="col-75">
                <input type="text" name="agente" value="<?php echo ucfirst($_SESSION['cod_agente']); ?>">
              </div>
            </div>

            <div class="row">
              <div class="col-25">
                <label for="fechavisita">Fecha</label>
              </div>
              <div class="col-75">
                <input type="date" name="fechavisita" step="1" min="2017-01-01" max="2017-12-31" value="2017-01-01">
              </div>
            </div>

            <div class="row">
              <div class="col-25">
                <label for="hora">Hora</label>
              </div>
              <div class="col-75">
                <input type="text" id="hora" name="hora" placeholder="Hora de la visita">
              </div>
            </div>

            <div class="row">
              <div class="col-25">
                <label for="optica">Optica</label>
              </div>
              <div class="col-75">
                <input type="text" id="optica" name="optica" placeholder="Nombre de la óptica">
              </div>
            </div>

            <div class="row">
              <div class="col-25">
                <label for="provincia">Provincia</label>
              </div>
              <div class="col-75">
                <input type="text" id="provincia" name="provincia" placeholder="Provincia">
              </div>
            </div>

            <div class="row">
              <div class="col-25">
                <label for="direccion">Direccion</label>
              </div>
              <div class="col-75">
                <input type="text" id="direccion" name="direccion" placeholder="Ubicación de la óptica">
              </div>
            </div>

            <div class="row">
              <div class="col-25">
                <label for="notas">Notas</label>
              </div>
              <div class="col-75">
                <textarea id="notas" name="notas" placeholder="Agregar una nota" style="height:100px"></textarea>
              </div>
            </div>
            <div align="center" class="row">
              <input type="submit" value="Registrar Visita">
            </div>
          </form>
        </div>
        <!-- Fin Formulario -->

I try to avoid making a query to the database, and leave the agent code in that default field.

I can not get it out in any way.

Someone would be so kind to help me.

Thank you very much in advance!

    
asked by Javier Avila Fernandez 09.11.2017 в 17:44
source

1 answer

1

I explain, if I have understood your question:

  • You want to load the value of a text input the COD_AGENTE by means of a Session variable, without the need to load it from a query to the DB.

If so, you would have to do it in a similar way, but you still have to load the data in a query to the DB.

    if ($nueva_consulta = $mysqli->prepare("SELECT prna.id_persona,
                                                 prna.nombre_contacto,
                                                 prna.id_usuario_punto,
                                                 prna.usuario,
                                                 prna.password,
                                                 tusr.desc_tipo_usuario,
                                                 empr.id_empresa,
                                                 empr.nit,
                                                 empr.nombre_empresa
                                          FROM persona prna
                                          INNER JOIN tipodeusuario tusr  ON tusr.id_tipoDeUsuario = prna.id_tipoUsuario
                                          INNER JOIN empresa empr ON empr.id_empresa = prna.empresa_per
                                          WHERE prna.usuario = ?
                                          AND prna.password = ?
                                          AND tusr.desc_tipo_usuario = 'operario'"))
      {
        $nueva_consulta->bind_param('ss', $nit, $password);
      }
      else
      {
        exit();
      }
$nueva_consulta->execute();
    $resultado = $nueva_consulta->get_result();
    if($resultado->num_rows == 1)
    {
      $datos = $resultado->fetch_assoc();
      $_SESSION['usuario'] = $datos;
      echo json_encode(array('error' => false, 'tipo' => $datos['desc_tipo_usuario']));
    }
    else
    {
        echo json_encode(array('error' => true));
    }
    $nueva_consulta->close();
    $mysqli->close();

In this way, I use the session variables that are usually the most I can use, they are loaded at the moment of logging into the platform with your username and password.

To which the data is stored in a variable, which in my case I called it $ data:

$datos = $resultado->fetch_assoc();
      $_SESSION['usuario'] = $datos;
      echo json_encode(array('error' => false, 'tipo' => $datos['desc_tipo_usuario']));

To which $ data is made equal to the use of the session, to be used later (Obviously you must start the session in the same document in which you are going to use it, unless you include it in a class for later to be used in your PHP files):

<a>
    ¡Bienvenido, <?php echo $_SESSION['usuario']['nombre_empresa']?>!
</a>
  

Instead of using 'company_name' I could load 'user_id_point' or any of those in my query.

I hope I have helped, if you have any questions, ask!

    
answered by 09.11.2017 / 18:18
source