0

i am stuck in a project when i try to log in my project it shows me

Fatal error: Call to a member function getid() on a non-object in C:\wamp\www\p3\controller\backend.php on line 31

I think its is problem related to my backend.php file+ AdminManager.php file+ Author.php file

this is my backend.php file

enter code here

<?php

// Chargement des classes
require_once('model/PostManager.php');
require_once('model/CommentManager.php');
require_once('model/AdminManager.php');

   //use \wwww\p3\model\AdminManager;

 function login()
  {
 require('view/frontend/connectView.php');
 }

function connexion($pseudo,$motdepasse)
 {
    //$postManager = new www\p3\model\PostManager();
   // Création d'un objet
$adminManager = new www\p3\model\AdminManager();
  //$adminManager = new AdminManager();
$resultat = $adminManager->connected($pseudo,$motdepasse);



if (!$resultat)
{
 echo  'Mauvais identifiant ou mot de passe !';
}
else
{


 $_SESSION['id'] = $resultat->getId();
 $_SESSION['pseudo'] = $resultat->getPseudo();
 header('Location: index.php?action=board');
 echo 'Vous êtes connecté !'; 






      }




   }

function board()
   {



 $commentManager = new www\p3\model\CommentManager();
 $comments = $commentManager->commentaireSignal();

 $postManager = new www\p3\model\PostManager();
 $posts = $postManager->getPosts();


// calling  the view
require('view/backend/addPostView.php');
}
   function eraseComment($commentId)
{
$commentManager = new www\p3\model\CommentManager();

$affectedLines = $commentManager->erase($commentId);

if ($affectedLines === false) {
    throw new Exception('commentaire d&eacute;ja effac&eacute; !');
}
else {
    header('Location: index.php');
     }
     }
function moderateComment($commentId)
{
$commentManager = new www\p3\model\CommentManager();

$affectedLines = $commentManager->moderate($commentId);

if ($affectedLines === false) {
    throw new Exception('commentaire d&eacute;ja mod&eacute;r&eacute; !');
}
else {
    header('Location: index.php');
   }
   }
function logout() {

// Suppression des variables de session et de la session
//$_SESSION = array();
 session_destroy();

// Suppression des cookies de connexion automatique
  header('Location: index.php');
 }
/**
 * method in call from the rounting page under action addPost 
 * @param $titre
 * @param $contenu
 */
 function addPost($titre, $contenu)
   {
$PostManager = new www\p3\model\PostManager();

$affectedLines = $PostManager->addPost($titre, $contenu);

if ($affectedLines === false) {
    throw new Exception('Impossible d\'ajouter le chapitre !');
}
else {
    header('Location: index.php?action=post&id=' . $postId);
   }
}
function erasePost($postId)
 {
$postManager = new www\p3\model\PostManager();

$affectedLines = $postManager->deletePost($postId);

if ($affectedLines === false) {
    throw new Exception('article d&eacute;ja effac&eacute; !');
}
else {
    header('Location: index.php');
  }
}
  function modifyPost($postId)
 {
    $postManager = new www\p3\model\PostManager();

$post = $postManager->getPost($postId);

require('view/backend/updatePostView.php');

 }
   function domodifyPost($id,$titre,$contenu)
 {
  $PostManager = new www\p3\model\PostManager();

$affectedLines = $PostManager->modifyPost($id,$titre,$contenu);

if ($affectedLines === false) {
    throw new Exception('Impossible d\'ajourner le chapitre !');
}
else {
    header('Location: index.php?action=board' );
  }
}

This is my AdminManager.php file

<?php
namespace www\p3\model;
require_once("model/Manager.php");
require_once("model/Author.php");

class AdminManager extends Manager
  {

   public function connected ($pseudo,$motdepasse)
  {
 $db= $this->dbConnect();
 $req = $db->prepare('SELECT id,nom,prenom,pseudo,motdepasse FROM auteur 
  WHERE pseudo=:pseudo AND motdepasse=:motdepasse');
 $req->execute(array('pseudo' => $pseudo,'motdepasse' => $motdepasse));
 $req->setFetchMode(\PDO::FETCH_CLASS|\PDO::FETCH_PROPS_LATE, "Author");
 $resultat = $req->fetch();
   return $resultat;
 }

}

This is my Author.php file

<?php
namespace www\p3\model;
/**
* Classe which represents an author
* @author David P.
* @version 0.1.0
*/
  class Author

  {
  /**
    * @var $id
    */
  private $id;
  private $firstname;
  private $lastname;
  private $pseudo;
  private $password;
// SETTERS
public function setId($id)

{
    $this->id = (int)$id;
}
public function setFirstname($firstname)

{
    $this->firstname = $firstname;
}
public function setLastname($lastname)

{
    $this->lastname = $lastname;
}
public function setPseudo($pseudo)

{
    $this->pseudo = $pseudo;
}
public function setPassword($password)

{
    $this->password = $password;
}

//GETTERS
public function getId()

{
    return $this->id ;
}
public function getFirstname()

{
    return $this->firstname ;
}
public function getLastname()

{
     return $this->lastname ;
}
public function getPseudo()

{
    return $this->pseudo ;
}
public function getPassword()

{
    return $this->password ;
}
}
   ?>
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • use $this->getId(); instead of $resultat->getId(); – rawathemant Jun 23 '18 at 12:12
  • Side note: never store passwords as plain text, use [`password_hash()`](http://php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://php.net/manual/en/function.password-verify.php). – AymDev Jun 23 '18 at 12:19

2 Answers2

2

i think it may cause of include and require

require_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions. The include_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.

If a.php is a php script calling b.php with require_once() statement, and does not find b.php, a.php stops executes causing a fatal error.

<?php
namespace www\p3\model;
include_once("model/Manager.php");
include_once("model/Author.php");

class AdminManager extends Manager
{

public function connected ($pseudo,$motdepasse)
{
$db= $this->dbConnect();
$req = $db->prepare('SELECT id,nom,prenom,pseudo,motdepasse FROM auteur
WHERE pseudo=:pseudo AND motdepasse=:motdepasse');
$req->execute(array('pseudo' => $pseudo,'motdepasse' => $motdepasse));
$req->setFetchMode(\PDO::FETCH_CLASS|\PDO::FETCH_PROPS_LATE, "Author");
$resultat = $req->fetch();
return $resultat;
}

}
also change these:-
include_once('model/PostManager.php');
include_once('model/CommentManager.php');
include_once('model/AdminManager.php');

Ajay Kadoula
  • 316
  • 2
  • 13
0

return $resultat; - not returning object.

Alex
  • 49
  • 6