CodeIgniter将数据插入db

时间:2017-05-12 10:13:45

标签: php mysql codeigniter

我开始使用PHP,而且我完全不喜欢PHP,而且我将数据插入表中,没有任何反应。 我试图改变我的模型和控制器,我检查了一切,但我看不出我弄错了什么

发布控制器

<?php


class Posts extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->model('Posts_model');
    }

    public function index($page='home'){


        $data['posts']= $this->Posts_model->get_posts();

        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');
    }


    public function view($mjestoOdredista=NULL){
        $data['posts'] = $this->Posts_model->get_posts($mjestoOdredista);

        if(empty($data['posts'])){
            show_404();
        }
        $data['id'] =$data['posts'];

        $this->load->view('templates/header');
        $this->load->view('posts/view',$data);
        $this->load->view('templates/footer');
    }


    public function create(){
        $data['title'] ='Create Posts';

        $this->form_validation->set_rules('mjestoPolaska','Mjesto Polaska', 'required');
        $this->form_validation->set_rules('mjestoOdredista','Mjesto Odredista', 'required');

        if($this->form_validation->run()===FALSE){

            $this->load->view('templates/header');
            $this->load->view('posts/create',$data);
            $this->load->view('templates/footer');

        }else {
            $this->Posts_model->create_post();
            redirect('posts');
        }

    }

    public function delete($id){

        $this->Posts_model->delete_post($id);
        redirect('posts');
    }

    public function edit($mjestoOdredista){
        $data['mjestoOdredista']= $this->Posts_model->get_posts($mjestoOdredista);

        if(empty($data['mjestoOdredista'])){
            show_404();
        }
        $data['id'] = 'Edit Post';

        $this->load->view('templates/header');
        $this->load->view('posts/edit',$data);
        $this->load->view('templates/footer');
    }


    public function update(){

        $this->Posts_model->update_post();
        redirect('posts');

    }

    }

帖子模型

<?php
 class Posts_Model extends CI_Model{

    public function __construct(){
        $this->load->database();
    }

     function get_posts($mjestoOdredista=FALSE){
        if($mjestoOdredista === FALSE){
            $this->db->order_by('id','DESC');
            $query=$this->db->get('posts');
            return $query->result_array();  
        }
        $query=$this->db->get_where('posts', array('mjestoOdredista' => $mjestoOdredista));
        return $query->row_array();
    }

    //Kreiranje post
    public function create_post(){

        $mjestoPolaska = url_title($this->input->post('title'));

        $data=array(
                'mjestoPolaska' => $mjestoPolaska,
                'mjestoOdredista' => $this->input ->post('mjestoOdredista'),
                'vrsta_usluge' => $this->input ->post('vrsta_usluge'),
                'datum_polaska' => $this->input ->post('datum_polaska'),
                'datum_povratka' => $this->input ->post('datum_povratka'),
                'cijena' => $this->input ->post('cijena'),
                'broj_mjesta' => $this->input ->post('broj_mjesta'),
                'opis' => $this->input ->post('opis'),

        );

        return $this->db->insert('posts',$data);


    }

    public function delete_post($id){
        $this->db->where('id',$id);
        $this->db->delete('posts');
        return true;
    }

    public function update_post(){
        $mjestoPolaska=url_title($this->input->post('Mjesto Polaska'));
        $data=array(
                'id' =>$this->input->post('id'),
                'Mjesto Polaska' => $this->input ->post('mjestoPolaska'),
                'Mjesto Odredista' => $this->input ->post('mjestoOdredista'),
                'Vrsta usluge' => $this->input ->post('vrsta_usluge'),
                'Datum Polaska' => $this->input ->post('datum_polaska'),
                'Datum Povratka' => $this->input ->post('datum_povratka'),
                'Cijena' => $this->input ->post('cijena'),
                'Broj Mjesta' => $this->input ->post('broj_mjesta'),
                'Opis' => $this->input ->post('opis'),

        );
        $this->db->where('id',$this->input->post('id'));
        return $this->db->update('posts',$data);

    }

    }



 ?>

创建视图

<h2><?= $title;?></h2>
<?php echo form_open('posts/create');?>
<?php echo validation_errors();?>

<!DOCTYPE h2 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
<html>
<head>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
 <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">


 <div class="form-group">
    <label for="Mjesto Polaska">Mjesto Polaska</label>
    <input type="mjesto_polaska" class="form-control" id="mjesto_polaska" placeholder="Mjesto Polaska">
  </div>

  <div class="form-group">
    <label for="Mjesto Odredista">Mjesto Odredista</label>
    <input type="mjesto_odredista" class="form-control" id="mjesto_odredista" placeholder="Mjesto Odredista">
  </div>


    <div class="form-group">
  <label for="sel1">Broj slobodnih mjesta:</label>
      <select class="form-control" id="sel1">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
      </select>
    </div>

     <div class="form-group">
    <label for="Cijena">Cijena</label>
    <input type="cijena" class="form-control" id="cijena" placeholder="Cijena">
  </div>


    <div class="bootstrap-iso">
    <div class="container-fluid">
    <div class="row">
    <div class="col-md-6 col-sm-6 col-xs-12">
    <form method="post">
     <div class="form-group ">
      <label class="control-label " for="date">
       Datum Polaska
      </label>
      <div class="input-group">
       <div class="input-group-addon">
        <i class="fa fa-calendar-minus-o">
        </i>
       </div>
       <input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text"/>
      </div>
     </div>
     <div class="form-group">
      <div>
      </div>
     </div>
    </form>
   </div>
  </div>
 </div>
</div>


    <div class="bootstrap-iso">
 <div class="container-fluid">
  <div class="row">
   <div class="col-md-6 col-sm-6 col-xs-12">
    <form method="post">
     <div class="form-group ">
      <label class="control-label " for="date">
       Datum Povratka
      </label>
      <div class="input-group">
       <div class="input-group-addon">
        <i class="fa fa-calendar-minus-o">
        </i>
       </div>
       <input class="form-control" id="date" name="date" placeholder="MM/DD/YYYY" type="text"/>
      </div>
     </div>
     <div class="form-group">
      <div>
      </div>
     </div>
    </form>
   </div>
  </div>
 </div>
</div>

<div class="form-group">
  <label for="comment">Opis:</label>
  <textarea class="form-control" rows="5" id="comment"></textarea>
</div>



  <div>
      <button type="Dodaj" class="btn btn-default">Dodaj</button>
  </div>




</head>

</html> 

1 个答案:

答案 0 :(得分:0)

当您将数据简单地发布到控制器中时,您的按钮类型必须为submit

尝试:

 <div>
      <button type="submit" class="btn btn-default">Dodaj</button>
  </div>
<?php echo form_close();?>