3
I need to check if the day doesn’t exist... do Insert, if yes, update.
I set an example in practice 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 Insert once, after having data in the database table does not re-enter and does not update.
I think the problem will be here, if(!$id_do_registro){
because after inserting values into the database does not re-enter or 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>";
}
Forehead without the exclamation:
if($id_do_registro) { ...
– Costamilam
Without the exclamation mark inserting as new lines the ones I should update and inserting the new records I made, the problem at this point is that it does not update
– Bruno
Puts
echo
s in various places and see if the variables are correct and where he is entering (if
orelse
), also test the SQL script right in phpMyAdmin or something to make sure that there was no missing or left any letters in some column– Costamilam
while($rows_cursos = mysqli_fetch_array($r1)) {
 $id_do_registro = $rows_cursos['Id'];
} echo $id_do_registro;
if(!$id_do_registro){
returns 186187188185185188. It is working– Bruno
if(!$id_do_registro){
 echo $id_do_registro;
 echo $string;
if(preg_match('/(\d{4}-\d{2}-\d{2})/', $string)){
here put these two Texts but does not return anything or inserts– Bruno
Put a
echo $id_do_registro
before theif
– Costamilam
while($rows_cursos = mysqli_fetch_array($R1)) { $id_do_registro = $rows_cursos['Id']; } echo $id_do_registro; if(!$id_do_registro){ retorno 186187188185188188.
– Bruno
Before the Insert I put these snippets
echo $id_do_registro; echo $string; 
$sql="INSERT INTO marcacao (arrachar) VALUES ('".$string."')";
and I detected a problem. The ids of what is already registered in the table are correct in what returns 1822018-04-24,Peq_lunch,10,Lunch,10,,Snack,10,1832018-04-25,,,,,Snack,15,Dinner,15,1842018-04-26,Peq_lunch,12,Lunch,12,,,,1852018-04-27,,,,,Snack,14,Dinner,14, but in the new registration repeats the id as I show 1852018-04-28,Lunch,10,Lunch,10,,,,,, where this id should be 324 which was the id that stayed on the table– Bruno
@Beginner tries the way I did
– Andrei Coelho