Home > Mobile >  Form with checkbox to update sql table
Form with checkbox to update sql table

Time:10-08

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..

CodePudding user response:

When you open the form tag the action attribute is missing:

<form method="post" id="employeeForm" action="pagename.php">

CodePudding user response:

do various things to check the update query.

  1. check that you are receiving in the variable $ _POST ['update']

  2. Print the query update that you are going to perform

  3. Check that the query is correct

If all this is OK, it is possibly the connection to the database that is not working.

  • Related