<?php
//AR操作数据库
$user=new User;
/*$user->username=$username;
$user->username=$username;
$user->username=$username;
$user->username=$username;*/
$user->attributes=$_POST;
$user->save();
user::model()->findByPk($id)-delete();
user::model()->findAll();
User::model()->count();
user::model()->findByPk($id)
$user=user::model()->findByPk($id)
$user->username=...;
$uer->save();
//留言板
testController.php
<?php
/*$db=Yii::app()->db;
$db->createcommand($sql)->execute(); queryRow,queryAll*/
class TestController extends Controller {
public $a=2;
public function actionIndex($model='写的'){
$this->render('edit',array(
'model'=>$model,
));
}
public function actionRegok(){
//接收的时候必须判断一下;防止sql注入等 以及判断信息是否合理,此处忽略;
/* $username=$_POST['username'];
$passwd=$_POST['passwd'];
$email=$_POST['email'];
$age=$_POST['age'];
$gender=$_POST['gender'];*/
// $model=new User;
// $commandInsert =$model->regoc();
$user=new User;
/*$user->username=$username;
$user->passwd=$passwd;
$user->email=$email;
$user->gender=$gender;
$user->age=$age;*/
$user->attributes=$_POST;
$commandInsert = $user->save();
if($commandInsert){
// print_r($commandInsert);
echo '成功';
}else{
echo '失败';
}
}
public function actionList(){
$users=user::model()->findAll();
//print_r($users);
$a='10011';
$time=date('Y-m-d H :i :s');
$this->renderPartial('list',array('users'=>$users,'oo'=>$time,));
//$this->render('rtt',array('users'=>$users,'time'=>$time,));
$this->renderPartial('b',array('a'=>$a));//render多个视图
}
public function actionDeleted(){
/* $model=new User;
$users =$model->deletee();*/
//print_r($users);
$id=$_GET['id'];
$users=user::model()->findByPk($id)->delete();
if($users){
echo '删除成功';
}else{
echo '删除失败';
}
}
public function actionUpdate(){
/*$model=new User;
$users =$model->chashuju();
//print_r($users);*/
//第一步
$id=$_GET['id'];
$users=User::model()->findByPk($id);
$this->renderPartial('update',array('users'=>$users));
}
public function actionGenxin(){
/*$model=new User;
$users =$model->genxin();
//print_r($users);*/
$id=$_POST['id'];
/*$username=$_POST['username'];
$passwd=$_POST['passwd'];
$email=$_POST['email'];
$age=$_POST['age'];
$gender=$_POST['gender'];*/
$user=User::model()->findByPk($id);
/* $user->id=$id;
$user->username=$username;
$user->passwd=$passwd;
$user->email=$email;
$user->gender=$gender;
$user->age=$age;*/
$user->Attributes=$_POST;
$users=$user->save();
// $users = $user->updateByPk($id);
if($users){
echo '更新成功';
}else{
echo '更新失败';
}
}
public function actionGor(){
echo Yii::app()->baseUrl,'<br />';
echo Yii::app()->name,'<br />';
echo Yii::app()->basePath,'<br />';
echo Yii::app()->timeZone,'<br />';
}
//查共多少行
public function actionCountt(){
$n=User::model()->count();
echo $n;
}
public function actionRu(){
// CController::render('one');
$this->render('one');
}
public function actionRow(){
/* $model=new User;
$v =$model->cha();
$this->render('detail',array('v'=>$v));
*/
$id=$_GET['id'];
$v=user::model()->findByPk($id);
$this->render('detail',array('v'=>$v));
}
}
?>
视图 注册页面 edit.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
</script>
<style type="text/css">
h2{
color:red;
}
</style>
</head>
<body>
<pre>create table user (
id int primary key auto_increment,
username varchar(20) not null default '',
passwd char(32) not null default '',
email varchar(50) not null default '',
gender char(1) not null default '',
age tinyint unsigned not null default 0
)engine myisam charset utf8</pre>
<h1>用户注册页面</h1><h2><?php echo $model; ?></h2>
<form action="<?php echo Yii::app()->request->baseUrl;?>/index.php?r=test/regok" method="post">
用户名:<input type="text" name="username" /><br />
密码:<input type="text" name="passwd" /><br />
重复密码:<input type="text" name="repasswd" /><br />
email:<input type="text" name="email" /><br />
年龄:<input type="text" name="age" /><br />
性别:男<input type="radio" name="gender" value="男" />
女:<input type="radio" name="gender" value="女" />
妖:<input type="radio" name="gender" value="妖" />
<input type="submit" value="提交" />
<h1><a href="<?php echo Yii::app()->request->baseUrl; ?>/index.php?r=test/list">首页</a></h1>
</form>
</body>
</html>
视图 列表页list.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>tgg</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
</script>
<style type="text/css">
</style>
</head>
<body>
<?php echo $oo; ?>
<h2>用户列表</h2>
<table border="2">
<h1><a href="<?php echo Yii::app()->request->baseUrl; ?>/index.php?r=test/index">添加</a></h1>
<h1><a href="<?php echo Yii::app()->request->baseUrl; ?>/index.php?r=test/list">首页</a></h1>
<tr>
<td>用户名32</td>
<td>密码</td>
<td>邮件</td>
<td>性别</td>
<td>年龄</td>
</tr>
<?php foreach($users as $k=>$v): ?>
<tr>
<td><a href="<?php echo Yii::app()->request->baseUrl;?>/index.php?r=test/row/&id=<?php echo $v['id']; ?>"><?php echo $v['username']; ?></a></td>
<td><?php echo $v['passwd'] ;?></td>
<td><?php echo $v['email']; ?></td>
<td><?php echo $v['gender']; ?></td>
<td><?php echo $v['age']; ?></td>
<td><a href="<php echo Yii::app()->request->baseUrl;?>/index.php?r=test/deleted/&id=<?php echo $v['id']; ?>">删除</a></td>
<td><a href="<php echo Yii::app()->request->baseUrl;?>/index.php?r=test/update/&id=<?php echo $v['id']; ?>">更新</a></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
视图 详细内容页detail.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
</script>
<style type="text/css">
a {
color:red;
}
</style>
</head>
<body>
<h1><a href="<?php echo Yii::app()->request->baseUrl; ?>/index.php?r=test/list">首页</a></h1>
<h2><?php echo $v['username'] ?></h2>
<h3><?php echo $v['passwd'] ?></h3>
<p><?php echo $v['email'] ?></p>
<p><?php echo $v['gender'] ?></p>
<p><?php echo $v['age'] ?></p>
</body>
</html>
视图 修改页 update.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
</script>
<style type="text/css">
</style>
</head>
<body>
<h1><a href="<?php echo Yii::app()->request->baseUrl; ?>/index.php?r=test/list">首页</a></h1>
<form action="/blog/index.php?r=test/Genxin" method="POST">
<input type="hidden" name="id" value="<?php echo $users['id'];?>"/><br />
姓名<input type="text" name="username" value="<?php echo $users['username'];?>"/><br />
passwd <input type="text" name="passwd" value="<?php echo $users['passwd'];?>"/><br />
gender<input type="text" name="gender" value="<?php echo $users['gender'];?>"/><br />
email<input type="text" name="email" value="<?php echo $users['email'];?>"/><br />
age<input type="text" name="age" value="<?php echo $users['age'];?>"/><br />
更新提交<input type="submit" />
</form>
</body>
</html>
//视图文件都放views下面的test文件夹下面。
yii简易留言板 AR操作数据库
最新推荐文章于 2018-10-23 16:56:22 发布