Problem changing using pdo

-1

I'm developing a system for college and am having the following problem

Notice: Undefined property: stdClass::$Disciplina in C:\Users\lucas.vianna\Desktop\SGA\db\turmaDAO.php on line 64

Notice: Undefined variable: getDisciplina in C:\Users\lucas.vianna\Desktop\SGA\turmas.php on line 41

Fatal error: Uncaught Error: Method name must be a string in C:\Users\lucas.vianna\Desktop\SGA\turmas.php:41 Stack trace: #0 {main} thrown in C:\Users\lucas.vianna\Desktop\SGA\turmas.php on line 41

Line 41: $disciplina = $resultado->$getDisciplina();
Line 64: $turma->setDisciplina($rs->Disciplina);

Follow the update code in DAO :

     public function atualizar($turma){
        global $pdo;
        try {
            $statement = $pdo->prepare("SELECT idTurma, Disciplina_idDisciplina,Nome FROM turma WHERE idTurma = :id");
            $statement->bindValue(":id", $turma->getIdTurma());
            if ($statement->execute()) {
                $rs = $statement->fetch(PDO::FETCH_OBJ);
                $turma->setIdTurma($rs->idTurma);
                $turma->setDisciplina($rs->Disciplina);
                $turma->setNome($rs->Nome);
                return $turma;
            } else {
                throw new PDOException("Erro: Não foi possível executar a declaração sql");
            }
        } catch (PDOException $erro) {
            return "Erro: ".$erro->getMessage();
        }
    }

Follow the select code in the view

if (isset($_REQUEST["act"]) && $_REQUEST["act"] == "upd" && $id != "") {

    $turma = new turma($id, '','');

    $resultado = $object->atualizar($turma);
    $nome = $resultado->getNome();
    $disciplina = $resultado->$getDisciplina();
}



<select name="disciplina"><?php
    $query = "SELECT * FROM Disciplina order by Nome;";
    $statement = $pdo->prepare($query);
    if ($statement->execute()) {
        $result = $statement->fetchAll(PDO::FETCH_OBJ);
        foreach ($result as $rs) {
            if ($rs->idDisciplina == $disciplina) {
                echo "<option value='$rs->idDisciplina' selected>$rs->Sigla</option>";
            } else {
                echo "<option value='$rs->idDisciplina'>$rs->Sigla</option>";
            }
        }
    } else {
        throw new PDOException("Erro: Não foi possível executar a declaração sql");
    }
    ?>
</select>
    
asked by anonymous 09.04.2018 / 15:45

1 answer

0

Notice: Undefined property: stdClass :: $ Discipline: Means that Class is not defined; Notice: Undefined variable: getDiscipline: Derived from the first error, obviously if the class is not defined the methods also not; Fatal error: Uncaught Error: Method name must be a string: You are probably creating a function with an invalid name with special characters or number: PHP Function Names: Never start with special characters or numbers, examples: "{$, #,%, 1}".

And in relation to global $ pdo it is not good practice to have a class with the connection to the data bank in a global way.

I consider creating a Singleton Class:

class DBConnection{
   private static $instance;
   private function __constructor(){}
   private function __clone(){}

   public function getDBaseConnection(){
      if(!isset(static::$instance)){
          $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
          static::$instance = new PDO(paramters, $pdo_options);
      }

      return static::$instance;
   }

}

$conneciton  = DBConnection::getDBaseConnection();
$connection->query(param);

// It will be the same class to be used in the whole project and need only be instantiated; If you want something more flexible like for example to use another white data you have the option of passing the bank name as parameter to getDBaseConnection ($ dbName);

    
09.04.2018 / 17:33