通过遵循CI文档,创建自己的应用程序以学习CodeIgniter,我无法将数据发布到db中。
我正在使用create.php:
<p class="h4 mb-4 text-center"><?= $title ?></p>
<label>Username</label>
<input type="text" id="textInput" class="form-control mb-4" placeholder="Your username" name="username">
<label for="textInput">Title</label>
<input class="form-control mb-4" placeholder="Your title" name="title">
<label>Your message</label>
<textarea class="form-control mb-4" placeholder="Your message content" name="body"></textarea>
<div class="row">
<div class="col-8">
<select class="browser-default custom-select mb-4" id="select" name="genre">
<option selected="">Choose your Genre</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
<div class="col-2">
<input class="form-control mb-4" placeholder="Your age" name="age">
</div>
</div>
<select class="browser-default custom-select mb-4" name="platform">
<option value="" disabled="" selected="">Choose your Platform</option>
<option value="1">Snapchat</option>
<option value="2">Kik</option>
<option value="3">Instagram</option>
<option value="4">Telegram</option>
</select>
<button class="btn btn-info my-4 btn-block" type="submit">Sign up</button>
<div class="text-center">
<p>By clicking
<em>Sign up</em> you agree to our
<a href="<?php echo base_url('tou'); ?>" target="_blank">terms of use</a>.
</p>
</div>
这是控制器:
public function create(){
$data['title'] = 'Add your Username';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('genre', 'Genre', 'required');
$this->form_validation->set_rules('platform', 'Platform', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
$this->post_model->create_post();
redirect('posts');
}
}
这是模型:
public function create_post(){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'username' => $this->input->post('username'),
'genre' => $this->input->post('genre'),
'age' => $this->input->post('age'),
'platform' => $this->input->post('platform'),
);
return $this->db->insert('posts', $data);
}
我还添加了自动加载功能(库-> form_validation)和(帮助程序->表单)。 在这些代码之前,我已经测试了所有内容,并且一切正常。问题是HTML中的那些数据我想将它们发送到db,但是即使验证器也可以在空白表单(不带内容的提交)或插入内容的情况下工作,它只是重新分配页面,因为它应该将其重定向到发布url。 有人可以帮助我吗?
答案 0 :(得分:0)
您的发布方法在哪里?
Ex:
<form method="post" name="from_submit" action="">
答案 1 :(得分:0)
您忘记添加表格
<form method="post" name="myform" action="">
//Your form code---
</form>