Test a condition before doing UPDATE, with PHP - MySQL

0

I'm trying the following: I need the code below to first check that the record that is trying to be finalized (Skip status 1 to 2) is no longer finalized. If you do not follow the NORMAL UPDATE. But what happens is that when I try to finalize a record that is already in the bank with status 2 that is Finalized, it gives the message that it can not finalize and such, but when I try to finalize a record that is active, which is status 1 in the bank and that should then the UPDATE execute and move to 2, nothing happens. Note: (Running only the UPDATE without testing it works normal.)

<?php 
@ini_set('display_errors', '1');
error_reporting(E_ALL);

if(file_exists("../config.php")) {
    require "../config.php";        
} else {
    echo "Arquivo config.php nao foi encontrado";
    exit;
}


$id = $_GET["id"];
settype($id, "integer");

Abre_Conexao();
//status
if($id){

/*STATUS: 1 = ATIVO, 2 = FINALIZADO*/

$sqlstatus = "SELECT * FROM projetos WHERE STATUS_PROJETO = '2' AND COD_PROJETO = $id";
$resultadost = mysql_query($sqlstatus) or die(mysql_error());

while($linha = mysql_fetch_assoc($resultadost)) {
    if($linha['STATUS_PROJETO'] > '0')
    echo "Atividade já estava finalizada";

}
} else {
    mysql_query("UPDATE projetos SET status_projeto = '2' where cod_projeto = $id");
    mysql_close();
    header("Location: projetos.php");

}
?>
    
asked by anonymous 16.10.2015 / 14:18

1 answer

1

I believe that correct would be to update the project only when an id is found and its status is 1 .

if($id){
    $sqlstatus = "SELECT * FROM projetos WHERE COD_PROJETO = $id";
    $resultadost = mysql_query($sqlstatus) or die(mysql_error());
    $projeto = mysql_fetch_assoc($resultadost));

    if($projeto['STATUS_PROJETO'] == '2')
        echo "Atividade já estava finalizada";
    } else {
        mysql_query("UPDATE projetos SET status_projeto = '2' where cod_projeto = $id")
        or die(mysql_error());
        mysql_close();
        //header("Location: projetos.php");
    }
}
    
16.10.2015 / 15:17