我正在尝试将数据插入数据库,当我插入数据并按下提交按钮时,文本字段只是空,数据无法插入数据库 从我的代码中看到的一切看起来都不错,但我犯了错误?
后置控制器
<?php
class Posts extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->database();
}
public function index($page='home'){
$data['posts']= $this->Posts_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($mjestoOdredista=NULL){
$data['posts'] = $this->Posts_model->get_posts($mjestoOdredista);
$post_id = $data['posts']['id'];
$data['comments'] = $this->comment_model->get_comments($post_id);
if(empty($data['posts'])){
show_404();
}
$data['id'] =$data['posts'];
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('templates/footer');
}
public function create(){
//check if user is logged in
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] ='Create Posts';
$data['categories'] = $this->Posts_model->get_categories();
if($this->form_validation->run()===FALSE){
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
}else {
//upload image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '500';
$config['max_height'] = '500';
$this->load->libary('upload', $config);
if(!$this->upload->do_upload()){
$error=array('error'=>$this->upload->display_errors());
$post_image='noimage.jpg';
}else{
$data = array('upload_data'=>$this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Posts_model->create_post($post_image);
$this->session->set_flashdata('post_creted', 'You post has been created') ;
redirect('posts');
}
}
public function delete($id){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->delete_post($id);
$this->session->set_flashdata('post_deleted', 'You post has been deleted ') ;
redirect('posts');
}
public function edit($mjestoOdredista){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['mjestoOdredista']= $this->Posts_model->get_posts($mjestoOdredista);
//Check if user is logged in
if($this->session->userdata('user_id') !=$this->Posts_model->get_posts($mjestoOdredista)['user_id'])
redirect('posts');
$data['categories'] = $this->Posts_model->get_categories();
if(empty($data['mjestoOdredista'])){
show_404();
}
$data['id'] = 'Edit Post';
$this->load->view('templates/header');
$this->load->view('posts/edit',$data);
$this->load->view('templates/footer');
}
public function update(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->Posts_model->update_post();
$this->session->set_flashdata('post_updated', 'You post has been updated ') ;
redirect('posts');
}
}
?>
发布模型
<?php
class Posts_Model extends CI_Model{
public function __construct(){
$this->load->database();
}
function get_posts($mjestoOdredista=FALSE){
if($mjestoOdredista === FALSE){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get('posts');
return $query->result_array();
}
$query=$this->db->get_where('posts', array('mjestoOdredista' => $mjestoOdredista));
return $query->row_array();
}
//Kreiranje post
public function create_post($post_image){
$mjestoPolaska = url_title($this->input->post('title'));
$data=array(
'mjestoPolaska' => $mjestoPolaska,
'mjestoOdredista' => $this->input ->post('Mjesto Odredista'),
'datumPolaska' => $this->input ->post('Datum Polaska'),
'datumPovratka' => $this->input ->post('Datum Povratka'),
'brojMjesta' => $this->input ->post('Broj Mjesta'),
'cijena' => $this->input ->post('cijena'),
'opis' => $this->input ->post('Opis'),
'category_id'=>$this->input->post('category_id'),
'user_id' =>$this->session->userdata('user_id'),
'post_image'=>$post_image
);
return $this->db->insert('posts',$data);
}
//Brisanje posta
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
//editovanje posta
public function update_post(){
$mjestoPolaska=url_title($this->input->post('Mjesto Polaska'));
$data=array(
'mjestoPolaska' => $mjestoPolaska,
'mjestoOdredista' => $this->input ->post('Mjesto Odredista'),
'datumPolaska' => $this->input ->post('Datum Polaska'),
'datumPovratka' => $this->input ->post('Datum Povratka'),
'brojMjesta' => $this->input ->post('Broj Mjesta'),
'cijena' => $this->input ->post('cijena'),
'opis' => $this->input ->post('Opis'),
'category_id'=>$this->input->post('category_id'),
'user_id' =>$this->session->userdata('user_id'),
'post_image'=>$post_image
);
$this->db->where('id',$this->input->post('id'));
return $this->db->update('posts',$data);
}
public function get_categories(){
$this->db->order_by('name');
$query = $this->db->get('categories');
return $query->result_array();
}
public function get_posts_by_category($category_id){
$this->db->order_by('posts.id','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query=$this->db->get_where('posts',array('category_id'=> $category_id));
return $query->result_array();
}
}
?>
查看
<h2><?= $title;?></h2>
<!-- jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- Isolated Version of Bootstrap, not needed if your site already uses Bootstrap -->
<link rel="stylesheet" href="https://formden.com/static/cdn/bootstrap-iso.css" />
<!-- Bootstrap Date-Picker Plugin -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
<!-- Include jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- Include Date Range Picker -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
<?php echo form_open_multipart('posts/create');?>
<?php echo validation_errors();?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label>Mjesto Polaska</label>
<input type="text" class="form-control" name="mjesto_polaska" placeholder="Mjesto Polaska">
</div>
<div class="form-group">
<label>Mjesto Odredista</label>
<input type="text" class="form-control" name="mjesto_odredista" placeholder="Mjesto Odredista">
</div>
<div class="container">
<div class="row">
<div class='col-sm-4'>
<div class="form-group">
<label>Datum Polaska</label>
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
</div>
</div>
<div class="container">
<div class="row">
<div class='col-sm-4'>
<div class="form-group">
<label>Datum Povratka</label>
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
</div>
</div>
<div class="form-group">
<label>Cijena</label>
<input type="text" class="form-control" name="cijena" placeholder="Cijena">
</div>
<div class="form-group">
<label for="sel1">Broj slobodnih mjesta:</label>
<select class="form-control" id="sel1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
<div class="form-group">
<label for="Kategorije">Kategorija</label>
<div>
<select name="category_id" class="form_control">
<?php foreach($categories as $category):?>
<option value="<?php echo $category['id'];?>"><?php echo $category['name'];?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label>Postavi sliku:</label>
<p><b>samo oni koji nude prevoz nek postave sliku svojeg vozila</b></p>
<input type="file" name="userfile" size="20">
</div>
<div class="form-group">
<label for="comment">Opis:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-block">Submit</button>
<?php form_close();?>
</div>
</div>
答案 0 :(得分:0)
我看到你输入的输入名称不正确 从
更改模型中的输入名称 $this->input->post('mjesto odredista')
致$this->input->post('mjesto_odredista')
.etc
确保视图中的所有输入=模型中的输入。
答案 1 :(得分:0)
问题出在您的模型和视图中。
在模型中,当您在$this->input->post('Mjesto Odredista')
内调用()
时,您必须提供输入字段的名称。我想在这里你给了placeholder
。
对于 VIEW ,对于某些输入字段,您没有提供name
属性,例如Datum Povratka
,Broj slobodnih mjesta
,Opis
等等。例如,
<div class='col-sm-4'>
<div class="form-group">
<label>Datum Povratka</label>
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>