Variable error

0

I have tried to find a mistake that is happening to me and I could not, I think it may be silly.  Error:

  

Notice: Undefined variable: st in   /var/www/html/questions/chk-gerent.php on line 143

The line he is pointing at is this:

$query_resp = mysql_query("SELECT *
     FROM 
        respostas 
             WHERE
               resp_usuario='$id_usuario'
                 AND resp_questionario='$quest'
                 AND resp_area='$area'
                 AND resp_subgrupo='$subgrupo'
                 AND resp_loja='$loja'
                 AND resp_pergunta_id='$idpergunta'")or die(mysql_error());
    while ($row = mysql_fetch_array($query_resp)) {
    $resposta   = $row['resp_resposta'];
    $st         = $row['resp_status'];
     }
    echo"<div class'form-group'>";
    echo"<div class='col-lg-10 col-lg-offset-2'>";
  

$ status = $ st;

     if ($status == "1") {
        echo"<center><h4>PERGUNTA JA RESPONDIDA | RESPOSTA:$resposta</h4></center>";
     } else {
        echo"<center><button  type='submit' class='btn btn-danger'>REPONDER</button></center><br />";
       }
        echo"</div>";
     echo"</div>";
      var_dump($query_resp);

Follow my full code if necessary:

<?php
ini_set('display_errors',true);
error_reporting(E_ALL);
?>

<?php
// A sessão precisa ser iniciada em cada página diferente
if (!isset($_SESSION)) session_start();  
// Verifica se não há a variável da sessão que identifica o usuário
if (!isset($_SESSION['usu_login'])) {
  // Destrói a sessão por segurança
  session_destroy();
  // Redireciona o visitante de volta pro login
  header("Location: logout.php"); exit;
}
?>
<?php 
require("conexao.php");
?>  
<html> 
  <head>
    <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>CHECKLIST LOJAS</title>
            <link href="includes/bootstrap.min.css" rel="stylesheet">
            <link href="includes/css/bootstrap.min.css" rel="stylesheet">
  </head> 
<body>
<?php
// SESSIONS
  //$_SESSION['usu_nome']      = $nome;
  //$_SESSION['usu_senha']     = $senha;
  //$_SESSION['usu_setor']     = $nivel;
  // $login = $_SESSION['usu_login'];
  $id_usuario = $_SESSION['usu_id'];
  $loja       = $_SESSION['usu_loja_id'];
  $quest      = $_SESSION['quest'];
?>
<?php
 $query_count = mysql_query("SELECT 
       COUNT(rp.id)  AS TOTAL_PERG      
           FROM 
              relacaoperg AS rp 
                                INNER JOIN perguntas AS p   ON rp.id_pergunta = p.perg_id
                                INNER JOIN area      AS a   ON rp.id_area     = a.area_id
                                INNER JOIN sub_area  AS sb  ON rp.id_subgrupo = sb.sub_area_cod
                                WHERE 
                                    a.area_id='1' AND sb.sub_area_cod='1'")or die(mysql_error());
    while ($row = mysql_fetch_array($query_count)) {
    $totalpergunta  = $row["TOTAL_PERG"];
    }
    ?>
    <?php
    $pergunta=1;
    while( $pergunta <= $totalpergunta ){
     $query_pesquisa = mysql_query("SELECT 
       rp.id            AS ORDEM,
       a.area_id        AS AREA,
       sb.sub_area_cod  AS SUBGRUPO,
       sb.sub_area_nome AS DESC_SUBGRUPO,
       p.perg_id        AS ID_PEGUNTA,
       p.perg_desc      AS DESC_PERGUNTA,
       rp.foto          AS FOTO          
           FROM 
              relacaoperg AS rp 
                                INNER JOIN perguntas AS p   ON rp.id_pergunta = p.perg_id
                                INNER JOIN area      AS a   ON rp.id_area     = a.area_id
                                INNER JOIN sub_area  AS sb  ON rp.id_subgrupo = sb.sub_area_cod

                                WHERE 
                                    a.area_id='1' AND sb.sub_area_cod='1' AND rp.id='$pergunta' ")or die(mysql_error());

    if (empty($query_pesquisa)) { 
    echo "Nenhum registro encontrado.";
    }
    while ($row = mysql_fetch_array($query_pesquisa)) {
    $ordem          = $row["ORDEM"];
    $area           = $row["AREA"];
    $subgrupo       = $row["SUBGRUPO"];
    $descsubgrupo   = $row["DESC_SUBGRUPO"];
    $idpergunta     = $row["ID_PEGUNTA"];
    $descpergunta   = $row["DESC_PERGUNTA"];
    $foto           = $row["FOTO"];
    }
    ?>
<br>
<div class="container">
<div id="div1" style="width:80%; margin:5 auto;">
      <center><div class="panel panel-danger"><div class="panel-heading"><?php echo"$descsubgrupo";?></div></div></center>
<div class="jumbotron">

 <form class="form-horizontal" action="index.php" method="GET">
  <fieldset>
    <legend>Pergunta <?php echo"$ordem";?>/<?php echo"$totalpergunta";?></legend>
    <div class="form-group">
      <h3><label><?php echo"$descpergunta";?></label></h3>
      <div class="col-lg-10">
        <div class="radio">
          <label>
            <input type="radio" name="optionsRadios" id="optionsRadios1" value="5">
            OTIMO
          </label>
        </div>
        <div class="radio">
          <label>
            <input type="radio" name="optionsRadios" id="optionsRadios1" value="3">
            REGULAR
          </label>
        </div>
        <div class="radio">
          <label>
            <input type="radio" name="optionsRadios" id="optionsRadios1" value="1">
            RUIM
          </label>
        </div>
        <div class="radio">
          <label>
            <input type="radio" name="optionsRadios" id="optionsRadios2" value="0">
            NAO APLICADO
          </label>
        </div>
    </div>

   <br />
<?php
 $query_resp = mysql_query("SELECT *
     FROM 
        respostas 
             WHERE
               resp_usuario='$id_usuario'
                 AND resp_questionario='$quest'
                 AND resp_area='$area'
                 AND resp_subgrupo='$subgrupo'
                 AND resp_loja='$loja'
                 AND resp_pergunta_id='$idpergunta'")or die(mysql_error());
    while ($row = mysql_fetch_array($query_resp)) {
    $resposta   = $row['resp_resposta'];
    $st         = $row['resp_status'];
     }
    echo"<div class'form-group'>";
    echo"<div class='col-lg-10 col-lg-offset-2'>";
    $status=$st;
     if ($status == "1") {
        echo"<center><h4>PERGUNTA JA RESPONDIDA | RESPOSTA:$resposta</h4></center>";
     } else {
        echo"<center><button  type='submit' class='btn btn-danger'>REPONDER</button></center><br />";
       }
        echo"</div>";
     echo"</div>";
      var_dump($query_resp);

?>    
  </fieldset>

 </form>
</div>
</div>
</div>


</body>
</html>
<?php $pergunta++; } ?>

What's the idea: if the user has already replied, I'll change the button by:

  

QUESTION JA ANSWERED | ANSWER: $ response

    
asked by anonymous 08.02.2016 / 10:58

1 answer

5
  

Notice: Undefined variable: st in   /var/www/html/questions/chk-gerent.php on line 143

" Undefined Variable $st on line 143 "

Line 143:

$status=$st;

The variable is being initialized inside a loop of repetition:

while ($row = mysql_fetch_array($query_resp)) {
$resposta   = $row['resp_resposta'];
$st         = $row['resp_status'];
 }

If the loopback is not executed, the $st variable will not be set.

By logical deduction, $row may be receiving value null or false .

$row = mysql_fetch_array($query_resp)

How to solve?

Create a more consistent logic by checking data integrity before running routines that rely on such data.

[edit]

Suggestion for implementation

if ($row = mysql_fetch_array($query_resp)) {
    foreach ($row as $v) {
        $resposta   = $v['resp_resposta'];
        $st         = $v['resp_status'];
    }
    echo"<div class'form-group'>";
    echo"<div class='col-lg-10 col-lg-offset-2'>";
    $status=$st;
    if ($status == "1") {
    echo"<center><h4>PERGUNTA JA RESPONDIDA | RESPOSTA:$resposta</h4></center>";
    } else {
    echo"<center><button  type='submit' class='btn btn-danger'>REPONDER</button></center><br />";
    }
    echo"</div>";
    echo"</div>";
    var_dump($query_resp);
} else {
    echo 'A consulta não encontrou nada.'; exit;
}
    
08.02.2016 / 11:24