-1

I use PHP version 7 and i get always this error, when i creat a news article:

Fatal error: Uncaught Error: Call to a member function setAuthor() on string in D:\xampp\htdocs\entwicklung\tools\adminlogin\lib\mod\news\newsForm.php:14 Stack trace: #0 {main} thrown in D:\xampp\htdocs\entwicklung\tools\adminlogin\lib\mod\news\newsForm.php on line 14

Here i creat my object and would save the article in my database:

if(isset($_POST["publish"]))
{
    $news = new News();
    $title = $news->setTitle($db, $_POST["ueberschrift"]);
    $news =  $news->setNews($db, $_POST["artikel"]);
    $author = $news->setAuthor($db, $_POST["autor"]); //This is line 14

    News::newsArticleCreate($db, $title, $news, $author);

}

This is my setter-method for the author:

    function setAuthor($db, $author)
    {
        if(!empty($author))
        {
            $author = mysqli_real_escape_string($db, $author);
            return $this->author = trim($author);
        }
        else
        {
            return false;
        }
    }

What is my mistake?

tereško
  • 58,060
  • 25
  • 98
  • 150
netsam
  • 3
  • 1
  • 5

1 Answers1

1

Fatal error: Uncaught Error: Call to a member function setAuthor() on string

The error says that you are trying to call setAuthor on a string ( $news ) rather than on an object of News Class. If you replace the following line:

$news =  $news->setNews($db, $_POST["artikel"]);

with

$news->setNews($db, $_POST["artikel"]);

It should work fine.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Tarun
  • 3,162
  • 3
  • 29
  • 45