Inserts blank lines when inserting foreach

Asked

Viewed 108 times

0

I am entering 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 it 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 I show you how you are entering the data: inserir a descrição da imagem aqui

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

  • When you say you use the same insert, you include the column for the values of the correct "number" inputs?

  • No, I’m trying to enter all the data in the same column, in the array column where I enter the checkboxes

  • I changed the name of the inputs type="number" and I use exactly the same name as the one I posted above, but insert many empty lines as shown in the question.

  • if Voce is concatenating all the $Selected variables inside the $string, you need to do the Insert only after q it exits the is...

  • I see the code if(!empty($_POST['arrachar'])){ should be inside the foreach and not before it, then you will check whether each control is empty or not. As it stands, only the first is checked.

  • But still inserts the other lines, inserts this way:1 2018-04-21,Peq_Almoço,10,Almoço,10,,Lanche,10,,&#xA;2 ,,,,,&#xA;3 ,,,,,&#xA;4 ,,,,,&#xA;5 ,,,,,&#xA;6 ,,,,,&#xA;7 ,,,,,&#xA;8 ,,,,,&#xA;9 ,,,,,&#xA;10 ,,,,,, should only insert the first line

Show 1 more comment

1 answer

1


You can check if the variable $string has a date, there will only enter the data that have the date in the format yyyy/MM/dd, 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 whether in $string has the standard 4 digits + hyphen + 2 digits + hyphen + 2 digits.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.