Inserts blank lines when inserting foreach

0

I am inserting in the same column all the data of my form, which contains checkboxes and inputs type="number":

$calendar .= "<td bgcolor='#F5F5F5' align='center' data-semana=''><center><font size='2px'/> 
    <input type='checkbox' name='arrachar[$year, $month, $day][dia]' value='$year-$month-$day' $marcado_data $disabled> $year-$month-$day <br /> 
    <input type='checkbox' name='arrachar[$year, $month, $day][OpcaoA]' value='Peq_Almoço' $marcado_pequeno $disabled> Peq. Almoço <input $disabled type='number' name='arrachar[$year, $month, $day][Qtd]' style='width:65px; height: 22px' /> <br /> 
    <input type='checkbox' name='arrachar[$year, $month, $day][opcaoB]' value='Almoço' $marcado_almoco $disabled> Almoço <input $disabled type='number' name='arrachar[$year, $month, $day][Qtd1]' style='width:65px; height: 22px' /> <br /> 
    <input type='checkbox' name='arrachar[$year, $month, $day][opcaoC]' value='Almoço_(Dieta)' $marcado_dieta $disabled> Almoço (Dieta) <input $disabled type='number' name='arrachar[$year, $month, $day][Qtd2]' style='width:65px; height: 22px' /> <br /> 
    <input type='checkbox' name='arrachar[$year, $month, $day][opcaoD]' value='Lanche' $marcado_lanche $disabled> Lanche <input $disabled type='number' name='arrachar[$year, $month, $day][Qtd3]' style='width:65px; height: 22px' /><br /> 
    <input type='checkbox' name='arrachar[$year, $month, $day][opcaoE]' value='Jantar' $marcado_jantar $disabled> Jantar <input $disabled type='number' name='arrachar[$year, $month, $day][Qtd4]' style='width:65px; height: 22px' /> <br /> 
    <input type='checkbox' name='arrachar[$year, $month, $day][opcaoF]' value='Jantar_(Dieta)' $marcado_jantardie $disabled> Jantar (Dieta) <input $disabled type='number' name='arrachar[$year, $month, $day][Qtd5]' style='width:65px; height: 22px' /> </font></center></td>";
}

I'm inserting into the table this way:

if(!empty($_POST['arrachar'])){
    // Loop to store and display values of individual checked checkbox. 
    foreach($_POST['arrachar'] as $selected){ 
        $string = implode(',', $selected);
        $sql="INSERT INTO marcacao (arrachar) VALUES('$string')";
        $r = mysqli_query($conn,$sql);      
    }
}

Here's how you're entering the data:

But I should only insert the first line and should stop, because it was the only day I marked meals, but inserts the remaining days until the end of the month even though they are empty. How can I solve the problem?

    
asked by anonymous 19.04.2018 / 18:50

1 answer

1

You can check if the $string variable has a date, then it will only insert data that has the date in yyyy/MM/dd format, as your question demonstrates.

Place the line that makes the INSERT inside a if with preg_match using the regex /(\d{4}-\d{2}-\d{2})/ :

if(preg_match('/(\d{4}-\d{2}-\d{2})/', $string)){
    $sql="INSERT INTO marcacao (arrachar) VALUES('$string')";
}

This regex will check if the $string has the pattern 4 digits + hyphen + 2 digits + hyphen + 2 digits     

22.04.2018 / 20:07