Skip to content
Advertisement

Insert into PDO with IF clause

I need some help with inserting some data into a sql server table using the IF clause. I have the return of an API that I insert in the table of the sql server. However, as I will schedule my code every day to get the update from the API to see if there is anything new, but every time it executes the code it inserts the same information into the table.

The most logical thing I thought was to make a select in the table where I will insert the data and create an IF, where it compares what is in the table and my return from the array. If it’s the same, it doesn’t do anything, if it’s different, insert it. However, the else is not being validated, even if the return of the IF is false.

I’ve tryed If, ifelse, If(): else: endif; but i couldnt.

Can someone help me please?

   $resultado = json_decode(curl_exec($curl),true);
    curl_close($curl);



//! Teste SELECT:

$qyS = Conexao::getConnection()->prepare("SELECT idSkill FROM LIVEPERSON_SKILLS");
$qyS->execute();
$selectResult = $qyS->fetchAll();

foreach ($selectResult as $indSelect => $retSelect){
    $idSkillComp = $retSelect['idSkill'];


//* 6 | Abertura do laço ['skills']
    foreach ($resultado as $indice => $infoSkills){
        $idSkill = $infoSkills['id'];
        $name = $infoSkills['name'];
        $deleted = $infoSkills['deleted'];
        $dh_insert = date("Y-m-d H:i:s");

//* 7 | Inserção dados no banco:

    if($idSkill == $idSkillComp){
        echo "error";
    }else{
    $qyI = Conexao::getConnection()->prepare("INSERT INTO LIVEPERSON_SKILLS 
        (DH_INSERT,idSkill,name,deleted) 
        VALUES (:d, :i, :n, :x)");

    $qyI->bindValue(":d",$dh_insert);
    $qyI->bindValue(":i",$idSkill);
    $qyI->bindValue(":n",$name);
    $qyI->bindValue(":x",$deleted);
    $qyI->execute();
        }


    }
  }

Advertisement

Answer

good afternoon. I identified what was going on in that case. In the second foreach I asked for the opening from each row found in the query (I believe it is the normal behavior of the foreach). Therefore, when inserting the data, I ended up inserting a duplicate, as it was 36 skillIds for each line found in the select.

I solved this issue leaving the rule inside the insert, changing it to insert select instead of making the select and then compare with what would be inserted.

Here’s how it went:

   $resultado = json_decode(curl_exec($curl),true);
    curl_close($curl);

    foreach ($resultado as $indInfoSkills => $infoSkills){
        $idSkillPrepare = array($infoSkills['id']);
        $idSkill =  implode(",",$idSkillPrepare);
        $name = $infoSkills['name'];
        $deleted = $infoSkills['deleted'];
        $dh_insert = date("Y-m-d H:i:s");

    $qyI = Conexao::getConnection()->prepare(
        "INSERT INTO LIVEPERSON_SKILLS (DH_INSERT,idSkill,name,deleted) 
            SELECT :dhInsert, 
                   :idSkill, 
                   :name, 
                   :deleted 
            WHERE NOT EXISTS (SELECT idSkill 
                              FROM LIVEPERSON_SKILLS 
                              WHERE idSkill = '$idSkill')");
        
    $qyI->bindValue(":dhInsert",$dh_insert);
    $qyI->bindValue(":idSkill",$idSkill);
    $qyI->bindValue(":name",$name);
    $qyI->bindValue(":deleted",$deleted);
    $qyI->execute();

    }

Thanks so much for everyone who tried to help me.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement