我在尝试发帖评论时遇到问题,当我输入(姓名,电子邮件和评论正文)并按“提交”按钮时,我的帖子已被自动删除。我试图找到一个错误,但无法看到我在哪里犯了错误
后置控制器
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT-0700"));
发布模型
<?php
class Posts extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->database();
$this->load->model('Posts_model');
}
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>Info o voznji:</h2>
<div class></div>
<div class="post-body">
<hr>
<?php echo form_open('/posts/delete/'.$posts['id']);?>
<?php if($this->session->userdata('user_id')==$posts['user_id']);?>
<a class="btn btn-default " href="posts/edit/">Edit</a>
<input type="submit" value="Delete" class="btn btn-danger">
</div>
<hr>
<h3>Comments</h3>
<?php if($comments):?>
<?php foreach ($comments as $comment):?>
<div class="well">
<h5><?php echo $comment['body']; ?>[by <strong><?php echo $comment['name']?></strong>]</h5>
</div>
<?php endforeach;?>
<?php else : ?>
<p>No Comments to display</p>
<?php endif;?>
<hr>
<h3>Add Comment</h3>
<?php echo validation_errors();?>
<?php echo form_open('comments/create/'.$posts['id']); ?>
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control">
<div class="form-group">
<label>Email</label>
<input type="text" name="name" class="form-control">
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control"></textarea>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
</div>
答案 0 :(得分:1)
让我们来看一下这个观点:
<div class="post-body">
<hr>
<?php echo form_open('/posts/delete/'.$posts['id']);?>
<?php if($this->session->userdata('user_id')==$posts['user_id']);?>
<a class="btn btn-default " href="posts/edit/">Edit</a>
<input type="submit" value="Delete" class="btn btn-danger">
</div>
以下是一些问题:
</form>
或form_close()
来电(并且在评论创建表单之后)。从本质上讲,它归结为无效的HTML;按“提交”按钮将提交第一个表单(因为它从未关闭)。以下代码解决了这些问题:
<div class="post-body">
<hr>
<?php if ($this->session->userdata('user_id') == $posts['user_id']): ?>
<?= form_open('/posts/delete/' . $posts['id']) ?>
<a class="btn btn-default" href="/posts/edit/<?= $posts['id'] ?>">Edit</a>
<input type="submit" value="Delete" class="btn btn-danger">
<?= form_close() ?>
<?php endif ?>
</div>