Show edit of an existing record in a table in php

0

I have a database to which I am creating a web interface with php and html, something simple.

In a php, I visualize a list of all the users of the database, to which I have added edit and delete buttons.

The edit links to another php, which shows a form to fill out. What I can not do is that the previous data that the user has in the table appears in the values of the form.

Thanks in advance.

<?php

    $OK = true; 
    $usuario="root";
    $contrasena="";
    $urlConexion='mysql:host=localhost;dbname=webdesigner';
    try{
        $conexion=new PDO($urlConexion, $usuario, $contrasena);
        $conexion->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        if (isset($_GET['id_user'])) {
            $sql = 'SELECT * FROM users WHERE id_user = ?';
            $stmt = $conexion->prepare($sql);
            $result = $stmt->execute(array($_GET['id_user']));
            $row = $stmt->fetch();
            if (empty($row)) {
                $result = "No se encontraron resultados.";
            }
        }
        if (array_key_exists('update', $_POST)) {
            $sql = 'UPDATE users SET username = ?, password = ?, email = ?, usertype = ?, company = ?, phone = ? WHERE id_user = ?';
            $stmt = $conexion->prepare($sql);
            $OK = $stmt->execute(array($_POST['username'],$_POST['password'],$_POST['email'],$_POST['usertype'],$_POST['company'],$_POST['phone'],$_GET['id_user']));
            $error = $stmt->errorInfo();
        if (!$OK) {
            echo $error[2];
        } else {
            echo 'El registro se ha actualizado correctamente.';

        }
    }

        }catch(Exception $e){
            echo ($e-GetMessage());
            die("<h3>Se ha producido una excepción</h3>");
        }

?>

<?php
        if(!$OK) :
            echo "";
        else :
?>


        <fieldset>
            <legend>Editar datos del usuario</legend>
            <form action="" method="post">
                <br/>
                <table>
                    <tr>
                        <td><label for="id_user">ID:</label></td>
                        <td><input type="text" name="id_user" id="id_user" disabled="disabled" value="<?php echo $row['id_user'];?>"></td>
                    </tr>
                    <tr>
                        <td><label for="username">Username:</label></td>
                        <td><input type="text" name="username" id="username" value="<?php echo $row['username'];?>"></td>
                    </tr>
                    <tr>
                        <td><label for="password">Password:</label></td>
                        <td><input type="text" name="password" id="password" value="<?php echo $row['password'];?>"></td>
                    <tr>
                        <td><label for="email">Email:</label></td>
                        <td><input type="email" name="email" id="email" value="<?php echo $row['email'];?>"></td>
                    </tr>
                    <tr>
                        <td><label for="company">Company:</label></td>
                        <td><input type="text" name="company" id="company" value="<?php echo $row['company'];?>"/></td>
                    </tr>
                    <tr>
                        <td><label for="phone">Phone:</label></td>
                        <td><input type="text" name="phone" id="phone" value="<?php echo $row['phone'];?>"></td>
                    </tr>
                    <tr>
                        <td><label for="usertype">Usertype:</label></td>
                        <td><input type="text" name="usertype" id="usertype" value="<?php echo $row['usertype'];?>"/></td>
                    </tr>

                </table>
                <br/>   
                <input type="submit" name="update" value="Actualizar"/>
            </form>
        </fieldset>



    <?php endif;?>


    <input type="button" value="Volver" onClick="location.href='listarusuarios.php'"/>
</body>     

    
asked by Ángel 14.02.2017 в 15:52
source

1 answer

0

The result of your query is an array with all the rows that fit. Even if it's just a row, it's an array. It means that instead of using $ row, you have to use $ row [0].

    
answered by 16.02.2017 в 20:02