
选择一张表:
首先要创建模型:
1
|
$n = M( "account" );
|
数据库添加数据:
1、使用数组:
1
2
3
|
1.使用数组 $arr = array ( "uid" => "zhangsan" , "name" => "张三" , "pwd" => "123" );
$n ->add( $arr ); //将数组中的数据添加进数据库。
|
数据添加成功。
2、AR方式
1
2
3
4
|
$n ->uid= "wangwu" ;
$n ->name= "王五" ;
$n ->pwd= "123" ;
$n ->add();
|
3、自动收集表单
此种方法需要有一个页面提交表单提交数据:
在同一个命名空间下,打开view文件夹,新建与控制前同名的文件夹,之中在新建同一个名字的html文件。
html文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!DOCTYPE html> <html> <head>
<meta charset= "UTF-8" >
<title></title>
</head>
<body>
<form action= "__ACTION__" method= "post" >
<div>账号:<input type= "text" name= "uid" id= "uid" value= "" /></div>
<div>姓名:<input type= "text" name= "name" id= "name" value= "" /></div>
<div>密码:<input type= "password" name= "pwd" id= "pwd" value= "" /></div>
<input type= "submit" value= "添加" />
</form>
</body>
</html> |
模型代码:
1
2
3
4
5
6
7
8
9
10
11
|
if ( empty ( $_POST ))
{
$this ->show();
}
else
{
$n = M( "account" );
$n ->create();
//$n->name = ""; //如果不想用提交过来的数据,可以在这里修改
$n ->add();
}
|
数据库修改数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public function xiugai()
{
$uid = "zhangsan" ;
$n = M( "account" );
if ( empty ( $_POST ))
{
$arr = $n ->find( $uid );
$this ->assign( "shuju" , $arr );
$this ->show();
}
else
{
//1.数组方式
//$n->save($_POST); //提交过来的书记本身就是一个数组
//2.AR方式
//$n->uid=$_POST["uid"];
//$n->name=$_POST["name"];
//$n->pwd=$_POST["pwd"];
//$n->save();
//3.自动收集表单
$n ->create();
$n ->save();
}
}
|
模型代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!DOCTYPE html> <html> <head>
<meta charset= "UTF-8" >
<title></title>
</head>
<body>
<form action= "__ACTION__" method= "post" >
<div>账号:<input type= "text" name= "uid" id= "uid" value= "{$shuju.uid}" /></div>
<div>姓名:<input type= "text" name= "name" id= "name" value= "{$shuju.name}" /></div>
<div>密码:<input type= "password" name= "pwd" id= "pwd" value= "{$shuju.pwd}" /></div>
<input type= "submit" value= "添加" />
</form>
</body>
</html> |
修改某一项(主键尽量不要修改):
数据库数据删除操作:
1
2
3
4
5
|
public function shanChu()
{
$n = M( "account" );
$n -> delete ( "zhangsan,wangwu" );
}
|
运行之后:
删除成功