当前位置:网站首页>Addition, deletion, modification and query of ThinkPHP database
Addition, deletion, modification and query of ThinkPHP database
2022-07-07 09:40:00 【qq_ forty-two million three hundred and seven thousand five hun】
chuangjian database thinkphp
The table is comm
Configuration database configuration file
Application/Common/config.php
<?php
return array(
//' Configuration item '=>' Configuration values '
'DB_TYPE' => 'mysql', // Database type
'DB_HOST' => 'localhost', // Server address
'DB_NAME' => 'thinkphp', // Database name
'DB_USER' => 'php', // user name
'DB_PWD' => '123456', // password
'DB_PORT' => '3306', // port
'DB_PREFIX' => '', // Database table prefix
'DB_PARAMS' => array(), // Database connection parameters
'DB_DEBUG' => TRUE, // Database debugging mode After opening, it can record SQL journal
'DB_FIELDS_CACHE' => true, // Enable field caching
'DB_CHARSET' => 'utf8', // The database encoding defaults to utf8
'DB_DEPLOY_TYPE' => 0, // Database deployment :0 Centralized ( Single server ),1 Distributed ( Master slave server )
'DB_RW_SEPARATE' => false, // Is the database read-write separated Master slave is effective
'DB_MASTER_NUM' => 1, // After the separation of reading and writing Number of primary servers
'DB_SLAVE_NO' => '', // Specify the slave server serial number
);
modify Application/Home/Controller/IndexController.class.php
<?php
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
//$this->display();
$Comm = M('comm'); //
// Instantiation comm object
$limit =C('PAGE_COUNT');// from config.php Take out PAGE_COUNT Value
$count = $Comm->count();// Query the total number of records that meet the requirements
$Page = new \Think\Page($count,25);// Instantiate the paging class The total number of incoming records and the number of records displayed per page (25)
$show = $Page->show();// Page display output
// Paging data query Be careful limit The parameters of the method use Page Attributes of a class
$list = $Comm->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);// Assignment data set
$this->assign('page',$show);// Assign value to page output
$this->display();
// Output template
}
public function del(){
$id = I("id");
$Comm = M('comm');
$result = $Comm->where("id=$id")->delete();
$result>0?$this->success(" Delete successful ",U("Home/Index/design"),2):$this->error(" Delete failed ");
}
public function test(){
$this->display("index");;
}
public function edit(){
$ip = get_client_ip();
if(!empty(I("sub"))){
$id = I("id");
$time = date("Y-m-d H:i:s",time());
$data = array(
'ip' => I('ip'),
'title' => I('title'),
'content' => I('content'),
'time' => $time,
);
$Comm = M('comm');
$result = $Comm->where("id=$id")->save($data);
$result>0?$this->success(" The update is successful ",U("Home/Index/design"),2):$this->error(" Update failed ");
}else{
$id = I("id");
// Find out the data
$Comm = M('comm');
$data = $Comm->where("id=$id")->find();// Query and get a piece of data
$this->assign('ip',$ip);
$this->assign('data',$data);
$this->display();
}
}
public function design(){
$Comm = M('comm'); //
// Instantiation comm object
$limit =C('PAGE_COUNT');// from config.php Take out PAGE_COUNT Value
$count = $Comm->count();// Query the total number of records that meet the requirements
$Page = new \Think\Page($count,25);// Instantiate the paging class The total number of incoming records and the number of records displayed per page (25)
$show = $Page->show();// Page display output
// Paging data query Be careful limit The parameters of the method use Page Attributes of a class
$list = $Comm->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);// Assignment data set
$this->assign('page',$show);// Assign value to page output
$this->display();
}
public function insert(){
$ip = get_client_ip();
if(!empty(I("sub"))){
// insert data
$title = I('title');
$content=I('content');
$time=date("Y-m-d H:i:s",time());
$data = array(
'ip' =>$ip,
'title' => $title,
'time' => $time,
'content' => $content
);
$Comm = M('comm');
$result = $Comm->add($data);
dump($result);
$result>0?$this->success(" Message success ",U("Home/Index/design"),2):$this->error(" Message failed ");
}else{
$this->assign(ip,$ip);
$this->display();
}
}
}
Modify the template index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title> Write the blog of Lightyear </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="yinqi" />
<link rel="shortcut icon" type="image/x-icon" href="__PUBLIC__/favicon.ico" />
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/materialdesignicons.min.css" />
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/style.min.css" />
</head>
<body>
<header class="lyear-header text-center" style="background-image:url(__PUBLIC__/images/left-bg.jpg);">
<div class="lyear-header-container">
<div class="lyear-mask"></div>
<h1 class="lyear-blogger pt-lg-4 mb-0"><a href="index.html"> Write the blog of Lightyear </a></h1>
<nav class="navbar navbar-expand-lg">
<a class="navbar-toggler" data-toggle="collapse" data-target="#navigation" aria-controls="navigation" aria-expanded="false" aria-label="Toggle navigation">
<div class="lyear-hamburger">
<div class="hamburger-inner"></div>
</div>
</a>
<div id="navigation" class="collapse navbar-collapse flex-column">
<div class="profile-section pt-3 pt-lg-0">
<img class="profile-image mb-3 rounded-circle mx-auto" src="images/lyear.png" width="120" height="120" alt=" Light year under the pen " >
<div class="lyear-sentence mb-3"> It must be remembered that our time for study is limited . Time is limited , Not only is life short , More because of the numerous personnel . We should try to spend all our time doing the most useful things .</div>
<hr>
</div>
<ul class="navbar-nav flex-column text-center">
<li class="nav-item active">
<a class="nav-link" href="index.html"> home page </a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.html"> technology </a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.html"> Sentiment </a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html"> About me </a>
</li>
</ul>
<div class="my-2 my-md-3">
<form class="lyear-search-form form-inline justify-content-center pt-3">
<input type="email" id="semail" name="semail1" class="form-control mr-md-1" placeholder=" Search keywords " />
</form>
</div>
</div>
</nav>
</div>
</header>
<div class="lyear-wrapper">
<section class="mt-5 pb-5">
<div class="container">
<div class="row">
<!-- The article lists -->
<div class="col-xl-8">
<foreach name="list" item="vo">
<article class="lyear-arc">
<div class="arc-header">
<h2 class="arc-title"><a href="post.html">{
$vo.content}</a></h2>
<ul class="arc-meta">
<li><i class="mdi mdi-calendar"></i> {
$vo.time}</li>
<li><i class="mdi mdi-tag-text-outline"></i> <a href="#">{
$vo.title}</a>, <a href="#"> results </a></li>
<li><i class="mdi mdi-comment-multiple-outline"></i> <a href="#">{
$vo.ip} ip</a></li>
</ul>
</div>
<div class="arc-synopsis">
<p>{
$vo.content}</p>
</div>
</article>
</foreach>
<!-- Content end -->
<!-- Sidebar -->
<div class="col-xl-4">
<div class="lyear-sidebar">
<!-- Hot articles -->
<aside class="widget widget-hot-posts">
<div class="widget-title"> Hot articles </div>
<ul>
<li>
<a href="#"> Samsung will be Galaxy Fold The user provides 149 Dollar replacement screen service </a> <span>2019-09-25 10:05</span>
</li>
<li>
<a href="#"> Experts :10 After year 6G Will come out The data transmission rate is expected to be faster than 5G fast 100 times </a> <span>2019-09-25 08:06</span>
</li>
<li>
<a href="#"> Apple officially released iPadOS 13.1 System , Add a number of powerful new features </a> <span>2019-09-25 09:35</span>
</li>
</ul>
</aside>
<!-- file -->
<aside class="widget">
<div class="widget-title"> file </div>
<ul>
<li><a href="#">2019 March </a> (40)</li>
<li><a href="#">2019 April </a> (08)</li>
<li><a href="#">2019 May </a> (11)</li>
<li><a href="#">2019 June </a> (21)</li>
</ul>
</aside>
<!-- label -->
<aside class="widget widget-tag-cloud">
<div class="widget-title"> label </div>
<div class="tag-cloud">
<a href="#" class="badge badge-light">php</a>
<a href="#" class="badge badge-primary"> Apple </a>
<a href="#" class="badge badge-danger"> The currency </a>
<a href="#" class="badge badge-light">linux</a>
<a href="#" class="badge badge-light"> front end </a>
<a href="#" class="badge badge-light">vue</a>
</div>
</aside>
</div>
</div>
<!-- Sidebar end -->
</div>
</div>
<!-- end container -->
</section>
</div>
__PUBLIC__
<script type="text/javascript" src="__PUBLIC__/js/jquery.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery.nicescroll.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/bootstrap.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/main.min.js"></script>
</body>
</html>
obtain ip Function writing
application/home/Common/functions.php
<?php
function get_client_ip($type = 0)
{
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP Address validation
$long = ip2long($ip);
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
Modify the template application/Home/view/insert.html
<include file="comm/head" />
<!--/sidebar-->
<div class="main-wrap">
<div class="crumb-wrap">
<div class="crumb-list"><i class="icon-font"></i><a href="{:U('Home/Index/index')}"> home page </a><span class="crumb-step">></span><a class="crumb-name" href="{:U('Home/Index/index')}"> Work management </a><span class="crumb-step">></span><span> New works </span></div>
</div>
<div class="result-wrap">
<div class="result-content">
<form action="{:U('Home/Index/insert')}" method="post" id="myform" name="myform" enctype="multipart/form-data">
<table class="insert-tab" width="100%">
<tbody>
<tr>
<th><i class="require-red">*</i> title :</th>
<td>
<input class="common-text required" id="title" name="title" size="50" value="" type="text">
</td>
</tr>
<th> Content :</th>
<td><textarea name="content" class="common-textarea" id="content" cols="30" style="width: 98%;" rows="10"></textarea></td>
</tr>
<tr>
<th></th>
<td>
<input class="btn btn-primary btn6 mr10" value=" Submit " type="submit" name="sub">
<input class="btn btn6" onClick="history.go(-1)" value=" return " type="button">
</td>
</tr>
</tbody></table>
</form>
</div>
</div>
</div>
<!--/main-->
</div>
</body>
</html>
{:U(‘Home/Index/insert’)} Here is submit to index Controller insert Method
\Application\Home\Controller\IndexController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
//$this->display();
$Comm = M('comm'); //
// Instantiation comm object
$limit =C('PAGE_COUNT');// from config.php Take out PAGE_COUNT Value
$count = $Comm->count();// Query the total number of records that meet the requirements
$Page = new \Think\Page($count,25);// Instantiate the paging class The total number of incoming records and the number of records displayed per page (25)
$show = $Page->show();// Page display output
// Paging data query Be careful limit The parameters of the method use Page Attributes of a class
$list = $Comm->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);// Assignment data set
$this->assign('page',$show);// Assign value to page output
$this->display();
// Output template
}
public function test(){
$this->display("index");;
}
public function design(){
$this->display();
}
public function insert(){
$ip = get_client_ip();
if(!empty(I("sub"))){
// insert data
$title = I('title');
$content=I('content');
$time=date("Y-m-d H:i:s",time());
$data = array(
'ip' =>$ip,
'title' => $title,
'time' => $time,
'content' => $content
);
$Comm = M('comm');
$result = $Comm->add($data);
dump($result);
$result>0?$this->success(" Message success ",U("Home/Index/index"),2):$this->error(" Message failed ");
}else{
$this->assign(ip,$ip);
$this->display();
}
}
}
\Application\Home\view\design.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title> Background management </title>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/common.css"/>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/css/main.css"/>
</head>
<body>
<div class="topbar-wrap white">
<div class="topbar-inner clearfix">
<div class="topbar-logo-wrap clearfix">
<h1 class="topbar-logo none"><a href="index.html" class="navbar-brand"> Background management </a></h1>
<ul class="navbar-list clearfix">
<li><a class="on" href="index.html"> home page </a></li>
<li><a href="#" target="_blank"> homepage </a></li>
</ul>
</div>
<div class="top-info-wrap">
<ul class="top-info-list clearfix">
<li><a href="#"> Administrators </a></li>
<li><a href="#"> Change Password </a></li>
<li><a href="#"> sign out </a></li>
</ul>
</div>
</div>
</div>
<div class="container clearfix">
<div class="sidebar-wrap">
<div class="sidebar-title">
<h1> menu </h1>
</div>
<div class="sidebar-content">
<ul class="sidebar-list">
<li>
<a href="#"><i class="icon-font"></i> Common operations </a>
<ul class="sub-menu">
<li><a href="design.html"><i class="icon-font"></i> Work management </a></li>
<li><a href="design.html"><i class="icon-font"></i> Blog management </a></li>
<li><a href="design.html"><i class="icon-font"></i> Classification management </a></li>
<li><a href="design.html"><i class="icon-font"></i> message management </a></li>
<li><a href="design.html"><i class="icon-font"></i> Comment management </a></li>
<li><a href="design.html"><i class="icon-font"></i> link </a></li>
<li><a href="design.html"><i class="icon-font"></i> Advertising management </a></li>
</ul>
</li>
<li>
<a href="#"><i class="icon-font"></i> System management </a>
<ul class="sub-menu">
<li><a href="system.html"><i class="icon-font"></i> System settings </a></li>
<li><a href="system.html"><i class="icon-font"></i> Clean cache </a></li>
<li><a href="system.html"><i class="icon-font"></i> The data backup </a></li>
<li><a href="system.html"><i class="icon-font"></i> Data restore </a></li>
</ul>
</li>
</ul>
</div>
</div>
<!--/sidebar-->
<div class="main-wrap">
<div class="crumb-wrap">
<div class="crumb-list"><i class="icon-font"></i><a href="index.html"> home page </a><span class="crumb-step">></span><span class="crumb-name"> Work management </span></div>
</div>
<div class="result-wrap">
<form name="myform" id="myform" method="post">
<div class="result-content">
<table class="result-tab" width="100%">
<tr>
<th>ID</th>
<th> title </th>
<th>ip</th>
<th> Update time </th>
<th> Content </th>
<th> operation </th>
</tr>
<foreach name="list" item="vo">
<tr>
<td>{
$vo.id}</td>
<td title="{$vo.title}"><a target="_blank" href="#" title="{$vo.title}">{
$vo.title}</a> …
</td>
<td>{
$vo.ip}</td>
<td>{
$vo.time}</td>
<td><p>{
$vo.content}</p></td>
<td>
<a class="link-update" href="#"> modify </a>
<a class="link-del" href="#"> Delete </a>
</td>
</tr>
</foreach>
</table>
<div class="list-page"> 2 strip 1/1 page </div>
</div>
</form>
</div>
</div>
<!--/main-->
</div>
</body>
</html>
Modify the template application/Home/view/edit.html
<include file="comm/head" />
<!--/sidebar-->
<div class="main-wrap">
<div class="crumb-wrap">
<div class="crumb-list"><i class="icon-font"></i><a href="{:U('Home/Index/index')}"> home page </a><span class="crumb-step">></span><a class="crumb-name" href="{:U('Home/Index/index')}"> Work management </a><span class="crumb-step">></span><span> New works </span></div>
</div>
<div class="result-wrap">
<div class="result-content">
<form action="{:U('Home/Index/edit')}" method="post" id="myform" name="myform" enctype="multipart/form-data">
<table class="insert-tab" width="100%">
<tbody>
<tr>
<td>
<input type="hidden" name="id" value="{
$data.id}" readonly="readonly">
<input type="hidden" name="ip" value="{
$data.ip} " readonly="readonly">
</td>
</tr>
<tr>
<th><i class="require-red">*</i> title :</th>
<td>
<input class="common-text required" id="title" name="title" size="50" value="{
$data.title}" type="text">
</td>
</tr>
<th> Content :</th>
<td><textarea name="content" class="common-textarea" id="content" cols="30" style="width: 98%;" rows="10"> {
$data.content}</textarea></td>
</tr>
<tr>
<th></th>
<td>
<input class="btn btn-primary btn6 mr10" value=" Submit " type="submit" name="sub">
<input class="btn btn6" onClick="history.go(-1)" value=" return " type="button">
</td>
</tr>
</tbody></table>
</form>
</div>
</div>
</div>
<!--/main-->
</div>
</body>
</html>
边栏推荐
- [4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力
- sql 里面使用中文字符判断有问题,哪位遇到过?比如value&lt;&gt;`无`
- Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
- 答案在哪里?action config/Interceptor/class/servlet
- First issue of JS reverse tutorial
- 【云原生】DevOps(一):DevOps介绍及Code工具使用
- 网易云微信小程序
- NATAPP内网穿透
- 细说Mysql MVCC多版本控制
- 浏览器中如何让视频倍速播放
猜你喜欢
![[SVN] what is SVN? How do you use it?](/img/45/a7df8989f18f0a6185582389398d1a.png)
[SVN] what is SVN? How do you use it?

信息安全实验四:Ip包监视程序实现

Unittest simple project

Huawei hcip datacom core_ 03day

Vs2013 generate solutions super slow solutions

Esp8266 uses TF card and reads and writes data (based on Arduino)

印象笔记终于支持默认markdown预览模式

答案在哪里?action config/Interceptor/class/servlet

Using JWT to realize login function

flex弹性布局
随机推荐
Communication mode between processes
JS reverse tutorial second issue - Ape anthropology first question
iNFTnews | 时尚品牌将以什么方式进入元宇宙?
thinkphp数据库的增删改查
網易雲微信小程序
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
Jenkins task grouping
章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕
The use of recycling ideas
在EXCEL写VBA连接ORACLE并查询数据库中的内容
VSCode+mingw64
The difference between viewpager2 and viewpager and the implementation of viewpager2 in the rotation chart
信息安全实验四:Ip包监视程序实现
进程间的通信方式
IIS faked death this morning, various troubleshooting, has been solved
Pick up the premise idea of programming
Strategic cooperation subquery becomes the secret weapon of Octopus web browser
如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
First issue of JS reverse tutorial
How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data