jeudi 7 octobre 2021

Form with checkbox to update sql table

I'm trying to update a MySQL table from an html form.

This is a piece of the form:

<div id="caricoModal" class="modal fade">
    <div class="modal-dialog">
        <form method="post" id="employeeForm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title"><i class="fa fa-plus"></i> Edit User</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="tipoOp" class="control-label">Operazione:</label>
                        <select id="tipo" name="tipo" class="form-control">
                            <option value="carico">Carico</option>
                            <option value="scarico">Scarico</option>
                        </select>
                    </div>
                    <div class="form-group">
                    <!-- Record list -->
                    <table border='0' style='border-collapse: collapse;' >
                        <tr style='background: whitesmoke;'>
                            <!-- Check/Uncheck All-->
                            <th>Username</th>
                            <th>Stato</th>
                        </tr>

    <?php 
    $query = "SELECT * FROM users where stato='0'";
    $result = mysqli_query($conn,$query);

    while($row = mysqli_fetch_array($result) ){
        $id = $row['id'];
        $stato = $row['stato'];
    ?>
                        <tr>
                            <!-- Checkbox -->
                            <td><input type='checkbox' name='update[]' value='<?= $id ?>' ></td>
                        
                            <td><?= $id ?></td>

                            <td><input type='number' name='stato_<?= $id ?>' value='<?= $stato ?>' ></td>

                        </tr>
    <?php
    }
    ?>
            </table>
    </div>

    <div class="form-group">                            
        <label for="name" class="control-label">Data Formulario</label>
        <input type="date" class="form-control" id="datafr" name="datafr" placeholder="Data Formulario" value="<?php echo date('Y-m-d'); ?>" required>          
    </div>

When i submit the dialog form, i call a method for add a new row in my table; i would to make a query based on the checkbox choices.

This is the method for add a new row:

public function addCarico(){
            
    $insertQuery = "INSERT INTO ".$this->empTable." 
                (data_fr, data_trasporto, codice_eer, stato_fisico, 
                classi_pericolo, formulario, produttore, trasportatore, 
                destinatario, quantità, note, carico, stato_carico) 
            VALUES ('".$_POST["datafr"]."', '".$_POST["datatr"]."', 
                    '".$_POST["eer"]."', '".$_POST["statof"]."', 
                    '".$_POST["pericolo"]."', '".$_POST["fr"]."', 
                    '".$_POST["prod"]."', '".$_POST["trasp"]."', 
                    '".$_POST["dest"]."', '".$_POST["qta"]."', 
                    '".$_POST["note"]."', '1', '0')";
    $isUpdated = mysqli_query($this->dbConnect, $insertQuery);  
    print_r($_POST['update']);
}

I tried to add this code:

if(isset($_POST['update'])){
    foreach($_POST['update'] as $updateid){

        $stato = $_POST['stato_'.$updateid];

        if($stato !=''){
            $updateUser = "UPDATE users SET 
                stato='1' 
            WHERE id=".$updateid;
            mysqli_query($con,$updateUser);
        }
        
    }
}

But i don't understand because the table isnt updated..




Aucun commentaire:

Enregistrer un commentaire