2017-11-14 2 views
2

Codeigniter에서 양식 페이지를 볼 수 없습니다. 열려고합니다.Codeigniter에서 양식 페이지를 볼 수 없습니다.

form.php 이것은 Codeigniter의 내보기 파일입니다. 저는 Codeigniter에서 초보자입니다.이 문제를 해결할 수 없습니다.

<!DOCTYPE html> 
<html> 
<head> 

<link href="<?php echo base_url().'assets/bootstrap.min.css'; ?>" rel="stylesheet" type='text/css'> 
<!-- jQuery library --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 

<!-- Latest compiled JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
<style> 
.error{color:red} 
</style> 
</head> 
<body> 
<div class="row"> 
<div class="col-md-3"></div> 
<div class="col-md-3"> 


<h1>Student Registraion</h1> 

<?php 
echo form_open(); 
    echo form_label('First Name'); 
    $data=[ 
    'name'=>'fname', 
    'class'=>'form-control', 
    'id'=>'fname', 
    'value'=>isset($dta['fname'])?$dta['fname']:'' 
    ]; 
    echo form_input($data); 
    echo form_error('fname'); 

    echo form_label('Last Name'); 
    $data=[ 
    'name'=>'lname', 
    'class'=>'form-control', 
    'id'=>'lname', 
    'value'=>isset($dta['lname'])?$dta['lname']:'' 
    ]; 
    echo form_input($data); 
    echo form_error('email'); 
    echo form_label('Email'); 

    $data=[ 
    'name'=>'email', 
    'class'=>'form-control', 
    'id'=>'email', 
    'value'=>isset($dta['email'])?$dta['email']:'' 
    ]; 


    echo form_input($data); 

    echo form_label('Mobile Number'); 

    $data=[ 
    'name'=>'mobile', 
    'class'=>'form-control', 
    'id'=>'mobile', 
    'value'=>isset($dta['mobile'])?$dta['mobile']:'' 
    ]; 


    echo form_input($data); 
    echo form_error('mobile'); 
    echo form_label('Message'); 
    $data=[ 
    'name'=>'message', 
    'class'=>'form-control', 
    'id'=>'message', 
    'value'=>isset($dta['message'])?$dta['message']:'' 
    ]; 
    echo form_textarea($data).br(1); 
    echo form_submit('submit','submit','class="btn btn-success"'); 
    echo form_close(); 
?> 
</div> 
<div class="col-md-3"></div> 
</div> 

</body> 
</html> 

내가 열려고하면 CodeIgniter의에서 양식 페이지를 볼 수 없습니다 home.php.

enter image description here 삽입 양식을 열면 오류가 발생하며 오류 스크린 샷이 첨부되었습니다.

<?php 

class home extends CI_Controller 
{ 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('select'); 
     $this->load->helper(array('form','url','html')); 
     $this->load->library(array('form_validation','session')); 


    } 


    function records() 
    { 

     $ytl['dta']=$this->users->datas(); 
     //$this->load->view('info',$ytl); 
     //echo "<pre>controller";print_r($ytl); 
    } 
    function primezone() 
    { 

      $ytl['dta']=$this->users->datas(); 
      echo "<pre>controller";print_r($ytl); 
    } 



    function form($id) 
    { 
    //die($id); 
    if($this->input->post()) 
    { 
     //echo"<pre>";print_r($this->input->post());die; 
     $this->form_validation->set_rules('fname','First Name','required|min_length[5]'); 
     $this->form_validation->set_rules('lname','Last Name','required|callback_check_picture[lname]'); 

     //$this->form_validation->set_rules('email','Email','required'); 

     $this->form_validation->set_rules('mobile','Mobile Number','required|callback_valid_phone_number[mobile]'); 

     $this->form_validation->set_error_delimiters('<h1 class="error">', '</h1>'); 
     if($this->form_validation->run()==True) 
     { 
     $data=[ 
     'fname'=>$this->input->post('fname'), 
     'lname'=>$this->input->post('lname'), 
     'email'=>$this->input->post('email'), 
     'mobile'=>$this->input->post('mobile'), 
     'message'=>$this->input->post('message'), 
     ]; 
     $ytl=$this->select->insertdata($data); 
     if($ytl) 
     { 
      $this->session->set_flashdata('message', 'Successfully Added.'); 
    redirect('home'); 
     } 
     }else 
     { 
       //$this->load->view('form'); 
     } 

    } 
    $ytl['dta']=$this->select->getDataById($id); 
    //echo "<pre>";print_r($ytl);die; 
    $this->load->view('form',$ytl); 


} 
public function check_picture($a){ 
    if(!is_numeric($a)) 
    { 
     return true; 
    }else{ 
    $this->form_validation->set_message('check_picture', 'Please enter only char value'); 
    return False; 
} 
} 




function valid_phone_number($value) 
{ 
    $value = strlen($value); 
    //echo $value;die; 
    if ($value == 10) { 
     return true; 
    } 
    else 
    { 
     $this->form_validation->set_message('valid_phone_number', 'Mobile number not in range'); //{10} 
    return false; 
    } 
} 



    public function index() 

    { 
     //load the database 
     //$this->load->database(); 
     //load the model 
     $this->load->model('select'); 
     //load the method of model 
     $data['h']=$this->select->select(); 
     //return the data in view 
     $this->load->view('fetch_view', $data); 
     } 

} 


?> 

select.php는

이 내 모델입니다. Codeigniter에서 양식 페이지를 볼 수 없습니다. 열려고 시도합니다.

<?php 

class select extends CI_model 

{ 
    function __construct() 
     { 
     // Call the Model constructor 
     parent::__construct(); 
    $this->load->database();   
     } 


    function datas() 
    { 
     $pzi=[ 
     'name'=>'pratibha', 
     'email'=>'[email protected]', 
     'mobile'=>998858585, 
     'address'=>'noida' 

     ]; 

     return $pzi; 
    } 

    function insertdata($data) 
    { 
     $this->load->database(); 
     return $this->db->insert('student', 
     $data); 
    } 



    public function select() 
     { 
     //data is retrive from this query 

     $query = $this->db->get('student'); 
     return $query; 
     } 

    function getDataById($id) 
    { 
     $this->db->select('*'); 
     $this->db->where('id',$id); 
     $this->db->from('student'); 
     $query = $this->db->get()->row_array(); 
     return $query; 
    } 
} 

?> 
+1

팁 : 항상 $ this-> load-> database();를 읽어 오는 중입니다. config/autoload.php에서 라이브러리를로드하지 않을까요? $ autoload [ 'libraries'] = array ('database'); ' – user4419336

+0

업데이트 기능은 어디에 있습니까 ?? –

답변

0

레코드를 업데이트하는 경우 해당 레코드의 ID를 전달해야합니다. 그런 다음 ID가 있으면 업데이트 할 수 있고 그렇지 않으면 현재 수행중인 것처럼 삽입 할 수 있습니다. 그래서 컨트롤러에

:

$data=[ 
     'id'=>!empty($this->input->post('id')) ? $this->input->post('id') : null, 
     'fname'=>$this->input->post('fname'), 
     'lname'=>$this->input->post('lname'), 
     'email'=>$this->input->post('email'), 
     'mobile'=>$this->input->post('mobile'), 
     'message'=>$this->input->post('message'), 
     ]; 

그리고 모델의 :

function insertdata($data) 
    { 
     if(!empty($data['id'])) { 
      $this->db->where('id', $data['id'])->update('student',$data); 
      return $data['id']; 
     } else { 
      unset($data['id']); 
      $this->db->insert('student', $data); 
      return $this->db->insert_id(); 
     } 
    } 

이 테이블은 id라는 열이 가정합니다. 필요한 경우 변경하십시오.

+0

선생님,하지만 고맙습니다. 선생님, 코드를 적용 할 때 구문 분석 오류가 발생합니다. 내 모델 파일에서 31 번째 줄에 C : \ xampp \ htdocs \ codeIgniter \ application \ models \ select.php의 구문 오류가 발생했습니다. – Kumar

+0

죄송합니다 ... if 문에 누락 된 대괄호가 있습니다 ... 답변을 수정했습니다. – Tom

0

컨트롤러에서 insert 기능을 호출하고 있습니다. 업데이트 함수를 호출하십시오.

$this->db->where('id',$id); 
$this->db->set($data); 
$this->db->update('student');