I need to check if the day does not exist ... it does insert, if it does, it does update.
I leave the practice example of what I need:
if(ver se existe no banco){
.....if(){
..........faz insert
.....}
}else{
.... faz updade
}
The code I have:
if(!empty($_POST['arrachar'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['arrachar'] as $selected){
$string = implode(',', $selected);
preg_match('/(\d{4}-\d{2}-\d{2})/', $string, $dia);
$dia = $dia[0];
$sq="SELECT Id, arrachar FROM centrodb.marcacao WHERE LOCATE('$dia', arrachar) > 0";;
$r1 = mysqli_query($conn,$sq);
while($rows_cursos = mysqli_fetch_array($r1)) {
$id_do_registro = $rows_cursos['Id'];
}
if(!$id_do_registro){
if(preg_match('/(\d{4}-\d{2}-\d{2})/', $string)){
$sql="INSERT INTO marcacao (arrachar) VALUES ('".$string."')";
$r = mysqli_query($conn,$sql);
}else{ $sql1 = "UPDATE marcacao SET arrachar = '$string' WHERE Id = '$id_do_registro'";
$rs = mysqli_query($conn,$sql1);
}
}
}
}
It is not working properly, because it only does the insert once, after having data in the database table it does not re-insert and does not update.
I think the problem will be here, if(!$id_do_registro){
because after inserting values into the database it does not re-enter nor does the update
This is my html:
$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]' value='$marcado_pequeno_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]' value='$marcado_almoco_qtd' 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]' value='$marcado_dieta_qtd' 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]' value='$marcado_lanche_qtd' 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]' value='$marcado_jantar_qtd' 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]' value='$marcado_jantardie_qtd' style='width:65px; height: 22px' /> </font></center></td>";
}