check whether the day exists with if

3

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>";

}
    
asked by anonymous 23.04.2018 / 11:47

2 answers

4

You were trying to UPDATE within IF which checks to see if ID does not exist. It will not update because the id does not exist. The sure thing is that this code is in an ELSE of this IF. Type this:

if(!$id_do_registro){ // se não existir id do registro
    // inclua
} else {
   // muda através do id
}

In addition, you have the error in your while, also quoted by @fernando.

EDIT

As discussed in chat

Your code looks like this:

if(!empty($_POST['arrachar'])){     
    // Loop to store and display values of individual checked checkbox. 
    $stringSelect = "SELECT SUBSTRING_INDEX(arrachar, ',', 1) as data, Id, arrachar FROM marcacao WHERE ";
    $stringNew = "INSERT INTO marcacao (arrachar) VALUES ";
    $arrayValues = array();
    $statusNew = false;
    foreach($_POST['arrachar'] as $selected){

        $string = implode(',', $selected);
        $vazio = str_replace(",","",$string);
        $date = explode(",",$string);
        $data = $date[0];
        if($vazio != ""){
            $stringSelect .= "SUBSTRING_INDEX(arrachar, ',', 1) = '$data' OR ";
            $arrayValues[] = array(
                "data" => $data,
                "arrachar" => $string
            );
        }

    }
    $stringSelect = substr($stringSelect, 0, -3);
    $r1 = mysqli_query($conn,$stringSelect) or die(mysqli_error($conn));
    $index = array();
    if(mysqli_num_rows($r1) > 0){
        while($rows_cursos = mysqli_fetch_array($r1)){
            for($i=0; $i < count($arrayValues); $i++){
                if($rows_cursos['data'] == $arrayValues[$i]['data']){
                    if($rows_cursos['arrachar'] != $arrayValues[$i]['arrachar']){
                        $id = $rows_cursos['Id'];
                        $strin = $arrayValues[$i]['arrachar'];
                        $sql1 = "UPDATE marcacao SET arrachar = '$strin' WHERE Id = '$id'";
                        $rs = mysqli_query($conn,$sql1) or die(mysqli_error($conn));                        
                    }
                    $index[] = $i;
                }
            }
        }
    }

    if(isset($arrayValues[0])){
        for($i = 0; $i < count($arrayValues); $i++){
            if(!in_array($i,$index)){
                $stringNew .= "('".$arrayValues[$i]['arrachar']."'),";
                $statusNew = true;
            }
        }

        if($statusNew){
            $stringNew = substr($stringNew, 0, -1);
            mysqli_query($conn,$stringNew) or die(mysqli_error($conn));
        }
    }


}
    
23.04.2018 / 13:17
3

You have a problem using variables. Check for example that within a while you make an assignment of values in a variable:

while($rows_cursos = mysqli_fetch_array($r1)) {
    $id_do_registro = $rows_cursos['Id'];
}

The $ log_id will always receive the last record from the 'Id' index of the $ rows_path array.

The IF is supposed to be simple, as put into the code but you need the right variables for it.

Possibly you would have to include the IF within the While but without more information about parameters sent there is no way to analyze it better.

Here's how it would look:

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);
                }
            }    
        }
    }
}

Indicate in the code the used logic that makes it easier to see what's going on. The ideal would be to have everything separated with a method that would try to insert the records in the DB. In this method it would receive a record or an array of records (it depends on the business) and for each one (within a loop) it would check whether it already exists to update or otherwise insert.

    
23.04.2018 / 13:05