Goodnight. I continue to learn PHP (3rd year Specialist, where the news site). Faced with such an ambush.
There is a main file
<?php require_once ("NewsDB.class.php"); $news = new NewsDB(); $errMsg=""; if ($_SERVER['REQUEST_METHOD']=='POST') { require_once ("save_news.inc.php");} require_once ("delete_news.inc.php"); ?> <!DOCTYPE html> <html> <head> <title>Новостная лента</title> <meta charset="utf-8" /> </head> <body> <h1>Последние новости</h1> <?php If ($errMsg) echo "<h3>$errMsg</h3>"; ?> <form action="<?= $_SERVER['PHP_SELF']; ?>" method="post"> Заголовок новости:<br /> <input type="text" name="title" /><br /> Выберите категорию:<br /> <select name="category"> <option value="1">Политика</option> <option value="2">Культура</option> <option value="3">Спорт</option> </select> <br /> Текст новости:<br /> <textarea name="description" cols="50" rows="5"></textarea><br /> Источник:<br /> <input type="text" name="source" /><br /> <br /> <input type="submit" value="Добавить!" /> </form> <?php require_once 'get_news.inc.php'; ?> </body> </html> There require save_news.inc.php
<?php $title = $news->clearStr($_POST[title]); $category = $news->clearInt($_POST[category]); $description = $news->clearStr($_POST[description]); $source = $news->clearStr($_POST[source]); If (empty($title) or empty($description)){ $errMsg = "Заполните все поля формы"; }else{If ($news->saveNews($title, $category, $description, $source)){ header('Location:news.php');exit;} else { $errMsg = "Ошибка при добавлении новости"; }} and delete_news.inc.php
<?php var_dump($news); //require_once 'NewsDB.class.php'; //$news = new NewsDB(); $iddel = $news->clearInt($_GET[iddel]); if ($iddel>0){ {if($news->deleteNews($iddel)){ $errMsg="Запись удалена"; header("Location:news.php"); }else $errMsg="Ошибка при удалении"; header("Location:news.php"); }} So save_news works fine and saves the news. But delete_news does not want to work, writes Fatal error: Call to a member function clearInt() on a non-object If you require on the main page, the var_dump in the second line of the file in delete_news shows that it is an object. And when we follow the link from the getnews file, the object is destroyed. If you uncomment 3 and 4 lines in delete and recreate the $ news object again, it works fine. Tell me in what an ambush? Why the object is destroyed.
Just in case, get_news
<?php $posts= $news->getNews(); $i=1; foreach ($posts as $post) { echo "Новость $i"; $i++; $datetim = date("dmY", $post['datetime']); ?> <h3><?= $post[title]?></h3> <p><?= $post[description]?></p> <p><?= $post[source]?></p> <p><?= $datetim ?></p> <a href="delete_news.inc.php?iddel=<?=$post['id']?>">Удалить новость сверху</a></br> <?php };
<a href="delete_news.inc.php?iddel=<?=$post['id']?>">Удалить новость сверху</a></br>and what else do you want? Opens directlydelete_news.inc.phphabrahabr.ru/post/179399 - ReinRaus<form action="<?= $_SERVER['PHP_SELF']; ?>" method="post">is there a difference? - Simps Sams