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 }; 
  • Here is a link to all files dl.dropboxusercontent.com/u/19358018/… - Simps Sams
  • <a href="delete_news.inc.php?iddel=<?=$post['id']?>">Удалить новость сверху</a></br> and what else do you want? Opens directly delete_news.inc.php habrahabr.ru/post/179399 - ReinRaus
  • First commented $ news, and now we complain that you cannot call methods on it, because it is not an object? - Sergey
  • I commented, because it seems to me that it should work and so. - Simps Sams
  • ReinRaus - yes it opens, but after all, when adding a product, it also goes to a new page, only there a page opens through POST <form action="<?= $_SERVER['PHP_SELF']; ?>" method="post"> is there a difference? - Simps Sams

1 answer 1

You misunderstand the mechanism of your page, hence, I think the problems.

Go through the points:

  1. Each page load in the browser, each page refresh will load your script. That is, php will process your code every time. (about caching while you can not understand).
  2. The first download is exactly the file that is listed in the address bar of the browser. It can be main.php, news.php, index.php or any other, but it will be the first one to be loaded.
  3. Further, as the main script is executed, other scripts are connected if the instructions for their inclusion are specified - include or require

Let's take a look at your delete_news.inc.php file:

You check the $ news variable and there will be an object in it, but only if it has been set somewhere before. That is why when loading the main file, the variable will contain a link to the object. But if you download the delete_news.inc.php file directly, the variable will be empty and you will get an error. Therefore, if you want to use this file separately, you need to uncomment the NewsBD connection and the installation of the $ news variable.

Let's go back to your main file, to the code:

 if ($_SERVER['REQUEST_METHOD']=='POST') { require_once ("save_news.inc.php");} require_once ("delete_news.inc.php"); ?> 

You lack the closing} after connecting delete_news.inc.php. And the connection of this file only when posting a request looks meaningless, because deleting the news takes place according to the GET parameter and does not depend on POST.

In general, the code you cited clearly shows a huge problem arising from a similar approach to website development. By connecting the necessary files manually, you will sooner or later come to the conclusion that for any changes you will have to re-shuffle a lot of files, and this will be a hell of a job.

  • I already knew about the overloading of scripts with each page. And thank you very much for clarifying this. - Simps Sams
  • I commented out, as I tried to get it to work without it. In general, I experimented. Previously, there was no connection class at all. Now it is clear. - Simps Sams
  • There is a closing} after connecting the first file, the second is connected without a condition, and as far as I can see it is not needed at all in the main one. Regarding the connection of files, I understand it is better to use automatic downloading. I will try to implement it here. This is all purely an educational project. - Simps Sams
  • Option close to the ideal - a single entry point + autoload - Victor
  • IMHO, sincerely recommend to take some microfreemvork or of those that are simpler and make some site on it. Self-writing is certainly informative, but this is a walk in the throes and rakes. - Victor