溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ThinkPHP框架如何實(shí)現(xiàn)用戶信息查詢更新及刪除功能

發(fā)布時(shí)間:2021-06-03 14:14:23 來源:億速云 閱讀:118 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)ThinkPHP框架如何實(shí)現(xiàn)用戶信息查詢更新及刪除功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

具體如下:

一 代碼

1、配置文件

<?php
return array(
  'APP_DEBUG' => false,    // 關(guān)閉調(diào)試模式
  'DB_TYPE'=> 'mysql',    // 數(shù)據(jù)庫類型
  'DB_HOST'=> 'localhost',   // 數(shù)據(jù)庫服務(wù)器地址
  'DB_NAME'=>'db_database30',     // 數(shù)據(jù)庫名稱
  'DB_USER'=>'root',      // 數(shù)據(jù)庫用戶名
  'DB_PWD'=>'root',        // 數(shù)據(jù)庫密碼
  'DB_PORT'=>'3306',      // 數(shù)據(jù)庫端口
  'DB_PREFIX'=>'think_',    // 數(shù)據(jù)表前綴
);
?>

2、入口文件

<?php
define('THINK_PATH', '../ThinkPHP');    //定義ThinkPHP框架路徑(相對(duì)于入口文件)
define('APP_NAME', 'App');       //定義項(xiàng)目名稱
define('APP_PATH', './App');        //定義項(xiàng)目路徑
require(THINK_PATH."/ThinkPHP.php");  //加載框架入口文件
App::run();               //實(shí)例化一個(gè)網(wǎng)站應(yīng)用實(shí)例
?>

3、控制器文件

<?php
header("Content-Type:text/html; charset=utf-8");  //設(shè)置頁面編碼格式
class IndexAction extends Action{
  public function index(){
    $db = M('User');              // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴
    $select = $db->order('id desc')->limit(10)->select();
    $this->assign('select',$select);       // 模板變量賦值
    $this->display();              // 指定模板頁
  }
  public function update(){
    $db = M('User');              // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴
    $select = $db->where('id='.$_GET['id'])->select();
    $this->assign('select',$select);       // 模板變量賦值
    $this->display(update);             // 指定模板頁
    if(isset($_POST['id'])){
      $data['user'] = $_POST['user'];       // 要修改的數(shù)據(jù)對(duì)象屬性賦值
      $data['pass'] = md5($_POST['pass']);
      $data['address'] = $_POST['address'];
      $result=$db->where('id='.$_POST['id'])->save($data);   // 根據(jù)條件保存修改的數(shù)據(jù)
      if($result){
        $this->redirect('Index/index','', 2,'數(shù)據(jù)更新成功');    //頁面重定向
      }
    }
  }
  public function delete(){
    $db = M('User');              // 實(shí)例化模型類,參數(shù)數(shù)據(jù)表名稱,不包含前綴
    $result=$db->where('id='.$_GET['id'])->delete();   // 刪除id為5的用戶數(shù)據(jù)
    if($result){
      $this->redirect('Index/index','', 2,'數(shù)據(jù)刪除成功');    //頁面重定向
    }
  }
}
?>

4、模板文件一

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用戶信息輸出</title>
<link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="4" bgcolor="#FFFFFF" class="title" align="center">用戶信息</td>
 </tr>
 <tr class="title">
  <td bgcolor="#FFFFFF" width="44">ID</td>
  <td bgcolor="#FFFFFF" width="120">名稱</td>
  <td bgcolor="#FFFFFF" width="111">地址</td>
  <td bgcolor="#FFFFFF" width="111">操作</td>
 </tr>
 <foreach name='select' item='user' >
 <tr class="content">
  <td bgcolor="#FFFFFF">{$user.id}</td>
  <td bgcolor="#FFFFFF">{$user.user}</td>
  <td bgcolor="#FFFFFF">{$user.address}</td>
  <td bgcolor="#FFFFFF"><a href="__URL__/update?id={$user.id}" rel="external nofollow" >更新</a>/<a href="__URL__/delete?id={$user.id}" rel="external nofollow" >刪除</a></td>
 </tr>
 </foreach>
</table>
</body>
</html>

5、模板文件二

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用戶信息輸出</title>
<link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form2" name="form2" method="post" action="__URL__/update">
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="2" bgcolor="#FFFFFF" class="title" align="center">用戶信息</td>
 </tr>
 <foreach name='select' item='user' >
 <tr class="content">
  <td bgcolor="#FFFFFF" class="right" width="103">名稱:</td>
  <td bgcolor="#FFFFFF" width="289"> <input type="hidden" name="id" id="hiddenField" value="{$user.id}" /><input name="user" type="text" id="user" size="20" value="{$user.user}" /></td>
  </tr>
 <tr class="content">
  <td bgcolor="#FFFFFF" class="right">密碼:</td>
  <td bgcolor="#FFFFFF"><input name="pass" type="password" id="pass" size="20" value="{$user.pass}" />
   </td>
  </tr>
 <tr class="content">
  <td bgcolor="#FFFFFF" class="right">&nbsp;地址:</td>
  <td bgcolor="#FFFFFF">&nbsp;
   <input name="address" type="text" id="address" size="30" value="{$user.address}" />
  </td>
  </tr>
 <tr class="content">
  <td bgcolor="#FFFFFF">&nbsp;</td>
  <td bgcolor="#FFFFFF"><input type="submit" name="button" id="button" value="更新" /></td>
 </tr>
 </foreach>
</table>
</form>
</body>
</html>

二 運(yùn)行結(jié)果

ThinkPHP框架如何實(shí)現(xiàn)用戶信息查詢更新及刪除功能

感謝各位的閱讀!關(guān)于“ThinkPHP框架如何實(shí)現(xiàn)用戶信息查詢更新及刪除功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI