php+mysql realizes article management and display code sharing

小云云
Release: 2023-03-21 13:20:02
Original
3426 people have browsed it

This article mainly shares with you the php+mysql implementation of article management and display code, hoping to help everyone.

1. Create the necessary folders and files

2. Create new corresponding management and execution files in the admin folder


3.Configuration config.php content, mainly configures the constants related to the database connection

defined('DS') or define('DS',DIRECTORY_SEPARATOR);

defined('PATH') or define('PATH',dirname(__FILE__));

define('HOST','localhost');

define('USERNAME','root');

define('PASSWORD','root');
Copy after login


4.Configure the database connection fileconnect.php, mainly configure the initialization related to the relevant database connection

##

require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'config.php');

$connect  = mysqli_connect(HOST,USERNAME,PASSWORD);

mysqli_select_db($connect,'lgc');

mysqli_query($connect,'set names utf8');
Copy after login

5. Configuration article additionHTMLPage



6.

Configure article add management



##7.

Configure the article editing page,

HTML content is consistent with the article adding page, only action points to article.modify. handle.php

require_once('../config.php');

require_once('../connect.php'); 

$id = $_GET['id'];

        

$modifySqlstr = "select * from article where id = $id";

$arr = array();

if($con = mysqli_query($connect,$modifySqlstr))
{
         while($row = mysqli_fetch_assoc($con) )
         {
                   $arr2 = $row;
         }
}
Copy after login

The

HTML content is omitted (and the HTMl## of the article addition page #Stay relatively consistent)......

8.配置文章编辑页面执行文件

require_once('../config.php');

require_once('../connect.php');


//print_r($_POST);

$id = $_POST['id'];

$title = $_POST['title'];

$author = $_POST['author'];

$description = $_POST['description'];

$content = $_POST['content'];

$dateline = time();

 

$modifySql = "update article set title='$title', author = '$author', description = '$description', content = '$content' where id = $id";

$scriptSuccess = '

$scriptError = '

 

if(mysqli_query($connect,$modifySql)){
      echo $scriptSuccess;
}else{
      echo $scriptError;
}

 
mysqli_close($connect);
Copy after login

9. 配置文章删除执行文件

require_once('../config.php');
require_once('../connect.php');

$id = $_GET['id'];
$delStr = "delete from article where id = $id";
$scriptSuccess = '
$scriptError = '

        
if(mysqli_query($connect,$delStr))
{
    echo $scriptSuccess;
}else{
    echo $scriptError;
}
Copy after login

10.配置文章管理页面



执行php代码:

-- 查询获取所有文章信息:

require_once('../config.php');
require_once('../connect.php');

$con = mysqli_query($connect,'select * from article');
$arr = array();
 
while($row = mysqli_fetch_array($con,MYSQLI_ASSOC) )
{
     $arr[] = $row;
}

//print_r($arr);
Copy after login

-- 遍历生成文章信息列表:



11.配置文章列表(详情)展示页面:

require_once('./config.php');
require_once('./connect.php');

$id = $_SERVER['QUERY_STRING'];

if(empty($id)){
     $sql = 'select * from article order by dateline desc';
}else{       
      $id = $_GET['id'];
Copy after login
    $sql = "select * from article where id = $id "; //配置文章详情展示页面
Copy after login
}
Copy after login

$con = mysqli_query($connect,$sql);$arr = array();while($row = mysqli_fetch_array($con,MYSQLI_ASSOC)){ $arr[] = $row;}


//遍历生成文章首页文章列表



12. 总结:

mysqli_query(connection,query,resultmode);
Copy after login

在判断是否插入成功,或者查询成功的时候,不能直接把mysqli_query(参数)当成if语句的条件,在因为针对成功的 SELECTSHOWDESCRIBE  EXPLAIN 查询,将返回一个mysqli_result 对象,并不是返回一个布尔值;在针对其他的成功的查询,例如update等,则返回布尔值true,失败则返回false;

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(6) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ; 在返回对象的时候 $object = mysqli_query(参数); 然后可以用$object -> num_rows 的值来确定是否匹配查询到内容!

相关推荐:

PHP+MYSQL的文章管理系统(一)_PHP教程

PHP+MYSQL的文章管理系统(二)_PHP教程

php 文章管理_PHP教程

The above is the detailed content of php+mysql realizes article management and display code sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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