使用CodeIgniter将表单数据插入数据库

时间:2016-11-04 18:04:25

标签: database forms codeigniter insert

以下是我遇到的问题,我使用的是HMVC,到目前为止:

routes.php文件

$route['create_qso'] = 'qso/create_qso';
$route['insert_qso'] = 'qso/insert_qso';

Qso_model.php模型

function add_qso($data){

     $data = array(
        'user_id' => $this->input->post('user_id'),
        'date' => $this->input->post('date'),
        'time_in_field' => $this->input->post('time-in-field'),
        'discipline_id' => $this->input->post('discipline'),
        'shift_id' => $this->input->post('shift'),
        'type_id' => $this->input->post('type'),
        'at_risk_category_id' => $this->input->post('at-risk'),
        'at_risk_details' => $this->input->post('at-risk-details'),
        'severity_level_id' => $this->input->post('severity-level'));



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

create_qso.php查看

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// logic for form begins    
    $user_id_val = $this->session->userdata('user_id');
    $user_username_val = $this->session->userdata('username');

    //  discipline dropdown
    $disciplines = $this->db->query('SELECT DISTINCT discipline_desc FROM discipline');
    $disciplinedropdowns = $disciplines->result();
    foreach ($disciplinedropdowns as $disciplinedropdown){
        $disciplinedropdownlist[$disciplinedropdown->discipline_desc] = $disciplinedropdown->discipline_desc;
    }
    $finaldisciplinedropdown = $disciplinedropdownlist;

    // shift dropdown
    $shifts = $this->db->query('SELECT DISTINCT shift FROM shift');
    $shiftdropdowns = $shifts->result();
    foreach ($shiftdropdowns as $shiftdropdown){
        $shiftdropdownlist[$shiftdropdown->shift] = $shiftdropdown->shift;
    }
    $finalshiftdropdown = $shiftdropdownlist;

    // Type dropdown
    $types = $this->db->query('SELECT DISTINCT type FROM type');
    $typedropdowns = $types->result();
    foreach ($typedropdowns as $typedropdown){
        $typedropdownlist[$typedropdown->type] = $typedropdown->type;
    }
    $finaltypedropdown = $typedropdownlist;

    // At Risk dropdown
    $atriskcats = $this->db->query('SELECT DISTINCT at_risk_cat FROM at_risk_cat');
    $atriskcatdropdowns = $atriskcats->result();
    foreach ($atriskcatdropdowns as $atriskcatdropdown){
        $atriskcatdropdownlist[$atriskcatdropdown->at_risk_cat] =   $atriskcatdropdown->at_risk_cat;
    }
    $finalatriskdropdown = $atriskcatdropdownlist;

    // severity_level dropdown
    $severity_levels = $this->db->query('SELECT DISTINCT severity_level FROM severity_level');
    $severity_leveldropdowns = $severity_levels->result();
    foreach ($severity_leveldropdowns as $severity_leveldropdown){
        $severity_leveldropdownlist[$severity_leveldropdown->severity_level] = $severity_leveldropdown->severity_level;
    }
    $finalseverity_leveldropdown = $severity_leveldropdownlist;?>

<div class="text-left">
<h1 class="fg-primary f900 text-uppercase">Create QSO</h1>

</div>
<div id="add_qso_form">
<?php   echo form_open('create_qso/insert_qso');   
echo form_hidden('user_id', $user_id_val);  // hiding user id
$date = date("Y-m-d H:i:s");  // getting date in mySQL format
?>
<div class="col-sm-6">    
    <div class="form-group">  <!-- adding read only date -->
        <label for="date">Date</label>
        <?php 
        $datedata = array(
            'name'          => 'date',
            'id'            => 'date',
            'value'         => $date,
            'type'          => 'datetime',
            'class'         => 'form-control',
            'readonly'      =>  'true'
        );
        echo form_input($datedata); 
        ?>
    </div>
    <div class="form-group">   <!--Adding minutes observed-->
        <label for="time-in-field">Minutes Observed</label>
        <?php 
        $timeinfield = array(
            'name'          => 'time-in-field',
            'id'            => 'time-in-field',
            'value'         => '',
            'type'          => 'number',
            'min'           => '1',
            'max'           => '480',
            'class'         => 'form-control'
        );
        echo form_input($timeinfield); 
        ?>
    </div>
    <div class="form-group">   <!--Adding discipline-->
        <label for="discipline">Discipline</label>
        <?php
        $discipline_extras = array(
            'class' => 'form-control'
        );
        echo form_dropdown('discipline',$finaldisciplinedropdown,'',$discipline_extras); 
        ?>
    </div>
    <div class="form-group">   <!--Adding shift-->
        <label for="shift">Shift</label>
        <?php
        $shift_extras = array(
            'class' => 'form-control'
        );
        echo form_dropdown('shift',$finalshiftdropdown,'',$shift_extras); 
        ?>
    </div>
    <div class="form-group"> <!--Adding type-->
        <label for="type">Type</label>
        <?php
        $type_extras = array(
            'class' => 'form-control'
        );
        echo form_dropdown('type',$finaltypedropdown,'',$type_extras); 
        ?>

    </div>
</div>
<div class="col-sm-6">
    <div class="form-group"> <!--Adding At Risk Category-->
        <label for="type">At Risk Category</label>
        <?php
        $at_risk_cat_extras = array(
            'class' => 'form-control'
        );
        echo form_dropdown('at-risk',$finalatriskdropdown,'',$at_risk_cat_extras); 
        ?>

    </div>
    <div class="form-group">   <!--Adding At Risk Details-->
        <label for="time-in-field">At Risk Details</label>
        <?php 
        $timeinfield = array(
            'name'          => 'at-risk-details',
            'id'            => 'at-risk-details',
            'value'         => '',
            'type'          => 'text',
            'class'         => 'form-control'
        );
        echo form_textarea($timeinfield); 
        ?>
    </div>
    <div class="form-group"> <!--Adding severity_level-->
        <label for="severity_level">severity_level</label>
        <?php
        $severity_level_extras = array(
            'class' => 'form-control'
        );
        echo form_dropdown('severity-level',$finalseverity_leveldropdown,'',$severity_level_extras); ?>

    </div>

</div>
<div class="text-center">
<?php
    echo form_submit('submit', 'Submit QSO');
    echo form_open('create_qso/insert_qso');
?>

</div>

</div>

Create_qso.php CONTROLLER

class Create_qso extends Site_Controller 
{

public function __construct()
{
    parent::__construct();
    //Transfers data to model
    $this->load->model('Qso_model'); // load the model

} 

public function index() {  
    //  Quick Page setup is the same as $this->load->view() method used in CI
    $this->quick_page_setup(Settings_model::$db_config['active_theme'], 'main', 'Create QSO', 'create_qso', 'header', 'footer');  

    // Setting values for table coloums

}
public function insert_qso() {

    $this->Qso_model->add_qso();
    $this->load->view('qso/insert_qso');

}
}

当我运行它时,表单显示很好,我可以输入数据,但是当我提交表单时,我得到了这个:

http://versumgas.com/error_images/form-error.jpg

我无法弄清楚如何从表单中获取数据到我的数据库中的qso表中?

1 个答案:

答案 0 :(得分:1)

你使用什么类型的HMVC?

首先,这是我的HMVC文件夹: HMVC folder

我不需要使用$route['method']来配置网址或查看。我只想在加载视图时使用$this->load->view('account',$this->_data);account.php将放在application\modules\account\views

如果它无法连接并显示错误,可能问题应该是您的HMVC源。我在bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc使用了HMVC作为我的项目。

第二次,在您的VIEW文件中,这是正确的吗?此处的表单为form_close,而不是form_open,对吧?

<div class="text-center">
<?php
    echo form_submit('submit', 'Submit QSO');
    echo form_open('create_qso/insert_qso');
?>
</div>

应该是:

<div class="text-center">
<?php
    echo form_submit('submit', 'Submit QSO');
    echo form_close('create_qso/insert_qso');
?>
</div>

我没有足够的声誉来评论您的问题。所以请回复,我会检查并回复你。