Yii2.0 DAO-CURD简单操作

Yii2.0 DAO-CURD简单操作

创建控制器ExamController.php

<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
/**
 * Site controller
 */
class ExamController extends Controller
{
	public $enableCsrfValidation=false;
	public function actionIndex(){
		return $this->renderpartial('index');
	}
	public function actionAdd_do(){
		$data=Yii::$app->request->post();
		$username=$data['username'];
		$password=$data['password'];
		$res=Yii::$app->db->createCommand("insert into user value (null,'$username','$password')")->execute();
		return $this->redirect('?r=exam/show');
	}
	public function actionShow(){
		$data=Yii::$app->db->createCommand('select * from user')->queryAll();
		return $this->renderpartial('show',array('list'=>$data));
	}
	public function actionDel(){
		$id=Yii::$app->request->get('id');
		$res=Yii::$app->db->createCommand('delete from user where id='.$id)->execute();
		if($res){
			echo '<script>alert("删除成功");location.href="'.'?r=exam/show'.'";</script>';
		}
	}
	public function actionUpd(){
		$id=Yii::$app->request->get('id');
		$row=Yii::$app->db->createCommand('select * from user where id='.$id)->queryOne();
		return $this->renderpartial('upd',array('row'=>$row));
	}
	public function actionUpd_do(){
		$data=Yii::$app->request->post();
		$id=$data['id'];
		$username=$data['username'];
		$password=$data['password'];
		$res=Yii::$app->db->createCommand("update user set username='$username',password='$password' where id='$id'")->execute();
		if($res){
			echo '<script>alert("修改成功");location.href="'.'?r=exam/show'.'";</script>';
		}
	}
}

创建视图/views/exam/index.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<center>
	<form action="?r=exam/add_do" method="post">
		<table border="1">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="username" id=""></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="text" name="password" id=""></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="提交"></td>
			</tr>
		</table>
	</form>
</center>
</body>
</html>

创建视图/views/exam/show.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<center>
	<table border="1"> 
		<tr>
			<td>ID</td>
			<td>用户名</td>
			<td>密码</td>
			<td>操作</td>
		</tr>
		<?php foreach($list as $v){ ?>
		<tr>
			<td><?php echo $v['id'] ?></td>
			<td><?php echo $v['username'] ?></td>
			<td><?php echo $v['password'] ?></td>
			<td>
				<a href="?r=exam/del&id=<?=$v['id']?>">删除</a>
				<a href="?r=exam/upd&id=<?=$v['id']?>">修改</a>
			</td>
		</tr>
		<?php } ?>
	</table>
</center>
</body>
</html>

创建视图/views/exam/upd.php

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<center>
	<form action="?r=exam/upd_do" method="post">
		<table border="1">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="username" id="" value="<?=$row['username']?>"></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="text" name="password" id="" value="<?=$row['password']?>"></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="保存"></td>
			</tr>
			<input type="hidden" name="id" value="<?=$row['id']?>">
		</table>
	</form>
</center>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值