Yii框架中增删改查
来源:互联网 发布:淘宝950轮播代码 编辑:程序博客网 时间:2024/06/07 21:54
视图层:
表单:
<?php
//辅助函数
use yii\helpers\Url;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<table>
<form action="<?php echo Url::toRoute(['ha/add'])?>" method="post">
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>简介</td>
<td><textarea name="text" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td></td>
</tr>
</table>
</form>
</center>
</body>
</html>
展示:
<?php
use yii \helpers\Url;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>content</td>
<td>操作</td>
</tr>
<?php foreach ($data as $key => $val) { ?>
<tr>
<td><?php echo $val['id']?></td>
<td><?php echo $val['name']?></td>
<td><?php echo $val['content']?></td>
<td><a href="<?php echo Url::toRoute(['ha/del','id'=>$val['id']])?>">删除</a>||
<a href="<?php echo Url::toRoute(['ha/find','id'=>$val['id']])?>">修改</a></td>
</tr>
<?php } ?>
</table>
</center>
</body>
</html>
修改:
<?php
//辅助函数
use yii\helpers\Url;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<table>
<form action="<?php echo Url::toRoute(['ha/upload'])?>" method="post">
<input type="hidden" name='id' value="<?php echo $res['id']?>">
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="<?php echo $res['name']?>"></td>
</tr>
<tr>
<td>介绍</td>
<td><textarea name="content" cols="30" rows="10" value="<?php echo $res['content']?>"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td></td>
</tr>
</table>
</form>
</center>
</body>
</html>
控制器层:
添加数据:
public function actionAdd(){
$name= Yii::$app->request->post("name");
$content =Yii::$app->request->post("content");
$res=Yii::$app->db->createCommand("insert into yi(name,content) value('$name','$content')")->execute();
return $this->redirect(['ha/show']);
}
展示数据:
public function actionShow(){
header("content-type:text/html;charset=utf-8");
$data= Yii::$app->db->createCommand("select * from yi")->queryAll();
return $this->render('show',['data'=>$data]);
}
删除数据:
public function actionDel(){
$id= Yii::$app->request->get('id');
$del= Yii::$app->db->createCommand("delete from yi where id = '$id'")->execute();
return $this->redirect(['ha/show']);
}
修改数据:
public function actionFind(){
header("content-type:text/html;charset=utf-8");
$id= Yii::$app->request->get('id');
$data = yii::$app->db->createCommand("select * from yi where id ='$id'")->queryAll();
$res=$data[0];
return $this->render('upl',['res'=>$res]);
}
public function actionUpload(){
$id = Yii::$app->request->post('id');
$name = Yii::$app->request->post('name');
$content = Yii::$app->request->post('content');
Yii::$app->db->createCommand("update yi set name='$name',content='$content' where id = '$id'")->execute();
return $this->redirect(['ha/show']);
演示效果:
}
- Yii框架中增删改查
- Yii框架增删改查
- yii 框架增删改查
- Yii 框架增删改查
- YII框架数据库的增删改查
- Yii框架的增删改查
- YII框架数据库的增删改查
- YII框架数据库的增删改查
- YII框架的增删改查
- Yii框架下的增删改查
- yii框架的增删改查
- yii框架 AR增删改查 分页
- Yii框架 AR 增删改查
- yii框架的增删改查语句
- yii 增删改查
- yii增删改查
- yii框架基本增删改查分页操作
- 关于Yii框架基础的增删查改
- 第一章 对象导论1
- Linux】CentOS安装solr 4.10.3
- 关于HTTP_RAW_POST_DATA
- OpenCV学习之路(二)-图片特征提取、图片翻转
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- Yii框架中增删改查
- Retrofit框架读书总结笔记(一)
- Java虚拟机学习(一)-运行时数据区
- 移植zlog到arm linux
- 卷积神经网络(三)
- 获取文件的byte数组与根据byte数组生成文件的工具类示例
- 机器学习工程师指南
- node学习笔记(二)(ajax方式向node后台提交数据)
- 文章标题