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');
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');
5. Configuration article additionHTMLPage
Configure article add management
##7.
HTML content is consistent with the article adding page, only action points to article.modify. handle.phprequire_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;
}
}
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);
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; }
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);
-- 遍历生成文章信息列表:
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'];
$sql = "select * from article where id = $id "; //配置文章详情展示页面
}
$con = mysqli_query($connect,$sql);$arr = array();while($row = mysqli_fetch_array($con,MYSQLI_ASSOC)){ $arr[] = $row;}
//遍历生成文章首页文章列表
12. 总结:
mysqli_query(connection,query,resultmode);
在判断是否插入成功,或者查询成功的时候,不能直接把mysqli_query(参数)当成if语句的条件,在因为针对成功的 SELECT、SHOW、DESCRIBE 或 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 的值来确定是否匹配查询到内容!
相关推荐:
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!