使用codeigniter 3插入DB

时间:2016-07-24 12:31:12

标签: php codeigniter

我试图在我的数据库中添加新帖子。我创建了模型,控制器和视图。 Actualy即时通讯使用rest api,但现在我想用纯PHP强力执行。 但表单验证后什么也没有。因此,当我尝试发布时,不会发生。 这是我的代码。

型号:

// Create
function create($data) {
 // Insert data into DB
 $this->db->insert('blog', $data);
 return $this->db->insert_id();
}

控制器:

public function add() {
 if ($this->ion_auth->is_admin()) {

   // Validation rules
   $this->form_validation->set_rules('title', 'Titel', 'required');
   $this->form_validation->set_rules('teaser', 'Teaser', 'required');
   $this->form_validation->set_rules('full', 'Volltext', 'required');

   if (($this->form_validation->run() == FALSE)) {
     $this->load->view('templates/backend/header', $this->data);
     $this->load->view('pages/backend/blog/add', $this->data);
     $this->load->view('templates/backend/footer');
   } else {
     if($this->input->post()) {
       $data = array(
         'title' => $this->input->post('title'),
         'teaser' => $this->input->post('teaser'),
         'full' => $this->input->post('full')
       );
       $this->blog_model->create($data);
       redirect(base_url().'blog/');
     }
   }
 } else {
   redirect('login');
 }
}

至少我的观点是:

<div class="uk-margin-top">
<?php $attributes = array("class" => "uk-panel uk-panel-box uk-form  uk-margin-lage-bottom", "id" => "add-form", "method" => "post");
        echo form_open("/backend/blog/add", $attributes); ?>


  <div class="uk-form-row">
    <label class="uk-form-label" for="title">Title</label>
    <input id="title" class="uk-width-1-1 uk-form-large title redactor-box" name="title" placeholder="Beitragstitel" type="text"
           value="<?php echo set_value('title'); ?>"/>
    <span class="uk-text-danger"><?php echo form_error('title'); ?></span>
  </div>

  <div class="uk-form-row">
  <label class="uk-form-label" for="teaser">Teaser</label>
  <textarea id="teaser" class="uk-width-1-1 uk-form-large teaser redactor-box" name="teaser" data-uk-htmleditor></textarea>
    <span class="uk-text-danger"><?php echo form_error('teaser'); ?></span>
  </div>

  <div class="uk-form-row">
    <label class="uk-form-label" for="body">Body</label>
    <textarea id="full" name="full" rows="4" placeholder="Ihre Nachricht"
          value="<?php echo set_value('full'); ?>"></textarea>
    <span class="uk-text-danger"><?php echo form_error('full'); ?></span>
  </div>

  <div class="uk-form-row">
    <a class="uk-button uk-button-success" data-action="add-post">Submit</a>
  </div>

<?php echo form_close(); ?>
</div>

所以我的问题是,当我点击我的提交按钮时 - 什么都没有。也许你可以告诉我我的问题所在。

谢谢!

1 个答案:

答案 0 :(得分:1)

对于您的控制器,我认为您缺少表单助手和验证库。我在代码中包含了其他注释,但试试这个:

public function add() {

// you need to load these in:
$this->load->helper('form');
$this->load->library('form_validation');

// I am assuming ion_auth is working, however, I would try this code without
// this conditional statement
if ($this->ion_auth->is_admin()) {

    // Validation rules
    // Make sure the second parameter is right. I think Titel should be Title.
    $this->form_validation->set_rules('title', 'Titel', 'required');
    $this->form_validation->set_rules('teaser', 'Teaser', 'required');
    $this->form_validation->set_rules('full', 'Volltext', 'required');

    // added a triple === instead of == for stricter type checking
    if (($this->form_validation->run() === FALSE)) {

        // I am assuming $this->data is a property of your controller class
        $this->load->view('templates/backend/header', $this->data);
        $this->load->view('pages/backend/blog/add', $this->data);
        $this->load->view('templates/backend/footer');

    } else {

        // Check if the form was submitted via $_POST method
        if($this->input->post()) {

            // I removed your $data array and created it in the model.
            // I added a condition here to check if the data was successfully inserted
            if ($this->blog_model->create()) {
                redirect(base_url().'blog/');
            } else {
                // output an error message or redirect
            }

        }

    }
} else {

    redirect('login');

}

}

对于您的模型,我认为您没有将任何数据传递给您的模型。请为您的模型尝试以下操作:

public function create()
{
    // you need to pass an array of data or an object
    // the array key corresponds to your db table column
    // the array value corresponds to your views input field names
    $data = array(
        'name' => $this->input->post('title'),
        'teaser' => $this->input->post('teaser'),
        'full' => $this->input->post('full')
    );

    // returns true or false
    return $this->db->insert('blog', $data);
}