简体   繁体   English

Codeigniter表单不会将数据插入数据库

[英]Codeigniter Form Won't Insert Data Into Database

I have a web app built on Codeigniter but I don't know which version it is. 我有一个基于Codeigniter构建的Web应用程序,但我不知道它是哪个版本。 My job is just adding a simple form for this web app. 我的工作只是为此Web应用程序添加一个简单的表单。 The form should look like this: 该表格应如下所示:

在此处输入图片说明


And then here's my VIEW: 然后是我的观点:

<div class="box">
            <h4 class="white">ADD CIT Details</h4>
        <div class="box-container">
            <div class="modal-body form">


        <form action="#" id="formcit" class="form-horizontal">
        <?php echo form_open('insert_cit_ctrl'); ?>
          <div class="form-body">
          <?php if (isset($message)) { ?>
            <CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
            <?php } ?>

            <div class="form-group">
              <label class="control-label col-md-3">Terminal ID</label> <?php echo form_error('dterminalid'); ?>
              <div class="col-md-9">
                <?php echo form_input(array('id' => 'dterminalid', 'name' => 'dterminalid')); ?>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3">CIT</label>
              <div class="col-md-9">
                <select class="form-control" id="selectcit" >
                  <option value="none">-- Select CIT --</option>
                  <option value="CMS">1. CMS</option>
                  <option value="ALPHA">2. ALPHA</option>
                </select>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3">Branch</label> <?php echo form_error('dbranch'); ?>
              <div class="col-md-9">
                <?php echo form_input(array('id' => 'dbranch', 'name' => 'dbranch')); ?>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3">COMM</label> <?php echo form_error('dcomm'); ?>
              <div class="col-md-9">
                <?php echo form_input(array('id' => 'dcomm', 'name' => 'dcomm')); ?>
              </div>
            </div>

             <div class="form-group">
              <label class="control-label col-md-3">Machine Type</label> <?php echo form_error('dmachinetype'); ?>
              <div class="col-md-9">
                <?php echo form_input(array('id' => 'dmachinetype', 'name' => 'dmachinetype')); ?>
              </div>
            </div>

            <?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>


          </div>
          <?php echo form_close(); ?>
        </form>
          </div>
        </div><!-- end of div.box-container -->
        </div> <!-- end of div.box -->  

And then this is my controller: 然后这是我的控制器:

function insert_cit_ctrl() {
        $this->load->library('form_validation');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->form_validation->set_rules('dterminalid', 'Terminal ID', 'required');
        $this->form_validation->set_rules('dcit', 'CIT', 'required');
        $this->form_validation->set_rules('dbranch', 'Branch', 'required');
        $this->form_validation->set_rules('dcomm', 'COMM', 'required');
        $this->form_validation->set_rules('dmachinetype', 'Machine Type', 'required');

        if ($this->form_validation->run() == FALSE) {
        echo "<script>alert('Something wrong with your input');</script>";
        } else {
        //Setting values for tabel columns
        $data = array(
        'terminal_id' => $this->input->post('dterminalid'),
        'cit' => $this->input->post('dcit'),
        'branch' => $this->input->post('dbranch'),
        'comm' => $this->input->post('dcomm'),
        'machine_type' => $this->input->post('dmachinetype')
        );
        //Transfering data to Model
        $this->cit_model->modeladdcit($data);
        $data['message'] = 'Data Inserted Successfully';
        //Loading View
        $this->load->view('template', $data);
        }
    }

And here's my model: 这是我的模型:

<?php
class Cit_model extends CI_Model{
function __construct() {
parent::__construct();

}

function modeladdcit($data){
    $this->load->database('database_office', 'TRUE');
    $this->db->insert('tbl_cit', $data);
    }
}
?>

And the result? 结果呢? Here's my database: 这是我的数据库:

在此处输入图片说明

There's no error at all. 完全没有错误。 It's just that after I submit the data, it won't appeared on my database, not a single data. 只是在我提交数据之后,它不会出现在数据库中,也不会出现在单个数据中。 Meaning, that data is not even INSERT INTO the database. 意思是,该数据甚至没有插入到数据库中。

I don't understand. 我不明白 What is wrong with my code? 我的代码有什么问题?


UPDATE: 更新:

Ok, so I followed your tips guys, like this: 好的,所以我按照您的提示来做,就像这样:

<form action="<?php echo base_url("evangelion/insert_cit_ctrl"); ?>" id="formcit" method="POST" class="form-horizontal">

What actually happened was... NOTHING. 实际发生的事情是……没什么。 I mean, the data still not inserted into the database. 我的意思是,数据仍未插入数据库。 And worse, the page ruined, it called only the function, not the whole page like it used to. 更糟糕的是,页面毁了,它只调用了函数,而不是像过去那样调用整个页面。

将动作和方法添加到表单中

<form action="<?php echo base_url("controller_name/function_name"); ?>" id="formcit" method="POST" class="form-horizontal">
First try to display errors like this

you can find where you are wrong.. 你可以找到你错了。

where you call controller in codeigniter you must call controller 您在codeigniter中调用控制器的位置,必须调用控制器

action="controllername/actionname"

 <?php
        echo ini_get('display_errors');

       if (!ini_get('display_errors')) {
        ini_set('display_errors', '1');
         }

           echo ini_get('display_errors');
          ?>

i thought you did not mention method type in form 我以为您没有在表格中提及方法类型

method ="POST"

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

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