Testing a condition before UPDATE, with PHP - Mysql

Asked

Viewed 927 times

0

I’m trying the following: I need the code below first to check that the record that is trying to be finalized (Pass status 1 to 2) is not finished. 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, gives the message that it can not finish and such, but when I try to finalize a record that is active, which is status 1 in the bank and which should then UPDATE run and pass to 2, does not contain anything. Note: (Running only UPDATE without test 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");

}
?>
  • That if if($linha['STATUS_PROJETO'] > '0') shouldn’t be if($linha['STATUS_PROJETO'] == 2). There is a STATUS_PROJETO zero?

  • It really should be like this, but I adjusted and now returns a blank page (only with PDO connection error, but this is because of the method I used). @rray

  • But you’re not using the PDO.

  • No. To with the old connection, but this error ta giving on other pages, it would not be the problem.

  • I could update the question code. It’s a bit confusing, look at the if($id){ when it comes to Else supposedly has no id in this case you update, update which id? When testing all comments header(....);

1 answer

1


I believe it would be right 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");
    }
}
  • @Tiagoib was just like that?

Browser other questions tagged

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