Codeigniter-将数据插入数据库不起作用 [英] codeigniter- insert data into db not working

查看:70
本文介绍了Codeigniter-将数据插入数据库不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击提交按钮时,在代码正常工作之前(但在svn上提交该代码后却无法正常工作且数据已插入到)中时,我不断收到消息未添加菜单,请重试。 db

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

控制器:

 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>


推荐答案

检查

确保加载表单帮助器和url帮助器。

Make sure you load your form helper and url helper.

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

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

从此php用户指南中 http://php.net/manual/en/reserved.variables.post.php

From this php user guide here 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 3 user guide form validation http://www.codeigniter.com/user_guide/libraries/form_validation.html

Codeigniter 2用户指南表单验证 http://www.codeigniter.com/userguide2/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.

这篇关于Codeigniter-将数据插入数据库不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆