视图层:
表单:
<?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']);
演示效果:
}