Home > Backend Development > PHP Problem > How to delete articles in php

How to delete articles in php

藏色散人
Release: 2023-03-06 19:58:01
Original
1804 people have browsed it

How to delete articles in php: first turn on the cache through "ob_start();"; then introduce "require_coce("../conn.php");"; then execute the SQL delete statement; finally implement Just delete the important judgment statements of the static page.

How to delete articles in php

Recommended: "PHP Video Tutorial"

When PHP deletes an article, it also deletes the generated HTML page

Generate HTML static pages when adding articles, but if you want to delete articles, the HTML static pages generated when adding articles should also be deleted at the same time, otherwise they will become redundant files, so we should delete useless articles when , and delete the generated HTML static pages at the same time. Let’s take a look at how to delete the generated HTML static pages at the same time in the PHP article system. Here is just a simple principle system that can be used as a reference. For more mature systems, you can refer to the more mature ones. CMS system, the following is the source code file.

ob_start();
require_once("../inc/conn.php");
$id=$_GET["id"];
$path=$_GET["path"];
$sql="delete from newscontent where newsid=$id";
mysql_query($sql);
if(file_exists("../newslist/$path"))
{
unlink("../newslist/$path");
$foldername=substr($path,0,10);
$folder=fopen("../newslist/$foldername");
$n=0;
while($f=readdir($folder))
{
if($f<>"." && $f<>"..")
{
$n++;
}
}
closedir();
if($n==0)
{
rmdir("../newslist/$foldername");
}
}
header("location:del.php");
?>
Copy after login

These codes are relatively easy to understand. ob_start(); turns on caching, require_coce("../conn.php"); contains the database connection file, and the following variables $id and $path are accepted. page, these two values ​​​​are passed in the list page, and then the SQL delete statement is executed. First, delete the articles in the database. The following if statement is an important judgment statement for deleting static pages. If $path exists, use unlink to delete it. The while statement here is the directory to be read, and no in-depth understanding is required.

A complete system for generating HTML static pages from articles should be to generate static HTML files at the same time when adding articles, update the generated HTML static pages at the same time when updating articles, and delete the generated HTML at the same time when deleting. Static pages, regenerating static pages when updating will not be introduced here. It is the same as adding articles. It is to determine the $path of the article and then regenerate it accordingly. However, you must give the static file writing permission, otherwise it will not be updated and deleted. Static HTML articles must also be given sufficient permissions, otherwise errors will occur.

The above is the detailed content of How to delete articles in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template