Problem to update several records with php and mysql combobox

1

Hello, this is the first time that I have programmed in php and I have a problem that I have not been able to solve.

I can not get the data in a mysql column updated using a combobox with 2 elements.

The page is like this :

and this is the code I have to make the update:

<?php
include_once 'conexion.php';
if(!empty($_POST)) {

    $queryResult = $pdo->query('SELECT FROM ejj');
    while ($row = $queryResult->fetch()) {
        ?>
        <br><br>
        <?php
        var_dump($row);
    }
    $id = $_POST['id'];
    $vigencia = $_POST['vigencia'];

    $sql = "UPDATE ejj SET Vigencia=:vigencia where id=:id";
    $query = $pdo->prepare($sql);
    $query->execute([
        'id' => $id,
        'vigencia' => $vigencia
    ]);
    $idValue = $id;
    $vigenciaValue = $vigencia;


    } else {
    $id = $_GET['id'];
    $sql= "SELECT * FROM  ejj WHERE id=:id";
    $query = $pdo->prepare($sql);
    $query->execute([
        'id'=>$id
    ]);
    $row = $query->fetch(PDO::FETCH_ASSOC);
    $idValue = $row['id'];
    $vigenciaValue = $row['vigencia'];
}
?>
<br><br>
<?php
echo 'Se realizo una modificación';
?>

and the code in the table is:

    

<form action="modificar.php" method="post">
    <table>
        <h1>Trabajadores vigentes</h1>
        <th>ID</th>
        <th>Nombre</th>
        <th>Apellido</th>
        <th>vigentes</th>

        <?php
        $queryResult = $pdo->query('SELECT * FROM ejj');

        while($row = $queryResult->fetch(PDO::FETCH_ASSOC)){;
        ?>
        <tr>
            <td><?php echo $row['id']?></td>
            <td><?php echo $row['nombre']?></td>
            <td><?php echo $row['apellido']?></td>
            <td><select name="vigencia" value<?php echo $cboValue?> >
                <option value="1">Vigente</option>
                <option value="0">No vigente</option>
            </select></td>
        </tr>

        <?php }; ?>
        <th><input type="submit" name="BtnModificar" value="Modificar"/></th>
        <input type="hidden" name="id" value="<?php echo $id?>">

    </table>
    <a href="agregar.php"><br>Agregar trabajadores</a>
</form>
</body>

    
asked by Pedro Prada 15.02.2018 в 15:48
source

1 answer

0

To see there are multiple failures, the first thing you should keep in mind is that each name="loquesea" must be unique within the form, that is, for each record you must generate a unique name for that record.

For example, with the name="" as an array using the id, that is something like name="vigencia[id_del_registro]" :

<?php include_once 'conexion.php'; ?>
<html>
<body>
    <form action="modificar.php" method="post">
        <table>
            <tr>
                <h1>Trabajadores vigentes</h1>
                <th>ID</th>
                <th>Nombre</th>
                <th>Apellido</th>
                <th>vigentes</th>
            </tr>
            <?php
                $queryResult = $pdo->query('SELECT * FROM ejj');
                while($row = $queryResult->fetch(PDO::FETCH_ASSOC)) {
            ?>
                    <tr>
                        <td><?= $row['id'] ?></td>
                        <td><?= $row['nombre'] ?></td>
                        <td><?= $row['apellido'] ?></td>
                        <td>
                            <!-- Aqui generamos un name como array, unico para cada fila con el id del registro -->
                            <select name="vigencia[<?= $row['id'] ?>]">
                                <option value="1">Vigente</option>
                                <option value="0">No vigente</option>
                            </select>
                        </td>
                    </tr>
            <?php
                };
            ?>
            <tr>
                <th>
                    <!-- 
                    este input hidden es innecesario y carece de sentido
                    <input type="hidden" name="id" value="<?php echo $id?>">
                     -->
                    <input type="submit" name="BtnModificar" value="Modificar"/>
                </th>        
            </tr>
        </table>
        <a href="agregar.php"><br>Agregar trabajadores</a>
    </form>
</body>

At the time of doing the update, we will have to go through the array vigencia to update all the records in the table. For example:

<?php
include_once 'conexion.php';
if(!empty($_POST)) {
    // Esta consulta esta mal, necesitas añadir los campos al select o *
    // En cualquier caso tampoco entiendo el sentido de esta consulta
    /*
    $queryResult = $pdo->query('SELECT * FROM ejj');
    while ($row = $queryResult->fetch()) {
        var_dump($row);
    }
    */
    // id es innecesario ya que pasamos los id como claves del array vigencia
    //$id = $_POST['id'];

    // vigencia, sera un array ya que asi lo declaramos en el formulario
    $vigencia = $_POST['vigencia'];

    // Preparamos la consulta
    $sql = "UPDATE ejj SET Vigencia=:vigencia where id=:id";
    $query = $pdo->prepare($sql);

    // Recorremos el array actualizando cada id y su vigencia
    foreach ($vigencia as $id => $value) {
        $query->execute([
            'id' => $id,
            'vigencia' => $value
        ]);
    }

    // Esto no se para que es y no tiene mucho sentido tampoco
    /*
    $idValue = $id;
    $vigenciaValue = $vigencia;
    */
}
?>

If, on the other hand, you only want to update a single record, the easiest way would be to create a form for each record.

    
answered by 15.02.2018 / 17:41
source