创建项目
1在入口文件index.php中写入以下代码
define ('APP_PATH','./Application/');
define ('APP_DEBUG',true);
require './ThinkPHP/ThinkPHP.php';
注意: require一定要在define的下面,不然debug调试始终失败,我就在上面耗了很长时间,悲催
2运行,在地址栏中输入,
<a target=_blank href="http://localhost:8080/index.php">http://localhost:8080/index.php</a>
3 修改 Common--Conf目录下的config.php文件
return array(
//'配置项'=>'配置值'
/* 'DB_TYPE'=>'mysql',
'DB_HOST'=>'localhost',
'DB_NAME'=>'test',
'DB_USER'=>'root',
'DB_PWD'=>'root',
'DB_PORT'=>'3306',
*/
'DB_TYPE'=>'mysql', //设置数据库类型
'DB_HOST'=>'localhost',//设置主机
'DB_NAME'=>'weiphp',//设置数据库名
'DB_USER'=>'root', //设置用户名
'DB_PWD'=>'root', //设置密码
'DB_PORT'=>'3306', //设置端口号
'DB_PREFIX'=>'wp_', //设置表前缀
'SHOW_PAGE_TRACE'=>true,
4创建UserControl控制器,在Home---Controler文件夹下
写入一下代码
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
}
5添加CURD方法
<?php
// 本类由系统自动生成,仅供测试用途
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
public function showUser()
{
$Member = D("Member");
//if(isset($_GET['uid']))
// $data=$Member->find($_GET['uid']);
$data=$Member->select();
//echo $Member->getLastSql();
if(false === $data)
{
echo $Member->getDbError();
}
//var_dump($arr);
$this->assign('data',$data);
$this->display();
}
public function AddUser()
{
$member =M("Member");
$member->uid=$_POST['id'];
$member->nickname=$_POST['username'];
$num=$member->add();
if($num>0)
$this->success("数据库添加成功",'/index.php/Home/User/showUser');
$this->display();
}
public function Modify()
{
$uid=$_GET['uid'];
$member =M("Member");
$data=$member->find($uid);
$this->assign('data',$data);
$this->display();
}
public function UpdateUser()
{
$member =M("Member");
$member->uid=$_POST['id'];
$member->nickname=$_POST['username'];
$num=$member->save();
echo $member->getLastSql();
if($num>0)
$this->success("数据库修改成功","/index.php/Home/User/showUser");
}
public function DeleteUser()
{
$member =M("Member");
$id=$_GET['uid'];
$num=$member->where("uid='{$id}'")->delete();
echo $member->getLastSql();
if($num>0)
$this->success("数据删除成","/index.php/Home/User/showUser");
}
}
6在view文件夹下创建视图
在View文件夹下创建user文件夹,在user文件下创建方法名对应的视图
7编辑各个视图
showUser.html代码
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<table border="1">
<tr>
<td>uid</td>
<td>name</td>
</tr>
<volist name="data" id="vo">
<tr>
<td>{$vo['uid']}</td>
<td>{$vo['nickname']}</td>
<td><a href="/index.php/Home/User/Modify/uid/{$vo['uid']}">update</a>
<td><a href="/index.php/Home/User/DeleteUser/uid/{$vo['uid']}">delete</a></td>
</tr>
</volist>
<tr>
<td><a href="/index.php/Home/User/AddUser">add</a>
</tr>
</table>
</body>
</html>
AddUser.html
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<form name="form1" action="AddUser" method="post">
<table>
<tr>
<td>id</td>
<td ><input type="text" name="id"/></td>
<td>name</td>
<td> <input type="text" name="username" /></td>
<td><input type="submit" value="submit"/>
</tr>
</table>
</form>
</body>
Modify.html
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<form name="form1" action="/index.php/Home/User/UpdateUser" method="post">
<table>
<tr>
<td>id</td>
<td ><input type="text" name="id" value="{$data['uid']}"/></td>
<td>name</td>
<td> <input type="text" name="username" value="{$data['nickname']}"/></td>
<td><input type="submit" value="submit" />
</tr>
</table>
</form>
</body>