ThinkPHP5.1中增删改查操作

admin 2023年9月21日10:15:34评论27 views字数 3349阅读11分9秒阅读模式
当涉及到构建Web应用程序时,数据的增删改查是不可或缺的功能之一。ThinkPHP 5.1是一个流行的PHP框架,它提供了强大的工具和功能,使得增删改查操作变得更加容易。本博客将为您介绍如何在ThinkPHP 5.1中执行这些操作,并提供详细的代码示例。
增加数据(Create)
要在ThinkPHP 5.1中添加新数据,您需要使用模型和控制器来处理表单提交和数据库插入。以下是一个简单的示例,演示如何创建一个名为User的模型和控制器,并添加新的用户记录:
创建模型
首先,创建一个名为User的模型,用于与用户数据表进行交互
// application/common/model/User.phpnamespace appcommonmodel;use thinkModel;class User extends Model{    // 定义数据表名称    protected $table = 'user';    // 定义可写字段    protected $fillable = ['name', 'email', 'password'];}
创建控制器
接下来,创建一个控制器,用于处理表单提交并将数据插入数据库:
// application/index/controller/UserController.phpnamespace appindexcontroller;use appcommonmodelUser;use thinkController;use thinkRequest;class UserController extends Controller{    // 显示用户添加表单    public function create(){        return $this->fetch();    }    // 处理表单提交    public function save(Request $request){        $data = $request->post();        // 使用模型创建新用户        User::create($data);        // 重定向到用户列表页面
        $this->success('用户添加成功', 'index');
    }
}
创建用户表单视图
最后,创建一个用户添加表单的视图文件,用于输入用户信息:
<!-- application/index/view/user/create.html --><form method="post" action="<?php echo url('user/save'); ?>">    <label for="name">姓名:</label>    <input type="text" name="name" id="name" required><br>    <label for="email">邮箱:</label>    <input type="email" name="email" id="email" required><br>    <label for="password">密码:</label>    <input type="password" name="password" id="password" required><br>    <input type="submit" value="添加用户"></form>
现在,您可以访问/index/user/create以查看用户添加表单,并且提交后的数据将会被插入到数据库中。
删除数据(Delete)
删除数据通常需要通过一个请求来触发,比如点击删除按钮。以下是如何在ThinkPHP 5.1中删除用户数据的示例:
创建删除方法
在UserController控制器中创建一个方法来删除用户:
// application/index/controller/UserController.php// 删除用户public function delete($id){    // 使用模型删除用户    User::destroy($id);    // 重定向到用户列表页面    $this->success('用户删除成功', 'index');}
创建删除按钮
在用户列表中,为每个用户添加一个删除按钮:
<!-- application/index/view/user/index.html --><table>    <thead>        <tr>            <th>ID</th>            <th>姓名</th>            <th>邮箱</th>            <th>操作</th>        </tr>    </thead>    <tbody>        <?php foreach ($users as $user): ?>            <tr>                <td><?php echo $user['id']; ?></td>                <td><?php echo $user['name']; ?></td>                <td><?php echo $user['email']; ?></td>                <td>                    <a href="<?php echo url('user/delete', ['id' => $user['id']]); ?>" onclick="return confirm('确定要删除吗?')">删除</a>                </td>            </tr>        <?php endforeach; ?>    </tbody></table>
现在,您可以在用户列表中删除用户数据。
更新数据(Update)
要更新数据,您需要一个编辑页面和一个处理编辑表单的方法。以下是如何在ThinkPHP 5.1中更新用户数据的示例:
创建编辑方法
在UserController控制器中创建一个方法来显示用户编辑页面
// application/index/controller/UserController.php// 显示用户编辑页面public function edit($id){    $user = User::find($id);    return $this->fetch('edit', ['user' => $user]);}
创建编辑表单视图
创建一个编辑表单的视图文件,用于编辑用户信息:
<!-- application/index/view/user/edit.html --><form method="post" action="<?php echo url('user/update', ['id' => $user['id']]); ?>">    <label for="name">姓名:</label>    <input type="text" name="name" id="name" value="<?php echo $user['name']; ?>" required><br>    <label for="email">邮箱:</label>    <input type="email" name="email" id="email" value="<?php echo $user['email']; ?>" required><br>    <label for="password">密码:</label>    <input type="password" name="password" id="password"><br>    <input type="submit" value="更新用户"></form>
创建更新方法
在UserController控制器中创建一个方法来处理编辑表单的提交:
// application/index/controller/UserController.php// 处理编辑表单提交public function update(Request $request, $id){    $data = $request->post();    // 使用模型更新用户    User::update($data, ['id' => $id]);    // 重定向到用户列表页面    $this->success('用户更新成功', 'index');}
现在,您可以在用户列表中点击编辑按钮,进入编辑页面,然后更新用户数据。
查询数据(Read)
ThinkPHP 5.1使查询数据变得非常简单。您可以使用模型来执行各种查询操作。以下是如何查询用户数据的示例:
查询所有用户
$users = User::select();
根据条件查询用户
$users = User::where('age', '>', 18)->select


关 注 有 礼



欢迎关注公众号:网络安全者

       

ThinkPHP5.1中增删改查操作


原文始发于微信公众号(网络安全者):ThinkPHP5.1中增删改查操作

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年9月21日10:15:34
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   ThinkPHP5.1中增删改查操作https://cn-sec.com/archives/2054361.html

发表评论

匿名网友 填写信息