How to enter data from an SQL query in a form already done in PHP?

1

In my case I have a form and several functions, but I have not been able to transfer information from a sql query to already defined text fields, I want to be able to have the id, call its respective information in each text field when pressing One button, thanks. I quote the code below.

<form method="POST">
                    <table>
                        <tr>
                            <td>Documento de identidad:</td>
                            <td><input type="text" placeholder="Documento identidad" name="id" style="width: 100%;" onkeypress="return validar_texto(event)" required></td>
                        </tr>
                        <tr>
                            <td>Nombre:</td>
                            <td><input type="text" placeholder="Nombre" name="nombreCliente" style="width: 100%;" required></td>
                        </tr>
                        <tr>
                            <td>Dirección:</td>
                            <td><input type="text" placeholder="Dirección" name="direccion" style="width: 100%;"></td>
                        </tr>
                        <tr>
                            <td>Telefono</td>
                            <td><input type="text" placeholder="Telefono" name="telefono" style="width: 100%;" required></td>
                        </tr>
                        <tr>
                            <td>Email</td>
                            <td><input type="email" placeholder="Email" name="email" style="width: 100%;"></td>
                        </tr>
                    </table>
                    <div style="margin-top: 10px;">
                        <input type="submit" name="nuevoRegistro" value="Nuevo registro" class="button">
                        <input type="button" name="consultarRegistro" value="Consultar cliente" class="button">
                        <input type="submit" name="modificarRegistro" value="Modificar cliente" class="button"><br>
                        <input type="button" name="eliminarRegistro" value="Eliminar cliente" class="button">
                        <input type="reset" value="Reestablecer" class="button">
                        <input type="button" name="cerrarSesion" value="Cerrar sesión" class="button">
                    </div>
                </form>

// php

if(isset($_POST['consultarRegistro'])){
    $link = mysqli_connect("localhost", "root", "", "empresadb");
    $id = $_POST['id'];

    if($link){
        $consulta = mysqli_query($link, "select * from clientes where idCliente = '$id' limit 1");
        $nr = mysqli_num_rows($consulta);
        if($nr>0){
            $row = mysqli_fetch_array($consulta, MYSQLI_ASSOC);
            //$_post['nombreCliente'] = $row['nombre'];
        }
        else{
            echo "<script>alert(\"Cliente no existe con dicho id.\")</script>";
        }
    }
    else{
        echo "<script>alert(\"Conexión fallida.\")</script>";
    }
    mysqli_free_result($consulta);
    mysqli_close($link);
}
    
asked by Edward Hernando Guzmán Pérez 12.09.2018 в 02:17
source

1 answer

0

You can try the following: you assign the value of the variable to the value attribute of the input if it is set

...
<tr>
    <td>Nombre:</td>
    <td><input type="text" placeholder="Nombre" name="nombreCliente" style="width: 100%;" required value="<?php echo isset($row['nombre']) ? $row['nombre'] : '';?>"></td>
</tr>
...
    
answered by 12.09.2018 в 05:20