我已经连接了用户的id,我将它作为参数传递给我的控制器,这是addquestion($ id),但我一直收到这些错误:
A PHP Error was encountered Severity: Warning
Message: Missing argument 1 for Questions::addquestion()
Filename: controllers/questions.php
Line Number: 49
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: id
Filename: controllers/questions.php
Line Number: 67
A Database Error Occurred
Error Number: 1048
Column 'question_user_id' cannot be null
INSERT INTO `questions` (`question_user_id`, `question`) VALUES (NULL, 'sdf')
Filename: C:\xampp\htdocs\webdev\askme\system\database\DB_driver.php
Line Number: 330
这是我的观点:
<h1>ASK USERS</h1>
<ul class="list_items">
<?php foreach ($users as $user):?>
<li>
<div class="list_name"><b><?php echo $user->username;?></b></div>
<div class="list_body"><?php echo $user->register_date;?> </div>
<div class="list_body">
<a href="<?php echo base_url(); ?>questions/addquestion/<?php echo $user->id; ?>">Ask a question</a>
<!--<?php //echo anchor(//'questions/addquestion/'.$user->id, //'Ask a question'); ?> -->
</div>
</li>
<?php endforeach; ?>
</ul>
这是我的控制器中的addquestion函数:
public function addquestion($id) // line 49
{
$this->form_validation->set_rules('question','Your Question','trim|required|xss_clean');
if($this->form_validation->run() == FALSE)
{
//Load view and layout
$data['main_content'] = 'questions/add_question';
$this->load->view('layouts/main',$data);
}
else
{
//Validation has ran and passed
//Post values to array
$data1 = array(
'question_user_id' => $id,
'question' => $this->input->post('question')
);
if($this->Questions_model->create_question($data1))
{
$this->session->set_flashdata('question_created', 'Question sent');
// Redirect to index page with notification above
redirect('home/index');
}
}
}
请帮助我,我不知道什么是错的
答案 0 :(得分:0)
您正在向db字段发送空值,并且它不能为NULL或尝试使question_user_id
为NULL
所以你的$id
不能为空,它需要任何值,所以请使用check for this
else {
if(empty($id))
redirect('home'); // redirect to any page
$data1 = array(
'question_user_id' => $id,
'question' => $this->input->post('question')
);
答案 1 :(得分:0)
请试试这个:
public function addquestion($id='')