就是无法插入记录,但是查询,更新,删除都正常。模版:
<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数据库

解决方案 »

  1.   

    $Calendar->Create();
    判断下这句执行结果是否成功,不成功 echo $this->getErrors()看看是什么原因。
      

  2.   

    还真是这句话有错误,不过请问echo $this->getErrors()这句话加在哪里?我小白系统并没有报错。
      

  3.   

    $result=$Calendar->add();
    print_r($Calendar);
    exit();
    如果SQL有错误,应该是会打印出来的
      

  4.   

    这是打印结果Model Object ( [_extModel:private] => [db:protected] => DbMysql Object ( [dbType:protected] => MYSQL [autoFree:protected] => [debug] => 1 [pconnect:protected] => [queryStr:protected] => SHOW COLUMNS FROM `calendar` [lastInsID:protected] => [numRows:protected] => 8 [numCols:protected] => 0 [transTimes:protected] => 0 [error:protected] => [linkID:protected] => Array ( [0] => Resource id #13 ) [_linkID:protected] => Resource id #13 [queryID:protected] => Resource id #14 [connected:protected] => 1 [comparison:protected] => Array ( [eq] => = [neq] => != [gt] => > [egt] => >= [lt] => < [elt] => <= [notlike] => NOT LIKE [like] => LIKE ) [selectSql:protected] => SELECT%DISTINCT% %FIELDS% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% ) [pk:protected] => id [tablePrefix:protected] => [tableSuffix:protected] => [name:protected] => Calendar [dbName:protected] => [tableName:protected] => [trueTableName:protected] => calendar [error:protected] => 闈炴硶鏁版嵁瀵硅薄锛� [fields:protected] => Array ( [0] => id [1] => year [2] => month [3] => day [4] => hour [5] => minute [6] => description [7] => status [_autoinc] => 1 [_pk] => id ) [data:protected] => Array ( ) [options:protected] => Array ( ) [_validate:protected] => Array ( ) [_auto:protected] => Array ( ) [_map:protected] => Array ( ) [autoCheckFields:protected] => 1 )
      

  5.   

    如果是这样
    //$Calendar->Create();
            $result=$Calendar->add();就可以了,不知道为什么。
      

  6.   


    说错了 应该是这样
    //$Calendar->Create();
            $result=$Calendar->add($_POST);
      

  7.   

    可能是表单令牌的问题
    你是怎么从网页上提交表单的?
    直接从页面上form submit  还是ajax post?
    TP在使用 create的时候,有一个表单令牌验证
    如果你的提交数据里面没有包含这个表单令牌,就会出错
      

  8.   

    可能是表单令牌的问题
    你是怎么从网页上提交表单的?
    直接从页面上form submit  还是ajax post?
    TP在使用 create的时候,有一个表单令牌验证
    如果你的提交数据里面没有包含这个表单令牌,就会出错 
    说的很正确!!
      

  9.   


    是form submit提交的,请问怎么包含这个表单令牌呢?
      

  10.   

    'TOKEN_ON'=>true,  // 是否开启令牌验证 默认关闭
    'TOKEN_NAME'=>'__hash__',    // 令牌验证的表单隐藏字段名称
    'TOKEN_TYPE'=>'md5',  //令牌哈希验证规则 默认为MD5
    'TOKEN_RESET'=>true,  //令牌验证出错后是否重置令牌 默认为true配置中加入令牌验证,那么所有的模板中的表单都会有一个hidden的字段提交到后台进行验证,你用了create创建数据对象,但是没开启TOKEN(默认关闭),那就验证失败,当然插入不进去。建议安全级别不高的就用$DataObj->data($_POST)->add();
      

  11.   

    echo $Calendar->getLastSql();试试
      

  12.   


    public function insert()
        {
            $Calendar=new Model('Calendar');
            if($Calendar->create()){
                $result=$Calendar->add();
                $this->redirect('index');
            } else {
                exit($Calendar->getError());
            }
        }
      

  13.   

    还是多看看tp的使用手册,这样就避免像这样的错误。有问题请加qq群:7948162