就是无法插入记录,但是查询,更新,删除都正常。模版:
<html>
<head>
<title></title>
<meta charset="UTF-8">
<link rel="shorcut icon" href="favicon.ico" type="image/x-icon">
</head><body>
<h1>日程表</h1>
<a href="__URL__/add/">添加新日程</a>
<table border="1" id="list">
<tr>
<td>ID</td>
<td>日期</td>
<td>时间</td>
<td>具体事项</td>
<td>是否完成</td>
<td>操作</td>
</tr>
<volist name='list' id='vo'>
<tr>
<td>{$vo.id}</td>
<td>{$vo.year}-{$vo.month}-{$vo.day}</td>
<td>{$vo.hour}:{$vo.minute}</td>
<td>{$vo.description}</td>
<td>
<if condition="$vo.status eq Y">已完成
<else />未完成
</if>
</td>
<td>
<if condition="$vo.status neq Y">
<a href="__URL__/update/id/{$vo.id}">标记为完成</a>
</if>
<a href="__URL__/del/id/{$vo.id}">删除</a>
</td>
</tr>
</volist>
</table>
</body>
</html>插入用的表单:
<html>
<head>
<title></title>
<meta charset="UTF-8">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
</head><body>
<h1>添加新日程</h1>
<form method="post" action="__URL__/insert">
日期:<input type="text" name="year" size="4" />年<input type="text" name="month" size="2" />月<input type="text" name="day" size="2" />日<br/>
时间:<input type="text" name="hour" size="2" />时<input type="text" name="minute" size="2" />分<br/>
描述:<input type="text" name="description" /><br/>
<input type="hidden" name="status" value="N" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>控制器:
<?php
class IndexAction extends Action
{
public function index()
{
$Calendar=new Model('Calendar');
$list=$Calendar->select();
$this->assign('list',$list);
$this->display();
}
public function add()
{
$this->display();
}
public function insert()
{
$Calendar=new Model('Calendar');
$Calendar->Create();
$result=$Calendar->add();
$this->redirect('index');
}
public function update()
{
if(isset($_GET['id']))
{
$id=$_GET['id'];
$Calendar=new Model('Calendar');
$Calendar->query("update calendar set status='Y' where id=$id");
$this->redirect('index');
}
}
public function del()
{
if(isset($_GET['id']))
{
$id=$_GET['id'];
$Calendar=new Model('Calendar');
$Calendar->query("delete from calendar where id=$id");
$this->redirect('index');
}
}
}
?>希望各位能帮忙解决下,谢谢。ThinkPHP数据库
<html>
<head>
<title></title>
<meta charset="UTF-8">
<link rel="shorcut icon" href="favicon.ico" type="image/x-icon">
</head><body>
<h1>日程表</h1>
<a href="__URL__/add/">添加新日程</a>
<table border="1" id="list">
<tr>
<td>ID</td>
<td>日期</td>
<td>时间</td>
<td>具体事项</td>
<td>是否完成</td>
<td>操作</td>
</tr>
<volist name='list' id='vo'>
<tr>
<td>{$vo.id}</td>
<td>{$vo.year}-{$vo.month}-{$vo.day}</td>
<td>{$vo.hour}:{$vo.minute}</td>
<td>{$vo.description}</td>
<td>
<if condition="$vo.status eq Y">已完成
<else />未完成
</if>
</td>
<td>
<if condition="$vo.status neq Y">
<a href="__URL__/update/id/{$vo.id}">标记为完成</a>
</if>
<a href="__URL__/del/id/{$vo.id}">删除</a>
</td>
</tr>
</volist>
</table>
</body>
</html>插入用的表单:
<html>
<head>
<title></title>
<meta charset="UTF-8">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
</head><body>
<h1>添加新日程</h1>
<form method="post" action="__URL__/insert">
日期:<input type="text" name="year" size="4" />年<input type="text" name="month" size="2" />月<input type="text" name="day" size="2" />日<br/>
时间:<input type="text" name="hour" size="2" />时<input type="text" name="minute" size="2" />分<br/>
描述:<input type="text" name="description" /><br/>
<input type="hidden" name="status" value="N" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>控制器:
<?php
class IndexAction extends Action
{
public function index()
{
$Calendar=new Model('Calendar');
$list=$Calendar->select();
$this->assign('list',$list);
$this->display();
}
public function add()
{
$this->display();
}
public function insert()
{
$Calendar=new Model('Calendar');
$Calendar->Create();
$result=$Calendar->add();
$this->redirect('index');
}
public function update()
{
if(isset($_GET['id']))
{
$id=$_GET['id'];
$Calendar=new Model('Calendar');
$Calendar->query("update calendar set status='Y' where id=$id");
$this->redirect('index');
}
}
public function del()
{
if(isset($_GET['id']))
{
$id=$_GET['id'];
$Calendar=new Model('Calendar');
$Calendar->query("delete from calendar where id=$id");
$this->redirect('index');
}
}
}
?>希望各位能帮忙解决下,谢谢。ThinkPHP数据库
判断下这句执行结果是否成功,不成功 echo $this->getErrors()看看是什么原因。
print_r($Calendar);
exit();
如果SQL有错误,应该是会打印出来的
//$Calendar->Create();
$result=$Calendar->add();就可以了,不知道为什么。
说错了 应该是这样
//$Calendar->Create();
$result=$Calendar->add($_POST);
你是怎么从网页上提交表单的?
直接从页面上form submit 还是ajax post?
TP在使用 create的时候,有一个表单令牌验证
如果你的提交数据里面没有包含这个表单令牌,就会出错
你是怎么从网页上提交表单的?
直接从页面上form submit 还是ajax post?
TP在使用 create的时候,有一个表单令牌验证
如果你的提交数据里面没有包含这个表单令牌,就会出错
说的很正确!!
是form submit提交的,请问怎么包含这个表单令牌呢?
'TOKEN_NAME'=>'__hash__', // 令牌验证的表单隐藏字段名称
'TOKEN_TYPE'=>'md5', //令牌哈希验证规则 默认为MD5
'TOKEN_RESET'=>true, //令牌验证出错后是否重置令牌 默认为true配置中加入令牌验证,那么所有的模板中的表单都会有一个hidden的字段提交到后台进行验证,你用了create创建数据对象,但是没开启TOKEN(默认关闭),那就验证失败,当然插入不进去。建议安全级别不高的就用$DataObj->data($_POST)->add();
public function insert()
{
$Calendar=new Model('Calendar');
if($Calendar->create()){
$result=$Calendar->add();
$this->redirect('index');
} else {
exit($Calendar->getError());
}
}