2016-04-15 70 views
2

當我執行編輯表格上驗證它給出了一個錯誤:行無16 未定義的變量score_id,然後對所有變量,它是student_name,SUBJECT_NAME,馬克。編輯形式驗證錯誤

我的控制器文件。

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>&nbsp;</td> 
     <td><input type="submit" name="edit" value="Submit"/></td> 
    </tr> 

    </table> 
    </form> 

我的URI是:http://localhost/ci/index.php/score_listing/edit/1

它提供了以下錯誤: 編輯得分

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: score_id

Filename: views/edit_score.php

Line Number: 16

Backtrace:

File: D:\xampp\htdocs\ci\application\views\edit_score.php Line: 16 Function: _error_handler

File: D:\xampp\htdocs\ci\application\controllers\score_listing.php Line: 64 Function: view

File: D:\xampp\htdocs\ci\index.php Line: 292 Function: require_once

+0

** **行哪一頁的無-16???將您的完整錯誤信息與您的網址一起發佈! – Saty

+0

你的條件不起作用,所以你的score_id變量沒有定義 –

+0

這種方法的郵政編碼$ this-> score-> edit_score($ id); –

回答

1

在第一次加載時或驗證假它會運行這個代碼

if ($this->form_validation->run() == FALSE) 
    { 
     $score_id = $this->uri->segment('3'); 
     $this->load->view('edit_score'); 

    } 

這裏的數組參數不會傳遞到像這樣的視圖文件$ this-> load-> view('edit_score',$ data);.所以,你必須在驗證之前宣佈的空值或其他一些你和應通過數組參數值

$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 

    } 
+0

感謝gopal先生的幫助。我更喜歡你的答案,幾乎沒有變化,如果條件改變。顯然你的建議不夠回答,但幫助我很多。 –