codeigniter- insert data into db not working

2019-09-08 06:02发布

问题:

When I click on the submit button I keep getting the message "Your menu was not added, please try again" while the code was working properly before but after committing it on svn its not working and the data was being inserted into the db

Controller:

 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');
    }

}

Model:

  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;
    }

}

Here is the view:

  <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>

回答1:

Before you go any further in your project; turn on error reporting

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

This is not proper syntax.

What you most likely wanted is this.

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


回答2:

Checks

Make sure you load your form helper and url helper.

Make sure you use form validation when submitting form in codeigniter on controller.


From this php user guide here http://php.net/manual/en/reserved.variables.post.php

Example on your input would be like 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>

Model

<?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);
    }
}

}

Controller

<?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');
        }
    }
}

You could also check if has been inserted by using callback function

Codeigniter 3 user guide form validation http://www.codeigniter.com/user_guide/libraries/form_validation.html

Codeigniter 2 user guide form validation http://www.codeigniter.com/userguide2/libraries/form_validation.html

Also you should upgrade to the new bootstrap I see your using old version.