在表中插入表单值在codeigniter中不起作用

pgky5nke  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(356)

我正在尝试在表中插入表单值。我不知道它不发布值的原因它给我的错误值不能为空我确信我在表单中提到了正确的表值有人能检查我犯了什么错误吗
这是我的按钮:

<td><form action="<?php echo base_url();?>Dashboard/ReviewManagement" method="POST">
    <input type="hidden" name="topicid" value="<?php echo $proview->id;?>">
    <input type="hidden" name="courseid" value="<?php echo $proview->courseId;?>">
    <button type="submit" class="btn btn-xs btn-warning" >
        <span class="icon-ok"></span>
    </button>
</form></td>

这是我的控制器:

public function ReviewManagement() {

    $username = $this->session->userdata('user_name');
    $data['userslist'] = $this->Dashboard_model->getUsers();

        $insert_data = array(
            'topicid' =>$this->input->post('topicid'),
            'stage' => $this->input->post('stage'),
            'allocatedresource' => $this->input->post('allocatedresource'),
            'status' => 1,
            'createdby' => $username,
            'createdon' => date('d/m/Y'),
            'expec' =>$this->input->post('expec')

        );

        $this->Dashboard_model->reviewmanagerInsert($insert_data);
        $this->load->view('template/header');
            $this->load->view('dashboard/reviewmanagement',$data);
            $this->load->view('template/footer'); 
        }

Here is my model:

    public function reviewmanagerInsert($data)
    {
   $this->db->insert('reviewmanager',$data); 
}

以下是我的观点:

<form action="<?php echo base_url();?>Dashboard/ReviewManagement" method="post">
        <div class="form-row">
            <div class="col-md-3">Reviewer:</div>
            <div class="col-md-9"> 
                <select name="allocatedresource" class="form-control">
                   <option value="0">Select Reviewer</option>
                <?php foreach ($userslist as $users) { ?>
                    <option value="<?php echo $users->id; ?>"><?php echo $users->username; ?></option>
                <?php } ?>
            </select>
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-3">Stage:</div>
            <div class="col-md-9"> 
                <select class="form-control col-md-8" name="stage" required="">
                    <option value="" selected="">Select Stage</option>
                    <option value="1">PB Review</option>
                    <option value="2">PB2 Review</option>
                    <option value="3">Alpha Review</option>
                    <option value="4">Beta Review</option>
                    <option value="5">Gamma Review</option>
                </select>
            </div>
        </div>

        <div class="form-row">
            <div class="col-md-3">Due Date:</div>
            <div class="col-md-9">
                <div class="input-group">
                    <div class="input-group-addon"><span class="icon-calendar-empty"></span></div>
                    <input type="text" name="expec" autocomplete="off" required="" class="datepicker form-control" value=""/>
                </div>
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-8 col-md-offset-2">
                <button type="submit" class="btn btn-primary btn-block">Assign</button>             
            </div>
        </div>
    </form>

出现以下错误:
列“topicid”不能为空

INSERT INTO `reviewmanager` 
            (`topicid`, `stage`, `allocatedresource`, 
            `status`, `createdby`, `createdon`, `expec`) 
    VALUES (NULL, NULL, NULL, 1, 'admin', '16/08/2018', NULL)

谁能帮我一下我犯了什么错误
提前谢谢。

4sup72z8

4sup72z81#

只需在表单中添加以下两个输入。。。

<input type="hidden" name="topicid" id="topic_id">
<input type="hidden" name="courseid" id="course_id">
dhxwm5r4

dhxwm5r42#

只使用一个带有隐藏字段的窗体,并使用jquery-like为这些隐藏字段赋值
您的按钮视图

<td>
    <div id="topicid" value="<?php echo $proview->id;?>"></div>
    <div id="courseid" value="<?php echo $proview->courseId;?>"></div>
    <button type="button" class="btn btn-xs btn-warning" >
        <span class="icon-ok" id="submit"></span>
    </button>
</td>

在主窗体中,在下面添加隐藏字段

<input type="hidden" name="topicid" id="topic_id">
<input type="hidden" name="courseid" id="course_id">

然后在下面添加js来赋值

$(document).ready(function(){
    $("#submit").click(function(){
        var topicid = $("#topicid").val();
        var courseid = $("#courseid").val();
        $("#topic_id").val(topicid);
        $("#course_id").val(courseid);
    });
});

不过,这个答案没有经过检验。

rsaldnfx

rsaldnfx3#

解决方法如下

<td><form action="<?php echo base_url();?>Dashboard/ReviewManagement" method="POST">
                                                    <input type="hidden" name="topicid" value="<?php echo $proview->id;?>">
                                                    <input type="hidden" name="courseid" value="<?php echo $proview->courseId;?>">
                                                    <button type="submit" class="btn btn-xs btn-warning" >
                                                        <span class="icon-ok"></span>
                                                    </button>
                                                </form></td>

从下面改成了第二个

<form action="<?php echo base_url();?>Dashboard/ReviewManagementInsert" method="post">
                <div class="form-row">
                    <div class="col-md-3">Reviewer:</div>
                    <div class="col-md-9"> 
                        <select name="allocatedresource" class="form-control">
                           <option value="0">Select Reviewer</option>
                        <?php foreach ($userslist as $users) { ?>
                            <option value="<?php echo $users->username; ?>"><?php echo $users->username; ?></option>
                        <?php } ?>
                    </select>
                    </div>
                </div>

控制器代码1:

public function ReviewManagement() {

    if ($this->session->userdata('is_logged')) {
        if (!empty($this->input->post('topicid'))) {
            $id = $this->input->post('topicid');
            $data['userslist'] = $this->Dashboard_model->getUsers();
            $data['id'] = $this->input->post('topicid');
            $this->session->set_userdata('topicid', $data['id']);
            $this->load->view('template/header');
            $this->load->view('dashboard/reviewmanagement', $data);
            $this->load->view('template/footer');
        } elseif (!empty($this->session->userdata('topicid'))) {
            $id = $this->session->userdata('topicid');
            $data['userslist'] = $this->Dashboard_model->getUsers();
            $data['id'] = $this->session->userdata('gettopicdetails');
            $this->load->view('template/header');
            $this->load->view('dashboard/reviewmanagement', $data);
            $this->load->view('template/footer');
        } else {
            echo 'im else';
            die;
        }
    } else {
        $this->session->set_flashdata('error', 'Topic Id not captured.');
        redirect('Dashboard');
    }
}

控制器代码2

public function ReviewManagementInsert() {
    $topicid = $this->session->userdata('topicid');
    $username = $this->session->userdata('user_name');

    $insert_data = array(
        'topicid' => $topicid,
        'stage' => $this->input->post('stage'),
        'allocatedresource' => $this->input->post('allocatedresource'),
        'status' => 1,
        'createdby' => $username,
        'createdon' => date('d/m/Y'),
        'expec' => $this->input->post('expec')
    );

    $this->Dashboard_model->reviewmanagerInsert($insert_data);
    $this->load->view('template/header');
    $this->load->view('dashboard/reviewmanagement');
    $this->load->view('template/footer');
}

相关问题