Home  >  Article  >  Backend Development  >  用ThinkPHP做的实验,无法往数据表里插入记录。

用ThinkPHP做的实验,无法往数据表里插入记录。

WBOY
WBOYOriginal
2016-06-23 14:02:14894browse

就是无法插入记录,但是查询,更新,删除都正常。

模版:

  

日程表

添加新日程
ID 日期 时间 具体事项 是否完成 操作
{$vo.id} {$vo.year}-{$vo.month}-{$vo.day} {$vo.hour}:{$vo.minute} {$vo.description} 已完成 未完成 标记为完成 删除


插入用的表单:

添加新日程

日期:
时间:
描述:



控制器:
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');		}	}}?>


希望各位能帮忙解决下,谢谢。


回复讨论(解决方案)

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

$Calendar->Create();
判断下这句执行结果是否成功,不成功 echo $this->getErrors()看看是什么原因。
还真是这句话有错误,不过请问echo $this->getErrors()这句话加在哪里?我小白。。。系统并没有报错。

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

$result=$Calendar->add();
print_r($Calendar);
exit();
如果SQL有错误,应该是会打印出来的
这是打印结果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] =>   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 )

????版?瀵硅薄锛? 
非法数据对象

????版?瀵硅薄锛? 
非法数据对象
能具体说说吗?应该不是代码问题了,把光盘里文件直接放进去也不行。

如果是这样

//$Calendar->Create();        $result=$Calendar->add();


就可以了,不知道为什么。

如果是这样
PHP code?12//$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  还是ajax post?
TP在使用 create的时候,有一个表单令牌验证
如果你的提交数据里面没有包含这个表单令牌,就会出错

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

'TOKEN_ON'=>true,  // 是否开启令牌验证 默认关闭
'TOKEN_NAME'=>'__hash__',    // 令牌验证的表单隐藏字段名称
'TOKEN_TYPE'=>'md5',  //令牌哈希验证规则 默认为MD5
'TOKEN_RESET'=>true,  //令牌验证出错后是否重置令牌 默认为true

配置中加入令牌验证,那么所有的模板中的表单都会有一个hidden的字段提交到后台进行验证,你用了create创建数据对象,但是没开启TOKEN(默认关闭),那就验证失败,当然插入不进去。

建议安全级别不高的就用$DataObj->data($_POST)->add();

echo $Calendar->getLastSql();试试

能否将你数据库表结构贴出来看看,有可能是数据库的字段类型错了,该插入整形数据的时候插入字符串了。

引用 1 楼 sjh717142 的回复:$Calendar->Create();
判断下这句执行结果是否成功,不成功 echo $this->getErrors()看看是什么原因。
还真是这句话有错误,不过请问echo $this->getErrors()这句话加在哪里?我小白。。。系统并没有报错。

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn