codeigniter-将数据插入到数据库不工作

时间:2015-06-10 17:15:41

标签: php codeigniter

当我点击提交按钮时,我不断收到消息“你的菜单没有被添加,请再试一次”,而代码工作正常之前但是在svn上提交之后它无法正常工作并且数据被插入到分贝

控制器:

 public function addmenu() {

    $this->load->model('organizer_model');

    $data = array(

                $data['email']=$this>session>userdata('email');
                'menu_name' => $posted_data['menu name'],
                'price' => $posted_data['price']
            );

            if($this->organizer_model->insertmenu($data)) {

    $this->session->set_flashdata('message', 'Your menu has been added');
    redirect('/menu/index', 'refresh');

    } else {

    $this->session->set_flashdata('message', 'Your menu was not added, please try again');
    redirect('/menu/index', 'refresh');
    }

}

型号:

  public function insertmenu($data) {

    $condition = "email = '" . $data['email'] . "'";
    $this->db->select('organizer_id');
    $this->db->from('organizer');
    $this->db->where($condition);
    $this->db->limit(1);
    $query = $this->db->get();

    if ($query->num_rows() > 0){

        array_pop($data); //will remove email from data

        $row = $query->row();
        $data['organizer_id'] = $row->organizer_id;

        $this->db->insert('menu', $data);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }

}

以下是观点:

  <div class="widget-body">
                <?php echo $this->session->flashdata('message'); ?>

                <form action="<?php echo site_url('organizer/addmenu'); ?>" method="post" class="form-horizontal no-margin">

                  <div class="control-group">
                    <label class="control-label" for="name">
                     Menu Name
                    </label>
                    <div class="controls controls-row">
                      <input class="span3" name="data[menu name]" type="text" placeholder="Enter menu Name">
                    </div>
                  </div>

                  <div class="control-group">
                    <label class="control-label" for="price">
                      Price
                    </label>
                    <div class="controls controls-row">
                      <input class="span3" name="data[price]" type="text" placeholder="">
                    </div>
                  </div>

                  <div class="form-actions no-margin">
                    <button type="submit" name="submit" class="btn btn-info pull-right">
                      Add menu
                    </button>
                    <div class="clearfix">
                    </div>
                  </div>

                </form>
     </div>

2 个答案:

答案 0 :(得分:0)

在你进一步完成项目之前; turn on error reporting

array(
  $data['email']=$this>session>userdata('email');
  'menu_name' => $posted_data['menu name'],
  'price' => $posted_data['price']
);

这不是正确的语法。

你最想要的是这个。

array(
  'email' => $this->session->userdata('email'),
  'menu_name' => $posted_data['menu name'],
  'price' => $posted_data['price']
);

答案 1 :(得分:0)

检查

确保加载表单助手和网址助手。

确保在控制器上的codeigniter中提交表单时使用表单验证。

从这里的php用户指南http://php.net/manual/en/reserved.variables.post.php

您输入的示例将类似于 person [0] [first_name]

<form action="" method="">
    <input type="text" name="data_posts[0][menu_name]" placeholder="Enter menu Name">
    <input type="text" name="data_posts[0][price]"  placeholder="">
</form>

<强>模型

<?php

class Model_something extends CI_Model {

public function add_menu() {
    $data_posts = $this->input->post('data_posts');

    foreach ($data_posts as $data_post) {
        $data = array(
            'email' => $this->session->userdata('email'),
            'menu_name' => $data_post['menu_name'],
            'price' => $data_post['price']
        );

        $this->db->insert('tablename', $data);
    }
}

}

<强>控制器

<?php 

class Add_menu extends CI_Controller {

    public function index() {
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('form_validation');

        $data_posts = $this->input->post('data_posts');

        foreach ($data_posts as $data_post) {
            $this->form_validation->set_rules('data_posts['.$data_post.'][menu_name]', 'Menu Name', 'required');
            $this->form_validation->set_rules('data_posts['.$data_post.'][price]', 'Price', 'required');
        }

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('some_view');
        } else {
            $this->load->model('model_something');
            $this->model_something->add_menu();
            redirect('to_success_page');
        }
    }
}

您还可以使用回调功能检查是否已插入

Codeigniter 3用户指南表单验证http://www.codeigniter.com/user_guide/libraries/form_validation.html

Codeigniter 2用户指南表单验证http://www.codeigniter.com/userguide2/libraries/form_validation.html

此外,您应该升级到新的引导程序我看到您使用旧版本。