Edit rows with php

0

I just make a table of users with the functions of delete, edit and add, I have the functions of adding, deleting and editing, the problem is when I give edit to a row I see the fields in the row with the id = 1, this in any row, and what I want is that when I click on the row with the id = 5 I will see the data with that id and so depending on the row the data will appear

this is the index.php code

<table class="table table-striped">
    <thead>
        <tr>
            <th>ID</th>
            <th>FOTO</th>
            <th>NOMBRE</th>
            <th>APELLIDO</th>
            <th>TELEFONO</th>
            <th>DIRECCION</th>
            <th>EMAIL</th>
            <td></td>
            <td></td>
        </tr>
    </thead>    
<tbody>
 <?php
foreach ($rows as $row) {
    ?>
    <tr>
        <td><?php echo $row['id'];?></td>
        <td><?php echo $row['Foto']; ?></td>
        <td><?php echo $row['Nombre'];?></td>
        <td><?php echo $row['Apellido'];?></td>
        <td><?php echo $row['Telefono'];?></td>
        <td><?php echo $row['Direccion'];?></td>
        <td><?php echo $row['Email'];?></td>
        <td><?php echo "<button type='button' class='btn btn-danger' ><a href='eliminar.php?id=".$row["id"]."'>Eliminar</a></button>"?></td>
        <td>
       <div class="container">
           <?php echo '<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#Modal" id=' . $row['id'] . '" >Editar</button>' ?>
             <!--<?php /*echo *///'<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#Modal" id=' . $row['id'] . '" >Editar</button>' ?>-->
            <div class="modal fade" id="Modal" role="dialog">
                <div class="modal-dialog">

                    <div class="modal-content">

                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                            <div class="modal-body">
                                <fieldset style="width:400px">
                                    <form action=" " method="post" emctype="multipart/form data">
                                        <table class="table">

                                            <!--<tr>
                                                <td><label for="id">Id:</label></td>
                                                <td><input type="text" name="id" id="id" value='<?php// echo $row//['id'];?>'></td>
                                            </tr>-->

                                            <tr>
                                                <td><label for="nombre">Foto:</label></td>
                                                <td><input name="myFile" type="file" value='<?php echo $row['Foto'];?>' ></td>
                                            </tr>
                                            <tr>
                                                <td><label for="email">Nombre:</label></td>
                                                <td><input type="text" name="nombre" id="nombre" value='<?php echo  $row['Nombre'] ;?>'></td>
                                            </tr>
                                            <tr>
                                                <td><label for="Apellido">Apellido:</label></td>
                                                <td><input type="text" name="apellido" id="apellido" value='<?php echo $row['Apellido'];?>'/></td>
                                            </tr>
                                            <tr>
                                                <td><label for="Telefono">Telefono:</label></td>
                                                <td><input id="telNo" name="telNo" type="tel" value='<?php echo $row['Telefono'];?>' /></td>
                                            </tr>
                                            <tr> 
                                                <td><label for="Direccion">Direccion:</label></td>
                                                <td><input type="text" name="direccion" id="direccion" value='<?php echo $row['Direccion'];?>'/></td>
                                            </tr>
                                            <tr>
                                                <td><label for="Email">Email:</label></td>
                                                <td><input type="email" class="form-control" id="email" placeholder="Enter email" name="email"  value='<?php echo $row['Email'];?>'/></td>
                                            </tr>
                                            <tr>
                                                <td>
                                                <button id="actualizar" class="btn btn-primary">Actualizar</button>
                                                </td>
                                            </tr>
                                        </table>
                                    </form>
                                </fieldset>   
                            </div>
                            <div class="modal-footer">
                               <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            </div>
                    </div>   
                </div>
            </div>
        </div>
        </td>
    </tr>
    <?php } ?>
</tbody>
</table>

and here is the code edit.php

<?php
    require_once 'connection.php';
    $con = dbConnect();

    $query = "SELECT * FROM agenda WHERE id = '$id'";
?>
    
asked by José María Vela Vargas 26.04.2018 в 16:50
source

2 answers

1

You must specify which Modal you must open by clicking on the "Edit" button. In this case you must add the id in the "data-target" attribute

<?php echo '<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#Modal_' . $row['id'] . '" id=' . $row['id'] . '" >Editar</button>' ?>

And you must add a unique id to each Modal within your div.

<div class="modal fade" id="Modal_<?= $row['id']; ?>" role="dialog">

I hope you find it useful.

    
answered by 26.04.2018 в 16:58
0

Your problem is in this line:

<div class="modal fade" id="Modal" role="dialog">

You are generating all the manners with the same id. The ids must be unique. When you press the button, it will show the first instance of that id, which is row one. The solution is to give unique ids to each modal as you did with the buttons.

    
answered by 26.04.2018 в 16:56