2014-03-05 129 views
0
<?php 

class CarrierController extends AppController { 
public $helpers = array('Html', 'Form', 'Session'); 
    public $components = array('Session'); 


    var $uses=array('Carrier'); 
    public function index(){ 
    // $this->set('posts', $this->carrier->find('all')); 
     if($this->request->is('post')){ 
      Configure::read(); 
     pr($this->data); 
      $this->Carrier->create(); 
      $filename = null; 

if (
    !empty($this->request->data['Carrier']['Resume']['tmp_name']) 
    && is_uploaded_file($this->request->data['Carrier']['Resume']['tmp_name']) 
) { 
    // Strip path information 
    $filename = basename($this->request->data['Carrier']['Resume']['name']); 
    move_uploaded_file(
     $this->data['Carrier']['Resume']['tmp_name'], 
     WWW_ROOT . DS . 'documents' . DS . $filename 
    ); 
} 

// Set the file-name only to save in the database 
$this->data['Carrier']['Resume'] = $filename; 
    pr($this->data); 
      if ($this->Carrier->save($this->request->data)) { 

       $this->Session->setFlash(__('Your Details has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('Unable to add your Details')); 
      } 
      } 
     } 

} 
?> 

視圖/電信/ index.php文件SQLSTATE [42S22]:列未找到:1054未知列在 '字段列表'

<h1>welcome to Carriers</h1> 
<br /> 
<br/> 
<?php echo $this->Form->create('Carrier', array('enctype' => 'multipart/form-data'));?> 
<table> 
<tr><h3>Register here</h3></tr> 

<tr><td>F.Name</td><td><?php echo $this->Form->text('fname'); ?></td></tr> 
<tr><td>L.Name</td><td><?php echo $this->Form->text('lname');?></td></tr> 
<tr><td>Date Of Birth</td><td><?php echo $this->Form->date('dob');?></td></tr> 
<tr><td>Degree</td><td><?php echo $this->Form->select('field', array('options' => array('B.E','B.sc','Mca','Mtech','Mba'))); ?></td></tr> 
<tr><td>Sex</td><td><?php 
$options=array('M'=>'Male','F'=>'Female'); 
$attributes=array('legend'=>false); 
echo $this->Form->radio('gender',$options,$attributes); 
?></td></tr> 
<tr><td><?php echo $this->Form->input('Carrier.Resume', array('between'=>'<br />','type'=>'file'));?></td></tr> 

<tr><td><?php echo $this->Form->end('Apply');?></td></tr> 



</table> 

我得到錯誤,如

數據庫錯誤 '數組' 錯誤:SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'Array' SQL查詢:INSERT INTO cake_php_demo1carrierfnamelnamedobfieldgenderResume)VALUES( 'Thulasiram', '爸爸', '2014年3月25日', '1', 'M',陣列)

+0

爲什麼不簡單地使用Cake Uploader來解決這個問題?它會處理上傳並保存到數據庫爲你。 – skywalker

回答

0

您正在使用$this->data['Carrier']['Resume'] = $filename;這是正確的,但你打電話保存在$this->request->data

所以,你應該將其更改爲

// Set the file-name only to save in the database 
$this->request->data['Carrier']['Resume'] = $filename; 
pr($this->data); 
if ($this->Carrier->save($this->request->data)) { 
    // ... 
} 
+0

感謝您迴應..still不工作 –

+0

嘗試只用'$ this-> request-> data',就像我現在放在答案中 – cornelb

0

看起來對我來說,就好像$this->request->data['Carrier']['Resume']是一個數組(早在你的代碼是指$this->request->data['Carrier']['Resume']['tmp_name']$this->request->data['Carrier']['Resume']['name']),所以你可能需要unset它分配$ filename來之前它。嘗試:

// Set the file-name only to save in the database 
unset($this->request->data['Carrier']['Resume']); 
$this->request->data['Carrier']['Resume'] = $filename; 
相關問題