Variable error

Asked

Viewed 68 times

0

I have tried to find a mistake that is happening to me and I could not, Error:

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

The line he’s 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 is the idea: if the user has already answered I will exchange the button for:

QUESTION NOW ANSWERED | ANSWER:$answer

  • Note: Already try changing from: $reply = $Row['resp_reply']; $st = $Row['resp_status']; to $reply = $Row["resp_reply"]; $st = $Row["resp_status"];

1 answer

5


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

"Undefined variable $st on line 143"

The line 143:

$status=$st;

The variable is being initialized within a repeat loop:

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

If the loop loop is not executed, the variable $st will not be defined.

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 executing routines that depend on such data.

[Edit]

Suggested 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;
}
  • I’m sorry but I don’t understand:How to solve? Create a more consistent logic by checking the integrity of the data before executing routines that depend on such data.

  • exactly that, try to use logic and realize everything that Daniel said.

  • @porfavorexcluam me who answered the question lost time paw you help. And he tried to explain. If you don’t understand, say, if you think it was rude, tell the other member of the community or a moderator. You’ll find it’s worth learning from what the members of the community know....

Browser other questions tagged

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