表单数据不会在codeigniter中插入数据库

时间:2016-07-01 09:03:44

标签: php mysql codeigniter

我有一个表单来插入数据。当我单击提交按钮数据时不插入到database.submit表单而不填写表单验证不起作用。任何人都可以帮助我。

控制器

//load client from table

    $data['client'] = $this->project_list_model->get_client();

    $this->form_validation->set_rules('location', 'Location', 'required');
    $this->form_validation->set_rules('name', 'Project Name', 'required');
    $this->form_validation->set_rules('category', 'Category', 'required');
    $this->form_validation->set_rules('str_date', 'Start Date', 'required');
    $this->form_validation->set_rules('end_date', 'End Date', 'required');

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('add_project', $data);
    } else {
        $data = array(
        //'staff_id' => ($this->session->userdata['logged_in']['id']),
                        'project_name' => $this->input->post('name'),
                        'client_id' => $this->input->post('client'),
                        'location' =>  md5($this->input->post('location')),
                        'start_date' => $this->input->post('str_date'),
                        'end_date' => $this->input->post('end_date')
                        );
        $result = $this->project_list_model->new_project($data);
        $data['message_display'] = 'Add Project Successfully !';
        $this->load->view('add_project', $data);

    }
}

模型

//    add new project
function new_project($data){

    // Inserting in Table(students) of Database(college)
    $this->db->insert('project', $data);

}

查看

<?php
echo form_open('project_list/add_project');
?>

<!--display successful message-->                 
<?php
if (isset($message_display)) {
    echo "<div class='message alert alert-success'>";
    echo $message_display;
    echo "</div>";
}
?>
<!--display successful message--> 

 <?php                                          
//hidden value user session id
echo form_hidden('staff_id',($this->session->userdata['logged_in']['id']) );
?>   
<div class="form-body">
     <div class="form-horizontal" role="form"> 

 <!--client-->
<div class="form-group">
    <label class="control-label col-md-1"><b>Client Name </b>&nbsp;&nbsp;</label>
    <div class="col-md-9">
    <?php
       $attributes = 'class = "form-control" id = "client" style="width:80%; height:35px;"';
        echo form_dropdown('client',$client, set_value('client'), $attributes);?>
    </div>  
</div>
<!--client-->

<br>

<!--Category--> 
<div class="form-group">
    <label class="control-label col-md-1"><b>Category </b>&nbsp;&nbsp;</label>
    <div class="col-md-9">
         <select name="type" style="width:80%; height:35px;" >
    <option selected="selected" >SELECT CATEGORY</option>
<?php
    // the gender array
    $type = array("New Build", "Maintain");

    // Iterating through the type array
    foreach($type as $type){
?>
        <option value="<?php echo strtolower($type); ?>"><?php echo $type; ?></option>
<?php
    }
?>

        </select> 
     </div>
</div>

 <!--/.Category--> 
 <br>

<!--name-->
<div class="form-group">
    <label class="control-label col-md-1"><b>Project Name </b>&nbsp;&nbsp;</label>
    <div class="col-md-9">
        <input type="text" name="name" placeholder="Project Name" style="width:79%; height:25px;" >
    </div>  
</div>
<!--/.name--> 

<br>

<!--location-->
<div class="form-group">
    <label class="control-label col-md-1"><b>Location </b>&nbsp;&nbsp;</label>
    <div class="col-md-9">
        <input type="text" name="location" placeholder="Location" style="width:79%; height:25px;" >
    </div>  
</div>
<!--/.location-->

<br>

<!--Start Date-->
<div class="form-group">
    <label class="control-label col-md-1"><b>Start Date </b>&nbsp;&nbsp;</label>
    <div class="col-md-9">
        <input type="text" name="str_date" id="str_date" placeholder="Start Date" style="width:79%; height:25px;" >
    </div>  
</div>
<!--/.Start Date--> 

<br>

<!--End Date-->
<div class="form-group">
    <label class="control-label col-md-1"><b>End Date </b>&nbsp;&nbsp;</label>
    <div class="col-md-9">
        <input type="text" name="end_date" id="end_date" placeholder="Start Date" style="width:79%; height:25px;" >
    </div>  
</div>
<!--/.End Date--> 

<br>

<div class="form-group">
    <label class="control-label col-md-5"></label>
    <div class="col-md-3">
        <input type="submit" class="btn btn-success" id="btn btn-success" value="Submit"> 
        <input type="reset" class="btn btn-primary" id="btn btn-primary" value="Reset"> 
    </div>
    <div class="col-md-3">

    </div>
</div>

<?php
echo form_close();
?>
<!--form_close-->

1 个答案:

答案 0 :(得分:0)

请使用以下功能在表单中打印验证错误

<?php echo validation_errors() ?>

OR

<?php echo form_error('name');  //name will be name of your input field ?>
相关问题