创建我自己的应用程序以学习CodeIgniter,通过遵循CI文档,我无法将数据发布到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);
}
我也在autoload(libraries-> form_validation)和(helper-> form)中添加了。我已经在这些代码之前测试了所有内容,并且工作正常。问题是HTML中的那些数据我想将它们发送到数据库中,但即使验证器在空白表单上工作(提交没有内容)或插入内容,它只是在实现页面,因为它应该将其重定向到帖子URL。有人可以帮帮我吗?
你的帖子方法在哪里?
例如:
<form method="post" name="from_submit" action="">
你忘了添加表格
<form method="post" name="myform" action="">
//Your form code---
</form>