save an array of forms with checkbox

1

I have to save several forms at the same time and each form contains multiple checkboxes that when they are all selected, they are saved correctly but when one of them is empty, it marks an error someone helps me These are the forms you sent at the same time:

  <tbody>
                  <tr class="active">
                    <input type="hidden" id="id_us_res" name="id_us_res[]" value="<?php echo $row1['id_usuario_reservacion']; ?>">
                    <td>
                      Usuario<br>
                      <?php echo $row1['nombre_usuario']; ?>
                      <input type="hidden" id="nom_usu" name="nom_usu[]" value="<?php echo $row1['nombre_usuario']; ?>">
                    </td>
                    <td>
                      Personaje<br>
                      <?php echo $row1['nombre_personaje']; ?>
                      <input type="hidden" id="nom_per" name="nom_per[]" value="<?php echo $row1['nombre_personaje']; ?>">
                    </td>
                    <td>
                      Vacunas<br>
                      <input type="checkbox" id="pildora[]" name="pildora[]" value="pildora"> Pildora<br>
                      <input type="checkbox" id="antivirus[]" name="antivirus[]" value="antivirus"> Antivirus<br>
              </tbody>
            </table>
          </div><!-- /example -->
            <?php } ?>
            <?php if($_SESSION['tipo_usuario']==1) { ?>
            <button type="submit" class="btn btn-danger">Guardar Partida </button>
            <?php } ?>
            </form>

This is the error that comes out:

Here is my code:

$id_us_res = $_GET['id_us_res'];
$nom_usu = $_GET['nom_usu'];
$nom_per = $_GET['nom_per'];
$pildora = isset($_GET['pildora']) ? $_GET['pildora'] : 0;
$antivirus = isset($_GET['antivirus']) ? $_GET['antivirus'] : 0;


for($i = 0; $i<sizeof($id_us_res); ++$i)
{
echo "<br>" . $sql = "INSERT INTO ranking (id_usuario_ranking, nombre_usuario, nombre_personaje, Pildora, Antivirus)
           VALUES ('$id_us_res[$i]','$nom_usu[$i]','$nom_per[$i]','$pildora[$i]','$antivirus[$i]')";
           $result = $mysqli->query($sql);

}
    
asked by Carlo Caraza Mendoza 03.03.2018 в 02:45
source

2 answers

1

In the cycle for you have to review with isset if you have a value before using it in the query. Something like this:

for ($i = 0; $i<sizeof($id_us_res); ++$i) {

    $pil = isset($pildora[$i])   ? $pildora[$i]   : '';
    $ant = isset($antivirus[$i]) ? $antivirus[$i] : '';

    $sql = "INSERT INTO ranking 
            (id_usuario_ranking, nombre_usuario, nombre_personaje, Pildora, Antivirus)
            VALUES
            ('$id_us_res[$i]','$nom_usu[$i]','$nom_per[$i]','$pil','$ant')";

}
    
answered by 05.03.2018 / 02:49
source
1

Undefined offset means that you are requesting a position that does not exist within an array, on line 28 of your code (I do not know what line 28 of your code is) you are doing something like:

$a = $miarreglo[2];

But my $ miarreglo does not have position 2.

    
answered by 03.03.2018 в 06:57