[英]CodeIgniter 3: form is not submitting
我是Codeigniter的新手,我已經嘗試了所有方法,但是我的表單仍未提交到數據庫。
這是我的代碼。 我嚴重找不到我哪里出了問題。
視圖
<?= form_open('forms/submit_shifter_form');?>
<div id="form-interview-fill-mainform" class="form-group">
<div class="container">
<div class="col-sm-12">
<div class="input-group">
<label>Reason/s for shifting:</label>
<input type="text" class="form-control form-input" name="shifter_reason">
</div>
<div class="col-sm-6">
<div class="input-group">
<label>Strengths:</label>
<input type="text" class="form-control form-input" name="shifter_strength">
</div>
</div>
<div class="col-sm-6">
<div class="input-group">
<label>Weaknesses:</label>
<input type="text" class="form-control form-input" name="shifter_weakness">
</div>
</div>
</div>
</div>
<?php echo form_submit('submit','Confirm', 'class="btn btn-success btn-lg" id="submit"');?>
<?php echo form_close();?>
CONTROLLER
<?php
defined('BASEPATH') or exit('No direct script access allowed ');
class Forms extends CI_Controller {
public function session(){
if($this->session->userdata('logged_in')){
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['id'];
$data['username'] = $session_data['username'];
$data['password'] = $session_data['password'];
return $data;
}
}
public function submit_shifter_form(){
$shifter = array(
'course' => $this->input->post('shifter_course'),
'reason' => $this->input->post('shifter_reason'),
'strength' => $this->input->post('shifter_strength'),
'weakness' => $this->input->post('shifter_weakness'),
'futureContribution' => $this->input->post('shifter_contribution')
);
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$this->load->model('forms_model');
$this->forms_model->shifter_form_submit($id,$shifter);
redirect(site_url('profile'), 'refresh');
}
}
模型
<?php
defined('BASEPATH') or exit('No direct script access allowed ');
class Forms_Model extends CI_Model{
function shifter_form_submit($id,$shifter)
{
$this->db->insert('tbl_prototype_shifter',$shifter);
$insert_id = $this->db->insert_id();
$shift = array(
'studentId' => $id
);
$this->db->where('id', $insert_id);
$this->db->update('tbl_prototype_shifter', $shift);
}
}
我的其他表單在我的數據庫中正確提交了數據,在添加此新表單之后,我似乎不再正確提交數據。 拜托,有人可以幫忙,我不知道我哪里出了問題
你得到了
$shifter = array(
'course' => $this->input->post('shifter_course'),
'reason' => $this->input->post('shifter_reason'),
'strength' => $this->input->post('shifter_strength'),
'weakness' => $this->input->post('shifter_weakness'),
'futureContribution' => $this->input->post('shifter_contribution')
);
但是你的表格有
shifter_reason
shifter_strength
shifter_weakness
因此,其余所有字段將為null,您的表是否為其余字段接受null?
如果否,則設置一些默認值,
在控制器中
確保已加載數據庫:
function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('forms_model');
$this->load->helper('form');
}
然后用另一種方法
$session_data = $this->session->userdata('logged_in');
$this->forms_model->shifter_form_submit($session_data['id'],$shifter);
在模型中
function shifter_form_submit($id,$shifter)
{
// try to insert
$this->db->insert('tbl_prototype_shifter',$shifter);
// check if insert was ok
if($this->db->affected_rows() > 0)
{
// if ok get last insert id
$insert_id = $this->db->insert_id();
// data to be updated
$shift = array(
'studentId' => $id
);
// update
$this->db->where('id', $insert_id);
$this->db->update('tbl_prototype_shifter', $shift);
}else{
// failed to insert, so cannot update other table too
echo $this->db->_error_message();
throw new Exception("Can't insert the resource");
}
}
嘗試安裝您的提交按鈕。
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.