problem with checkbox and php

0

I have a check-box that the user will indicate respective months of a form, I can send it if you select January and February but if you select other months or January, March, December to say something, do not save it in the database.

<?php

if (
  $_POST['01'] == '01'
){
  $consulta = "
  INSERT INTO gastonocomun
  VALUES(
    '','$monto','$tipo',
    '2018-01-01','','',
    '','','','','','','','','','$gasto')
    ";
    $query = mysqli_query($conexion, $consulta);
  }

  else if (
    $_POST['01'] == '01' and 
    $_POST['02'] == '02'
  ){
    $consulta = "
    INSERT INTO gastonocomun
    VALUES(
      '','$monto','$tipo',
      '2018-01-01','2018-02-01','',
      '','','','','','','','','','$gasto')
      ";
      $query = mysqli_query($conexion, $consulta);
    } 

    else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03'
    ){
      $consulta = "
      INSERT INTO gastonocomun
      VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '','','','','','','','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
        $_POST['01'] == '01' and 
        $_POST['02'] == '02' and 
        $_POST['03'] == '03' and 
        $_POST['04'] == '04'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','','','','','','','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','','','','','','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05' and 
      $_POST['06'] == '06'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','2018-06-01',
        '','','','','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05' and 
      $_POST['06'] == '06' and 
      $_POST['07'] == '07'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','2018-06-01',
        '2018-07-01','','','','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05' and 
      $_POST['06'] == '06' and 
      $_POST['07'] == '07' and 
      $_POST['08'] == '08'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','2018-06-01',
        '2018-07-01','2018-08-01','','','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05' and 
      $_POST['06'] == '06' and 
      $_POST['07'] == '07' and 
      $_POST['08'] == '08' and 
      $_POST['09'] == '09'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','2018-06-01',
        '2018-07-01','2018-08-01','2018-09-01','','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05' and 
      $_POST['06'] == '06' and 
      $_POST['07'] == '07' and 
      $_POST['08'] == '08' and 
      $_POST['09'] == '09' and 
      $_POST['10'] == '10'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','2018-06-01',
        '2018-07-01','2018-08-01','2018-09-01',
        '2018-10-01','','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05' and 
      $_POST['06'] == '06' and 
      $_POST['07'] == '07' and 
      $_POST['08'] == '08' and 
      $_POST['09'] == '09' and 
      $_POST['10'] == '10' and 
      $_POST['11'] == '11'
      ){
        $consulta = "
        INSERT INTO gastonocomun
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','2018-06-01',
        '2018-07-01','2018-08-01','2018-09-01',
        '2018-10-01','2018-11-01','','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }

      else if (
      $_POST['01'] == '01' and 
      $_POST['02'] == '02' and 
      $_POST['03'] == '03' and 
      $_POST['04'] == '04' and 
      $_POST['05'] == '05' and 
      $_POST['06'] == '06' and 
      $_POST['07'] == '07' and 
      $_POST['08'] == '08' and 
      $_POST['09'] == '09' and 
      $_POST['10'] == '10' and 
      $_POST['11'] == '11' and 
      $_POST['12'] == '12'
      ){
        $consulta = "
        INSERT INTO gastonocomun 
        VALUES(
        '','$monto','$tipo',
        '2018-01-01','2018-02-01','2018-03-01',
        '2018-04-01','2018-05-01','2018-06-01',
        '2018-07-01','2018-08-01','2018-09-01',
        '2018-10-01','2018-11-01','2018-12-01','$gasto')
        ";
        $query = mysqli_query($conexion, $consulta);
      }
      ?>
<div class="form-group">
  <div class="form-group">
    <label for="monto">Fechas a cargar:</label><br>
    <input type="checkbox" name="01" value="01" > Enero<br>
    <input type="checkbox" name="02" value="02"> Febrero<br>
    <input type="checkbox" name="03" value="03"> Marzo<br>
    <input type="checkbox" name="04" value="04"> Abril<br>
    <input type="checkbox" name="05" value="05"> Mayo<br>
    <input type="checkbox" name="06" value="06"> Junio<br>
    <input type="checkbox" name="07" value="07"> Julio<br>
    <input type="checkbox" name="08" value="08"> Agosto<br>
    <input type="checkbox" name="09" value="09"> Septiembre<br>
    <input type="checkbox" name="10" value="10"> Octubre<br>
    <input type="checkbox" name="11" value="11"> Noviembre<br>
    <input type="checkbox" name="12" value="12"> Diciembre<br>
  </div>
</div>
    
asked by Anderson Rey 14.10.2018 в 08:36
source

2 answers

2

In principle, your logic in if is saying that the condition has to be If both , A and B (AND) is true then ...

Until that condition is met, for example in:

else if ($_POST['01'] == '01' and $_POST['02'] == '02' ){

 $consulta = "
 INSERT INTO gastonocomun  
 VALUES(
  '','$monto','$tipo',
  '2018-01-01','2018-02-01',
  '','','','','','','','','','','$gasto')";

$query = mysqli_query($conexion, $consulta);

}

Is that only if and only if Enero and Febrero are marked must be met. But if it turns out that you can skip a month like for example:

else if (
 $_POST['01'] == '01' and 
 $_POST['02'] == '02' and 
 $_POST['03'] == '03' and 
 $_POST['04'] == '04' ){

If you do not set Marzo here but% Abril , this code would not be executed, nor any other.

So on the one hand, in practice you should save marked checkboxes in an array, and this you do, saying in "Name" that it is an array.

<input type="checkbox" name="mes[]" value="01" > Enero<br>
<input type="checkbox" name="mes[]" value="02"> Febrero<br>
<input type="checkbox" name="mes[]" value="03"> Marzo<br>
<input type="checkbox" name="mes[]" value="04"> Abril<br>
<input type="checkbox" name="mes[]" value="05"> Mayo<br>
<input type="checkbox" name="mes[]" value="06"> Junio<br>
<input type="checkbox" name="mes[]" value="07"> Julio<br>
<input type="checkbox" name="mes[]" value="08"> Agosto<br>
<input type="checkbox" name="mes[]" value="09"> Septiembre<br>
<input type="checkbox" name="mes[]" value="10"> Octubre<br>
<input type="checkbox" name="mes[]" value="11"> Noviembre<br>
<input type="checkbox" name="mes[]" value="12"> Diciembre<br>

Then we go through the arrangement with a Foreach () and get the values of each Checkbox.

foreach ($array as $value) {

}

and depending on its value, you do the insertion in the BD as you handle it. You can separate the months in Variables separated by months, and you pass the variables to the query, a question that, if it does not exist, remains empty.

EDIT > $ Query

Tu Query should look something like this:

$consulta = "
INSERT INTO gastonocomun  
VALUES('','$monto','$tipo','$ene','$feb','$mar','$abr','$may','$jun','$jul',
'$ago','$sep','$oct','$nov','$dic','$gasto')
";
$query = mysqli_query($conexion, $consulta); 

The idea of foreach is to go through the Checkboxes to determine the ones that are selected and give the values to their respective variables ($ ene, $ feb, etc).

The conditional if, you should use it only to validate that a Checkbox has been selected, and thus send the Query.

    
answered by 14.10.2018 / 09:03
source
0

$mes= $_POST['mes[]'];


foreach ($array as $mes) {

$valor=$array;

switch ($valor[1]) {
case '01':
$ene;
break;
case '02':
$feb;
break;
case '03':
$mar;
break;
case '04':
$abr;
break;
case '05':
$may;
break;
case '06':
$jun;
break;
case '07':
$jul;
break;
case '08':
$ago;
break;
case '09':
$sep;
break;
case '10':
$oct;
break;
case '11':
$nov;
break;
case '12':
$dic;
break;
}


if ($_POST['01'] == '01' and $_POST['02'] == '02' and $_POST['03'] == '03' and $_POST['04'] == '04' and $_POST['05'] == '05' and $_POST['06'] == '06' and $_POST['07'] == '07' and $_POST['08'] == '08' and $_POST['09'] == '09' and $_POST['10'] == '10' and $_POST['11'] == '11' and $_POST['12'] == '12'  ){

$consulta = "INSERT INTO gastonocomun  VALUES('','$monto','$tipo','2018-01-01','2018-02-01','2018-03-01','2018-04-01','2018-05-01','2018-06-01','2018-07-01','2018-08-01','2018-09-01','2018-10-01','2018-11-01','2018-12-01','$gasto')";
$query = mysqli_query($conexion, $consulta);

}







}




?> 
    
answered by 14.10.2018 в 21:24