2016-11-15 97 views
0

聽到是我使用的代碼。Codeigniter消息:使用未定義常量

$data = array( question=>$this->input->post('questions'), 
       answer1=>$this->input->post('answer1'), 
       answer2=>$this->input->post('answer2'), 
       answer3=>$this->input->post('answer3'), 
       answer4=>$this->input->post('answer4'), 
       answer5=>$this->input->post('answer5'), 
       correctanswer=>$this->input->post('correctanswer') 
      ); 
$this->db->insert('questionandanswers',$data); 

請提供此通知消息的幫助。

一個PHP錯誤遇到

Severity: Notice 
Message: Use of undefined constant question - assumed 'question' 

回答

1

您需要添加數組鍵單引號,因爲它考慮陣列關鍵question恆定。

你的陣列應該是這樣..

$data = array(
      'question'=>$this->input->post('questions'), 
      'answer1'=>$this->input->post('answer1'), 
      'answer2'=>$this->input->post('answer2'), 
      'answer3'=>$this->input->post('answer3'), 
      'answer4'=>$this->input->post('answer4'), 
      'answer5'=>$this->input->post('answer5'), 
      'correctanswer'=>$this->input->post('correctanswer') 
    ); 
    $this->db->insert('questionandanswers',$data); 
+0

謝謝,它的工作 – Chetan

+0

您的歡迎..... –

0

放在數組索引單或雙引號,因爲數組索引爲數字或字符串,字符串使用引號定義。因此,改變像代碼:

$數據=

array(
'question'=>$this->input->post('questions'), 
'answer1'=>$this->input->post('answer1'), 
'answer2'=>$this->input->post('answer2'), 
'answer3'=>$this->input->post('answer3'), 
'answer4'=>$this->input->post('answer4'), 
'answer5'=>$this->input->post('answer5'), 
'correctanswer'=>$this->input->post('correctanswer')); 

$this->db->insert('questionandanswers',$data); 
+0

是的,它的工作 – Chetan

0

陣列串鑰匙必須加引號。在你的數組'問題'中,必須引用'answer1'。例如'question'=>$this->input->post('questions')

0

需要將所有的陣列狀波紋管key單或雙引號:

$data = array(
      "question"=>$this->input->post('questions'), 
      "answer1"=>$this->input->post('answer1'), 
      "answer2"=>$this->input->post('answer2'), 
      "answer3"=>$this->input->post('answer3'), 
      "answer4"=>$this->input->post('answer4'), 
      "answer5"=>$this->input->post('answer5'), 
      "correctanswer"=>$this->input->post('correctanswer') 
); 
0
$data = array(question=>$this->input->post('questions'),answer1=>$this->input->post('answer1'),answer2=>$this->input->post('answer2'),answer3=>$this->input->post('answer3'),answer4=>$this->input->post('answer4'),answer5=>$this->input->post('answer5'),correctanswer=>$this->input->post('correctanswer'));$this->db->insert('questionandanswers',$data); 

什麼是這裏的問題,你需要使用的問題和其他指標作爲一個字符串像

array('question' => $this->input->post('questions')) 
0

您缺少陣列語法

$data = array( 'question'=>$this->input->post('questions'), 
       'answer1'=>$this->input->post('answer1'), 
       'answer2'=>$this->input->post('answer2'), 
       'answer3'=>$this->input->post('answer3'), 
       'answer4'=>$this->input->post('answer4'), 
       'answer5'=>$this->input->post('answer5'), 
       'correctanswer'=>$this->input->post('correctanswer')); 
$this->db->insert('questionandanswers',$data);