thinkphp利用模型通用数据编辑添加和删除的实例代码
作者:jingxian 时间:2024-05-03 15:13:14
数据添加函数实例
//数据添加
public function newData($strName="")
{
if (IS_POST) {
//如果用户提交数据
$model = D("$strName");
if (!$model->create()){
// 如果创建失败 表示验证没有通过 输出错误提示信息
$info = array(
"info"=>"{$model->getError()}",
"status"=>"n",
);
$this->ajaxReturn($info,'json');
exit();
} else {
if ($model->add()){
$info = array(
"info"=>"恭喜操作成功!",
"status"=>"y",
);
$this->ajaxReturn($info,'json');
} else {
$info = array(
"info"=>"操作失败!",
"status"=>"n",
);
$this->ajaxReturn($info,'json');
}
}
}
}
数据更新函数实例
//数据更新
public function update($strName="")
{
if (IS_POST) {
//如果用户提交数据
$model = D("$strName");
if (!$model->create()){
// 如果创建失败 表示验证没有通过 输出错误提示信息
$info = array(
"info"=>"{$model->getError()}",
"status"=>"n",
);
$this->ajaxReturn($info,'json');
exit();
} else {
if ($model->save()){
$info = array(
"info"=>"恭喜操作成功!",
"status"=>"y",
);
$this->ajaxReturn($info,'json');
} else {
$info = array(
"info"=>"操作失败!",
"status"=>"n",
);
$this->ajaxReturn($info,'json');
}
}
}
}
数据删除函数实例
//数据删除
public function delete($strNmae = '')
{
if(IS_POST)
{
$id = I('post.id');
if(!empty($id))
{
//用户提交数据删除当前id
$model = D("$strName");
$result = $model->delete("$id");
if($result !== false){
$info = array(
"info"=>"操作成功!",
"status"=>"y",
);
$this->ajaxReturn($info,'json');
}else{
$info = array(
"info"=>"操作失败!",
"status"=>"n",
);
$this->ajaxReturn($info,'json');
}
}
}
}
标签:thinkphp,模型
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mysql锁内部实现机制之C源码解析
2024-01-12 21:13:25
![](https://img.aspxhome.com/file/2023/4/70454_0s.png)
华为2019校招笔试题之处理字符串(python版)
2023-09-29 19:42:04
Python3爬虫带上cookie的实例代码
2021-11-10 18:06:58
python读取csv和txt数据转换成向量的实例
2021-09-12 12:01:24
费明红:什么样的404页面才是正确的?
2009-05-21 17:47:00
![](https://img.aspxhome.com/file/UploadPic/20095/21/1-50s.jpg)
django admin 根据choice字段选择的不同来显示不同的页面方式
2022-04-26 06:39:10
![](https://img.aspxhome.com/file/2023/3/96913_0s.jpg)
vue如何实现动态加载脚本
2024-05-25 15:19:28
在JavaScript中使用inline函数的问题
2024-04-29 13:42:44
![](https://img.aspxhome.com/file/2023/6/132586_0s.gif)
ASP程序与SQL存储过程结合使用详解
2011-03-25 10:50:00
JS实现局部选择打印和局部不选择打印
2024-05-11 09:05:49
Python混合使用同步和异步函数的方法
2021-07-19 05:01:05
![](https://img.aspxhome.com/file/2023/6/114766_0s.gif)
Python csv文件的读写操作实例详解
2021-06-07 19:02:41
![](https://img.aspxhome.com/file/2023/2/101152_0s.jpg)
浅析Git 分支的新建与合并
2022-06-07 05:10:54
![](https://img.aspxhome.com/file/2023/1/127211_0s.png)
python:socket传输大文件示例
2022-04-18 19:44:26
Python逐行读取文件内容的方法总结
2022-05-22 18:01:24
联邦学习论文解读分散数据的深层网络通信
2023-09-06 00:07:37
![](https://img.aspxhome.com/file/2023/9/109549_0s.png)
mysql去重查询的三种方法小结
2024-01-21 14:52:22
![](https://img.aspxhome.com/file/2023/1/112631_0s.jpg)
用python实现批量重命名文件的代码
2023-01-02 09:09:20
jquery ajax传递中文参数乱码问题及解决方法说明
2024-04-22 22:21:54
PyTorch的SoftMax交叉熵损失和梯度用法
2023-06-17 12:46:49