2017-11-06 1 views
0

나는 코 몬트 섹션에서 필자 소개 기능에 문제가있는 Codeigniter에서 블로그를 개발 중이다. 보기/양식, 컨트롤러 및 모델 모두 괜찮지 만 여전히 DB에 행을 삽입 할 수없는 것 같습니다. 누군가 나를 도울 수 있습니까?codeigniter에 코 몬트 문제 삽입하기

보기 comments.php가

<div class="container"> 
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous"> 
    <link href="<?php echo base_url(); ?>template/css/style.css" rel="stylesheet"> 
    <div class="row"> 
    <!-- Contenedor Principal --> 
    <div class="comments-container"> 
     <h1>Coments</h1> 
     <h3><a data-toggle="modal" data-target="#myModal" href="#">Add a coment</a></h3> 
     <ul id="comments-list" class="comments-list"> 
     <li> 
      <?php foreach ($result as $result):?> 
      <div class="comment-main-level"> 
      <!-- Contenedor del Comentario --> 
      <div class="comment-box"> 
       <div class="comment-head"> 
       <h6 class="comment-name by-author"><?php echo $result->name; ?></h6> 
       <span><?php echo time_elapsed_string($result->created); ?></span> 
       </div> 
       <div class="comment-content"> 
       <?php echo $result->body; ?> 
       </div> 
      </div> 
      </div> 
      <?php endforeach; ?> 
     </li> 
     </ul> 
    </div> 
    </div> 
</div> 
<!-- Modal --> 
    <div class="modal fade" id="myModal" role="dialog"> 
    <div class="modal-dialog"> 
     <!-- Modal content--> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <h4 class="modal-title">Add a coment</h4> 
     </div> 
     <div class="modal-body"> 
      <form method="post" action="<?php echo base_url('index.php/coment'); ?>" > 
      <div class="form-group"> 
       <label for="name">Your name:</label> 
       <input type="hidden" name="post" id="post" value="22"/> 
       <input class="form-control" type="text" name="name" id="name" /><br /> 
      </div> 
      <div class="form-group"> 
       <label for="coment">Your coment:</label> 
       <textarea class="form-control" name="body" id="body" rows="10" colums="3"></textarea><br /> 
      </div> 
      <button type="submit" class="btn btn-default" data-dismiss="modal">Submit</button> 
      </form> 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 
<?php $this->load->view("includes/footer"); ?> 

coment.php

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
    class Coment extends CI_Controller{ 
    public function __construct(){ 
     parent::__construct(); 
     $this->load->model('coment_model'); 
    } 

    public function index(){ 
     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('name', 'name', 'trim|required|min_length[5]'); 
     $this->form_validation->set_rules('body', 'body', 'trim|required|min_length[5]'); 

     if ($this->form_validation->run() == FALSE) { 
     echo "<script>alert('Your coment could not be posted');</script>"; 
     }else{ 
      $data = array(
      'post' => $this->input->post('post'), 
      'name' => $this->input->post('name'), 
      'body' => $this->input->post('body') 
     ); 
     $this->coment_model($data); 
     $data['message'] = 'Coment Inserted Successfully'; 
     } 
    } 
    } 
?> 

그리고 finnaly 모델 coment_model.php이 코드에

<?php  
    class coment_model extends CI_Model{ 
     public function __construct() { 
     parent::__construct(); 
     } 

     public function index($data){ 
     // Inserting in Table(students) of Database(college) 
     $this->db->insert('comment', $data); 
     } 
    } 
?> 
+0

양식을 제출하면 어떻게됩니까? 어떤 오류가 있습니까? 추적 했습니까? –

+0

컨트롤러에서 모델 함수 '$ this-> coment_model-> index ($ data)'를 호출하지 않습니다. https://www.codeigniter.com/user_guide/general/models.html#loading-a-model – user4419336

+0

무엇입니까? 당신이 직면 한 오류? –

답변

0

를 기반 컨트롤러는 I 만 있다고 가정 할 수 있습니다 삽입물이 잘못된 표를 가리키고 있습니다.

public function index($data){ 
    // Inserting in Table(students) of Database(college) 
    $this->db->insert('comment', $data); 
} 

주석 처리 된 행을 올바르게 읽으면 테이블 이름이 "students"이고 삽입 지점이 "comment"라는 테이블을 가리 킵니다.

테이블이 존재하고 모든 필드에 대해 동일한 검사를 수행 했습니까?

그래도 문제가 해결되지 않으면보고를 돌려 보내고 질문에 오류를 넣어 대답을 업데이트하십시오.

+0

실제로 이것은 다른 프로젝트의 메모 일 뿐이며 코드 자체와는 아무런 관련이 없습니다. 이 테이블을 '주석'이라고합니다. 불행히도 양식을 제출할 때 오류가 표시되지 않습니다. 그것은 잘 작동하고 그것은 hasnt 때 실행 된 것처럼 나를 같은 페이지로 리디렉션합니다. 무슨 일이 일어나고 있는지 어떤 단서가 있습니까? –

+0

방금 ​​문제를 발견했습니다. 부트 스트랩 모달이 나를 일으켰습니다. 저를 도와주기 위해 시간을내어 주셔서 대단히 감사합니다. –