当我在编辑表单上执行验证时,它会出错: 未定义的变量score_id在第16行,然后是所有变量,即student_name,subject_name,marks。
我的控制器文件。
public function edit($id)
{
$this->load->library('form_validation');
$score_id = $this->uri->segment('3');
$this->form_validation->set_rules('std_name','student_name','trim|required|alpha');
$this->form_validation->set_rules('txt_name','subject_name','trim|required|alpha');
$this->form_validation->set_rules('marks','marks','required|numeric');
if ($this->form_validation->run() == FALSE)
{
$score_id = $this->uri->segment('3');
$this->load->view('edit_score');
}
elseif($this->input->post('edit') && $this->input->post('edit_id')!='')
{
$score_id = $this->uri->segment('3');
$data = array(
'score_id' => $this->input->post('score_id'),
'student_name' => $this->input->post('std_name'),
'subject_name' => $this->input->post('txt_name'),
'marks' => $this->input->post('marks'),
);
$result = $this->score->update_record($id,$data);
header('location:'.base_url().'index.php/score_listing');
}
if($id)
{
echo "this is score_id".$id;
$result = $this->score->edit_score($id);
$data['action'] = 'edit';
$data['score_id'] = $result[0]->score_id;
$data['student_name'] = $result[0]->student_name;
$data['subject_name'] = $result[0]->subject_name;
$data['marks'] = $result[0]->marks;
}
echo "welome to edit page";
$this->load->view('edit_score',$data);
}
这是我的编辑文件
<h1>Edit score</h1>
<?php $action = $score_id!='' ? 'edit/'.$score_id : 'add'; ?>
<form name="edit_score" method="post" action="<?php echo $action; ?>">
<input type="hidden" name="score_id" value="<?php echo $score_id;?>"/>
<?php echo form_hidden('admin_id',$this->load->session->userdata('admin_id')); ?>
<!--for above function use form helper in controller file -->
<table style="width:100%" border="0">
<tr>
<td>student Name:</td>
<td><input type="text" name="std_name" value="<?php echo $student_name; ?>"/></td>
<?php echo form_error("std_name"); ?>
</tr>
<tr>
<td>subject Name:</td>
<td><input type="text" name="txt_name" value="<?php echo $subject_name; ?>"/></td>
<?php echo form_error("txt_name"); ?>
</tr>
<tr>
<td>max marks:</td>
<td><input type="text" name="marks" value="<?php echo $marks; ?>" /></td>
<?php echo form_error("marks"); ?>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="edit" value="Submit"/></td>
</tr>
</table>
</form>
我的uri是:http://localhost/ci/index.php/score_listing/edit/1
它出现以下错误: 编辑分数
遇到PHP错误
严重性:注意
消息:未定义的变量:score_id
文件名:views / edit_score.php
行号:16
回溯:
文件:D:\ xampp \ htdocs \ ci \ application \ views \ edit_score.php 行:16 功能:_error_handler
文件:D:\ xampp \ htdocs \ ci \ application \ controllers \ score_listing.php 行:64 功能:查看
文件:D:\ xampp \ htdocs \ ci \ index.php 行:292 功能:require_once
答案 0 :(得分:1)
首次加载或验证时,它将运行此代码
if ($this->form_validation->run() == FALSE)
{
$score_id = $this->uri->segment('3');
$this->load->view('edit_score');
}
此处数组参数未传递给视图文件,如$ this-&gt; load-&gt; view('edit_score',$ data);.因此,您已经在验证之前声明了null值或其他一些值,并且应该传递数组参数值
$data = array(
'score_id' => '',
'student_name' => '',
'subject_name' => '',
'marks' => '',
);
if ($this->form_validation->run() == FALSE)
{
$score_id = $this->uri->segment('3');
$this->load->view('edit_score',$data);// here is changes pass $data
}