繁体   English   中英

数据不会插入数据库(Codeigniter3)

[英]Data won't be inserted in the Database (Codeigniter3)

我的问题是,每次我单击“提交”按钮时,数据都不会插入数据库中

数据库配置文件配置正确,但我不知道问题出在哪里

这些是文件:控制器:

class Store_items extends MX_Controller {

    function __construct() {
        parent::__construct();
    }

    function create() {

        $this->load->module('site_security');
        $this->site_security->_make_sure_is_admin();

        $update_id = $this->uri->segment(3);
        $submit = $this->input->post('submit', TRUE);
        if ($submit == "Submit") {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('item_title', 'Item Title', 'required|max_length[240]');
            $this->form_validation->set_rules('item_price', 'Item Price', 'required|numeric');
            $this->form_validation->set_rules('was_price', 'Was Price', 'numeric');
            $this->form_validation->set_rules('item_description', 'Item Description', 'required');
            if ($this->form_validation->run($this) == TRUE) {
                $data = $this->fetch_data_from_post();
                if (is_numeric($update_id)) {
                    //update
                    $this->_update($update_id, $data);
                    $flash_msg = "Le produit est mise à jour avec succes.";

                    $value = '<div class="alert alert-success" >' . $flash_msg . '</div>';
                    $this->session->set_flashdata('item', '$value');
                    redirect('store_items/create/' . $update_id);
                } else {
                    // insert new item
                    $this->_insert($data);
                    $update_id = $this->get_max(); //recupere l'id du nouveau produit

                    $flash_msg = "Le produit est ins avec succs.";
                    $value = '<div class="alert alert-success" >' . $flash_msg . '</div>';

                    $this->session->set_flashdata('item', '$value');
                    redirect('store_items/create/' . $update_id);//remember the flash data

                }
            }
        }

        if ((is_numeric($update_id)) && ($submit != "Submit")) {
            $data = $this->fetch_data_from_db($update_id);

        } else {
            $data = $this->fetch_data_from_post();
        }

        if ( ! is_numeric($update_id)) {
            $data['headline'] = "Add new";
        } else {
            $data['headline'] = "update";
        }

        $data['update_id'] = $update_id;
        $data['flash'] = $this->session->flashdata('item');
        $data['view_module'] = "store_items";
        $data['view_file'] = "create";
        $this->load->module('templates');
        $this->templates->admin($data);

    }

    function manage() {
        $this->load->module('site_security');
        $this->site_security->_make_sure_is_admin();
        //echo"Manage";
        $data['view_module'] = "store_items";
        $data['view_file'] = "manage";
        $this->load->module('templates');
        $this->templates->admin($data);

    }

    function fetch_data_from_post() {

        $data['item_title'] = $this->input->post('item_title', TRUE);
        $data['item_price'] = $this->input->post('item_price', TRUE);
        $data['was_price'] = $this->input->post('was_price', TRUE);
        $data['item_description'] = $this->input->post('item_description', TRUE);

        return $data;
    }

    function fetch_data_from_db($update_id) {
        $query = $this->get_where($update_id);
        foreach ($query->result() as $row) {
            $data['item_title'] = $row->item_title;
            $data['item_url'] = $row->item_url;
            $data['item_price'] = $row->item_price;
            $data['item_description'] = $row->item_description;
            $data['big_pic'] = $row->big_pic;
            $data['small_pic'] = $row->small_pic;
            $data['was_price'] = $row->was_price;

        }
        if ( ! isset($data)) {
            $data = "";
        }

        return $data;

    }

    function get($order_by) {
        $this->load->model('mdl_store_items');
        $query = $this->mdl_store_items->get($order_by);

        return $query;
    }

    function get_with_limit($limit, $offset, $order_by) {
        $this->load->model('mdl_store_items');
        $query = $this->mdl_store_items->get_with_limit($limit, $offset, $order_by);

        return $query;
    }

    function get_where($id) {
        $this->load->model('mdl_store_items');
        $query = $this->mdl_store_items->get_where($id);

        return $query;
    }

    function get_where_custom($col, $value) {
        $this->load->model('mdl_store_items');
        $query = $this->mdl_store_items->get_where_custom($col, $value);

        return $query;
    }

    function _insert($data) {
        $this->load->model('mdl_store_items');
        $this->mdl_store_items->_insert($data);
    }

    function _update($id, $data) {
        $this->load->model('mdl_store_items');
        $this->mdl_store_items->_update($id, $data);
    }

    function _delete($id) {
        $this->load->model('mdl_store_items');
        $this->mdl_store_items->_delete($id);
    }

    function count_where($column, $value) {
        $this->load->model('mdl_store_items');
        $count = $this->mdl_store_items->count_where($column, $value);

        return $count;
    }

    function get_max() {
        $this->load->model('mdl_store_items');
        $max_id = $this->mdl_store_items->get_max();

        return $max_id;
    }

    function _custom_query($mysql_query) {
        $this->load->model('mdl_store_items');
        $query = $this->mdl_store_items->_custom_query($mysql_query);

        return $query;
    }
}

这是一个名为Create.php的视图文件,我将在其中将所有数据插入数据库。

<h1> <? =$headline ?></h1>
<?= validation_errors("<p style='color: red;'>", "</p>") ?>

<?php
if (isset($flash)) {
    echo $flash;
}
?>
<div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-header" data-original-title>
            <h2><i class="halflings-icon white edit"></i><span class="break"></span>Details du produit</h2>
            <div class="box-icon">
                <a href="#" class="btn-setting"><i class="halflings-icon white wrench"></i></a>
                <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
                <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
            </div>
        </div>
        <div class="box-content">
            <?php $form_location = base_url() . "store_items/create/" . $update_id; ?>
            <form class="form-horizontal" action="<?= $form_location ?>">
                <fieldset>
                    <div class="control-group">
                        <label class="control-label" for="typeahead">Nom du produit</label>
                        <div class="controls">
                            <input type="text" class="span6" name="item_title" value="<?= $item_title ?>">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="typeahead">Prix du produit</label>
                        <div class="controls">
                            <input type="text" class="span1" name="item_price" value="<?= $item_price ?>">

                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="typeahead">Ancien prix</label>
                        <div class="controls">
                            <input type="text" class="span1" name="was_price" value="<?= $was_price ?>">

                        </div>
                    </div>
                    <div class="control-group hidden-phone">
                        <label class="control-label" for="textarea2">Description du produit</label>
                        <div class="controls">
                                <textarea class="form-control" rows="3" id="textarea2" name="item_description">
                                    <?php echo $item_description; ?>
                                </textarea>
                        </div>
                    </div>
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary" name="submit" value="Submit">Save changes</button>
                        <button type="reset" class="btn">Cancel</button>
                    </div>
                </fieldset>
            </form>
        </div>
    </div><!--/span-->
</div><!--/row-->
if ($this->form_validation->run($this) == TRUE)

run($this)更改为run()

Form_validation方法run()参数

$group (string) -- The name of the validation group to run

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM