url访问地址:localhost/bb/index.php?r=Appear/add
models
他们的名称都与表名相同
appear.php文件里的内容 如下
class appear extends CActiveRecord{
public function rules(){//规则
return array(//返回数组
array('username,title,des,dataline','required')//表的字段 'required'为字段后必须添加的
);
}
}
Reply.php文件里的内容 如下
class Reply extends CActiveRecord{
public function rules(){
return array(
array('tid,username,des,dataline','required'),//表的字段 'required'为字段后必须添加的
);
}
}
controllers文件夹下的文件
文件名为AppearController.php //显示添加||修改页面
public function actionadd(){
$appear = new appear();//实例化表
if(isset($_GET['id'])&&!empty($_GET['id'])){//判断时候是添加或修改
$id = $_GET['id'];//如果存在则是修改然后查处相关的数据,相反则是添加
$list = $appear->findAllByAttributes(array('id'=>$id));//根据id查找某条数据
$this->render('add',array('list'=>$list));//加载add页面 第二个参数是往前台传值
}else{
$this->render('add');//加载add页面
}
}
保存数据
//保存添加||修改数据
public function actionsave(){
$Appear = new Appear();
$id = $_POST['id'];
$data['title'] = $_POST['title'];
$data['des'] = $_POST['des'];
$data['username'] = $_POST['username'];
$data['dataline'] = time();
if(empty($id)){
$Appear->attributes=$data;//给属性赋值
if($Appear->insert()){//保存
echo 1;
}else{
echo 0;
}
}else{
$upd = $Appear->updateAll($data,"id=$id");//修改数据
if($upd){
echo 1;
}else{
echo 0;
}
}
}
views文件夹下的文件
如果aacontroller下的方法引用页面,那么views下必须要有aa文件夹
文件名为add.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>留言板添加页</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<form method="post" action="index.php?r=Appear/save1" >
<table>
<tr>
<td>标题:</td>
<td><input type="text" class="title" name="title" value="<?php echo $list[0]['title']?>"></td>
</tr>
<tr>
<td>内容:</td>
<td><textarea name="des" class="des"><?php echo $list[0]['username']?></textarea></td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" class="username" value="<?php echo $list[0]['username']?>"></td>
</tr>
<tr>
<input type="hidden" value="<?php echo $list[0]['id']?>" class="ids" />
<td><input type="button" id="tijiao" name="dosubmit" value="保存"></td>
<td></td>
</tr>
</table>
</form>
</body>
<script src="js/jquery.js"></script>
<script>
$(document).ready(function(){
$("#tijiao").click(function (){
var title = $(".title").val();
var des = $(".des").val();
var username = $(".username").val();
var id = $(".ids").val();
$.ajax({
type:'post',
data:'title='+title+'&des='+des+'&username='+username+'&id='+id,
url:'index.php?r=Appear/save',
success:function(data){
if(data == 1){
window.location.href='http://localhost/qjk_yii/index.php?r=Appear/list';
}else{
alert('2');
}
}
});
$(".title").val('');
$(".des").val('');
$(".username").val('');
})
});
</script>
</html>