在thinkphp調用add方法時就提示無法訪問http://localhost..../insert
public function insert() {header('Content-type:text/html; charset=utf-8');date_default_timezone_set('Etc/GMT-8');$article = M('article');$data = array();$data['title'] = $_POST['title'];$data['author'] = $_SESSION['username'];$data['category'] = $_POST['category'];$data['content'] = $_POST['content'];$data['createtime'] = strtotime(date("Y-m-d"));if($data['category'] == '請選擇' || $data['title']=='') {$this->error('資料寫入不完整,請重新輸入');}$result = $article->add($data); }
執行到add方法就停止了,如果//add()這行在執行print_r($data);
資料完完整整的輸出來了,這是為什麼,如果用$article->execute($sql)
執行就可以插入資料。
回複討論(解決方案)
$article->add($data);
echo $article->getLastSql();
exit;
貼出sql
問題是一執行這句話,就提示
後面的根本不會顯示。
啟動偵錯模式
修改insert方法
public function insert() {
die("ok");
}
調用Article/insert 試試,看提示什嗎?
按你說的輸出:ok
我的問題是這樣的,它把資料傳遞過去了。
print_r($data);
不加$article->add($data);
一切執行正常,可以將數組中的內容輸出來
我還用$article->execute($aql)
執行sql語句,可以插入到資料庫,在資料庫中有
資料,會不會是我的php 6.0版本的問題?和
Thinkphp有衝突。 同時感謝你們的回答
//if($data['category'] == '請選擇' || $data['title']=='') {
//$this->error('資料寫入不完整,請重新輸入');
// }
把上面這句話注釋掉
$result = $article->add($data);
echo $article->getLastSql();
再試試、還有檢查下,你的model,是否做了驗證。
$result = $article->add($data);
或者改成
$result = $article->data($data)->add();
我的model類:
我都測試好長時間了,為什麼老是不行,難道是設定檔問題
要不我把我的ThinkPHP model的add()方法貼出來看看
我測試時讀取資料可以,但插入和修改就出問題。。。
Thinkphp源碼add方法,求指導:
public function add($data='',$options=array(),$replace=false) { if(empty($data)) { // 沒有傳遞資料,擷取當前資料對象的值 if(!empty($this->data)) { $data = $this->data; // 重設資料 $this->data = array(); }else{ $this->error = L('_DATA_TYPE_INVALID_'); return false; } } // 分析運算式 $options = $this->_parseOptions($options); // 資料處理 $data = $this->_facade($data); if(false === $this->_before_insert($data,$options)) { return false; } // 寫入資料到資料庫 $result = $this->db->insert($data,$options,$replace); if(false !== $result ) { $insertId = $this->getLastInsID(); if($insertId) { // 自增主鍵返回插入ID $data[$this->getPk()] = $insertId; $this->_after_insert($data,$options); return $insertId; } $this->_after_insert($data,$options); } return $result; }
問題是在log檔案裡沒有找到記錄,無從下手
你可以在 Db.class.php 的 773行附近的
public function insert($data,$options=array(),$replace=false) { $values = $fields = array(); $this->model = $options['model']; foreach ($data as $key=>$val){ $value = $this->parseValue($val); if(is_scalar($value)) { // 過濾非標量資料 $values[] = $value; $fields[] = $this->parseKey($key); } } $sql = ($replace?'REPLACE':'INSERT').' INTO '.$this->parseTable($options['table']).' ('.implode(',', $fields).') VALUES ('.implode(',', $values).')'; $sql .= $this->parseLock(isset($options['lock'])?$options['lock']:false); return $this->execute($sql); }中逐條檢查資料是否正確
你用 execute 可以插入,只表示你手寫的sql指令沒有錯誤
而 add 方法插入失敗則表示程式組裝sql指令時出現了問題
這怎麼調試 ,不會呀。。。
我用別人的環境就可以執行,我的appserv有問題,還是PHP版本的問題?
我使用其他人的php5.2.8版本可以完美執行而我的php6.0則出問題。。。