Value sent to the database becomes zero after sql return

Asked

Viewed 33 times

0

Good afternoon stack overflow.

I have the following situation. I need to calculate the amount of days a status takes to change. For example, the registration of my partner is with the status of "Registration Started", then passes to "Awaiting approval of registration", these facts occurred on 06/04/2017 and 10/04/2017 respectively. Let’s face it, that data took six days to change.

This sql below, already works perfectly

$selectDiasEntreStatus = "SELECT TIMESTAMPDIFF(DAY,MAX(prt_partner_historic_status._data_registro) +
          INTERVAL TIMESTAMPDIFF(YEAR, prt_partner_historic_status._data_registro, now()) YEAR ,now()) AS dias_entre_status
          FROM prt_partner_historic_status WHERE id_partner = :id_partner";
$stmt = DB::prepare($selectDiasEntreStatus);
$stmt->bindParam(":id_partner",$id,PDO::PARAM_INT);
$stmt->execute();
foreach ($stmt->fetchAll() as $keys => $values) {
    $this->diasStatus = $values->dias_entre_status;
 }

When I comment the code and have the returned value printed, the printed value is 6 as I put in the explanation.

When I run the whole process, which is the code below, mysteriously the value Zera.

 $updateDiasStatus = "UPDATE prt_partner_historic_status SET dias_entre_status = :dias_entre_status WHERE id_partner = :id_partner AND id_historic_status = :id_historic_status ";
 $stmt = DB::prepare($updateDiasStatus);
 $stmt->bindParam(":dias_entre_status",$this->diasStatus,PDO::PARAM_INT);
 $stmt->bindParam(":id_partner",$id,PDO::PARAM_INT);
 $stmt->bindParam(":id_historic_status", $this->idHistoricStatus,PDO::PARAM_INT);

 $stmt->execute();
 $stmt->closeCursor();

 if (self::getInstance()->commit()) {
     return true;
 }

There is no apparent error.

The whole code of the piece

public function updateStatus($id){
        $Exc = new ExceptionDatabase();
        try{

            self::getInstance()->beginTransaction();
            self::getInstance()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



            $updateStatus = "UPDATE prt_partner SET id_status = :id_status WHERE id_partner = :id_partner";

            $stmt = DB::prepare($updateStatus);
            $stmt->bindParam(":id_status",$this->idStatus,PDO::PARAM_STR);
            $stmt->bindParam(":id_partner",$id,PDO::PARAM_STR);

            $stmt->execute();


            $inserirStatus = "INSERT INTO prt_partner_historic_status(id_historic_status,id_status,id_partner,nota)
                                                      VALUES(NULL ,:id_status,:id_partner,:nota);";
            $stmt = DB::prepare($inserirStatus);
            $stmt->bindParam(":id_status",$this->idStatus, PDO::PARAM_INT);
            $stmt->bindParam(":id_partner",$id,PDO::PARAM_INT);
            $stmt->bindParam(":nota",$this->nota,PDO::PARAM_STR);


            $stmt->execute();

            $this->idHistoricStatus = DB::getInstance()->lastInsertId();


            if($this->idStatus == "3"){
                $inserirHistoricoRegistro = "INSERT INTO prt_partner_historic_register(id_historic_register,id_partner,id_historic_status,uniqueid_portal_papercut,login_portal_papercut,senha_portal_papercut,_ativo)
                                                                                VALUES(NULL,:id_partner,:id_historic_status,:uniqueid_portal_papercut,:login_portal_papercut,:senha_portal_papercut,:ativo);";


                $stmt = DB::prepare($inserirHistoricoRegistro);
                $stmt->bindParam(":id_partner",$id,PDO::PARAM_INT);
                $stmt->bindParam(":id_historic_status",$this->idHistoricStatus,PDO::PARAM_INT);
                $stmt->bindParam(":uniqueid_portal_papercut",$this->uniqueIDPapercut,PDO::PARAM_STR);
                $stmt->bindParam(":login_portal_papercut",$this->loginPapercut,PDO::PARAM_STR);
                $stmt->bindParam(":senha_portal_papercut",$this->senhaPapercut,PDO::PARAM_STR);
                $stmt->bindParam(":ativo",$this->ativo,PDO::PARAM_STR);
                $stmt->execute();
            }

            $selectDiasEntreStatus = "SELECT TIMESTAMPDIFF(DAY,MAX(prt_partner_historic_status._data_registro) +
          INTERVAL TIMESTAMPDIFF(YEAR, prt_partner_historic_status._data_registro, now()) YEAR ,now()) AS dias_entre_status
          FROM prt_partner_historic_status WHERE id_partner = :id_partner";
            $stmt = DB::prepare($selectDiasEntreStatus);
            $stmt->bindParam(":id_partner",$id,PDO::PARAM_INT);
            $stmt->execute();
            foreach ($stmt->fetchAll() as $keys => $values) {
               $this->diasStatus = $values->dias_entre_status;

            }
            $updateDiasStatus = "UPDATE prt_partner_historic_status SET dias_entre_status = :dias_entre_status WHERE id_partner = :id_partner AND id_historic_status = :id_historic_status ";
            $stmt = DB::prepare($updateDiasStatus);
            $stmt->bindParam(":dias_entre_status",$this->diasStatus,PDO::PARAM_INT);
            $stmt->bindParam(":id_partner",$id,PDO::PARAM_INT);
            $stmt->bindParam(":id_historic_status", $this->idHistoricStatus,PDO::PARAM_INT);

            $stmt->execute();


             $stmt->closeCursor();

            if (self::getInstance()->commit()) {
                return true;
            }
        }
        catch(PDOException $ex){

            self::getInstance()->rollBack();
            date_default_timezone_set('America/Sao_Paulo');
            $dataRegistro = date("Y-m-d H:i:s");
            $this->Caminho = explode("/", $_SERVER['SCRIPT_NAME']);
            $this->arquivo = $this->Caminho[count($this->Caminho)-1];
            $this->arquivoLog = 'log/erros.txt';
            $this->erro =  $ex->getCode();
            $this->mensagem  = $ex->getMessage();

            $Exc->setTipoLog(enum::Error);
            $Exc->setTitleLog($ex->errorInfo[0]);
            $Exc->setDescLog($ex->getMessage());
            $Exc->setDataRegistro($dataRegistro);
            $Exc->setArquivo($this->arquivo);
            $Exc->setArquivoLog($this->arquivoLog);
            $Exc->setErro($this->erro);
            $Exc->setMensagem($this->mensagem);

            $Exc->erro();
        }
    }
  • this update is inside the loop? Put the complete code to help you, this way it gets complicated.

  • No, it’s not inside the loop

  • @Jessika look if help haha

  • It helped yes, considering that I do not know the structure of your db, I will make some assumptions and hope to help you. Good as you are reading the results of select (with a foreach), you expect more than 1 result in the query, so the update should be within the loop. That way I’d leave the code like this:

  • foreach ($stmt->fetchAll() as $keys => $values) {
 $updateDiasStatus = "UPDATE prt_partner_historic_status SET dias_entre_status = :dias_entre_status WHERE id_partner = :id_partner AND id_historic_status = :id_historic_status ";
 $stmt = DB::prepare($updateDiasStatus);
 $stmt->bindParam(":dias_entre_status",$values->dias_entre_status,PDO::PARAM_INT);
 $stmt->bindParam(":id_partner",$id,PDO::PARAM_INT);
 $stmt->bindParam(":id_historic_status", $this->idHistoricStatus,PDO::PARAM_INT);
 $stmt->execute();
}

  • Well, I did that way too @Jessika but it didn’t work...

Show 2 more comments
No answers

Browser other questions tagged

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