当前位置:网站首页>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>
边栏推荐
- Jenkins+ant+jmeter use
- Jenkins task grouping
- Elaborate on MySQL mvcc multi version control
- 信息安全实验二 :使用X-SCANNER扫描工具
- 数据库多表关联查询问题
- flinkcdc 用sqlclient可以指定mysqlbinlog id执行任务吗
- 【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
- Zen - batch import test cases
- STM32 and motor development (from stand-alone version to Networking)
- 4、 Fundamentals of machine learning
猜你喜欢
Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
VSCode+mingw64
Strategic cooperation subquery becomes the secret weapon of Octopus web browser
其实特简单,教你轻松实现酷炫的数据可视化大屏
信息安全实验四:Ip包监视程序实现
Unity uses mesh to realize real-time point cloud (I)
Difference between interface iterator and iteratable
First issue of JS reverse tutorial
PLC信号处理系列之开关量信号防抖FB
Loxodonframework quick start
随机推荐
Install pyqt5 and Matplotlib module
ViewPager2和VIewPager的區別以及ViewPager2實現輪播圖
Regular matching starts with XXX and ends with XXX
asp. How to call vb DLL function in net project
(3/8) method parameters of improper use of enumeration (2)
(3/8)枚举的不当用法 之 方法参数(二)
Difference between process and thread
用flinksql的方式 写进 sr的表,发现需要删除的数据没有删除,参照文档https://do
Pick up the premise idea of programming
如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
flex弹性布局
Netease Cloud Wechat applet
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
PLC信号处理系列之开关量信号防抖FB
Diffusion模型详解
信息安全实验三 :PGP邮件加密软件的使用
JS judge whether checkbox is selected in the project
Niuke - Huawei question bank (61~70)
信息安全实验二 :使用X-SCANNER扫描工具
Difference between interface iterator and iteratable