Codeigniter 消息:使用未定义常量

Codeigniter Message: Use of undefined constant

听说是我用的代码。

$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'

您需要在数组键上添加 单引号,因为它将数组键 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);

在数组索引上加上单引号或双引号,因为数组索引是数字或字符串,而字符串是使用引号定义的。因此,将代码更改为:

$数据=

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);

数组字符串键必须用引号引起来。在您的数组 'question' 中,必须引用 'answer1'。例如'question'=>$this->input->post('questions')

需要所有数组 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')
);
$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);

What is question here you need to use question and other indexes as a string like

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

您缺少数组语法

$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);