2011-07-23 13 views
0

私はcodeigniterを使用していますが、UPDATEクエリを実行しようとしていますが、INSERTクエリのように新しい行を作成し続けます。私はうんざりしていて、ウェブを検索しても私にはあまり得意ではありませんでした。 (私はn00bのビットです)。どんな助けでも大歓迎です。ここで更新クエリで新しい行が作成されるのはなぜですか?

は形式です:ここでは

<form action="<?=base_url();?>blog/new_post" method="post" name="write_new"> 
    <input type="hidden" name="user" value="<?=$user?>"> 
    <label>Title:</label><br> 
    <input type="text" name="title" value="<?php if(isset($query->title)) { echo $query->title; }?>" size="50"><br> 
    <label>Date:</label><br> 
    <input type="text" name="date" size="46" value="<?php if(isset($query->date)) { echo $query->date; }?>" /> 
    <script language="JavaScript"> 
    new tcal ({ 
     // javascript calender thingy 
     // form name 
     'formname': 'write_new', 
     // input name 
     'controlname': 'date' 
    }); 
    </script> <br><br> 
    <label>Article:</label><br> 
    <textarea name="content" style="width:100%;height:300px;"><?php if(isset($query->article)) { echo $query->article; }?></textarea><br> 
    <select name="category"> 
     <option value="news">Dreamsy News</option> 
     <option value="web">From the Interwebs</option> 
    </select> 
    <input type="submit"/> 
</form> 
<div class="clearboth"></div> 

がモデルだ:

function edit_post($title, $date, $author, $article, $category, $id) 
    { 
     $this->load->helper('form'); //loads the CI form helper 
     $this->load->library('form_validation'); //loads the form validation class 
     $this->form_validation->set_rules('title', 'title', 'required|min_length[3]|max_length[40]|xss_clean'); 
     $this->form_validation->set_rules('date', 'date', 'required|min_length[5]|max_length[15]|xss_clean'); 
     $this->form_validation->set_rules('category', 'category', 'required|xss_clean'); 
     $this->form_validation->set_rules('content', 'article', 'required|xss_clean'); 
     $this->form_validation->set_rules('user', 'you are not logged in', 'required'); 

     if ($this->form_validation->run() == FALSE) //If the form does not validate 
     {  
      $this->load->vars(array('title' => 'Error')); 
      $this->template->write_view('content', 'error/new_post_fail'); 
      $this->template->render(); 
     } 
     else 
     { 

      $article = htmlentities($article, ENT_QUOTES);   

      /*$data = array(
       'title' => $title, 
       'date' => $date, 
       'author' => $author, 
       'article' => $article, 
       'category' => $category 
      );*/ 

      //$this->db->where('id', $id); 
      //$this->db->update('blog', $data); 

      $query = 'UPDATE blog SET title = '.$title.' date = '.$date.', author = '.$author.', article = '.$article.', category = '.$category; 

      $this->db->query($query); 

      redirect(base_url().'blog'); 
     } 

そして、ここでは、コントローラの:

public function write() 
{ 
    $id = $this->uri->segment(3); 
    $query = 'SELECT * FROM blog WHERE id = '.$id; 
    $query = $this->db->query($query); 
    $query = $query->row(); 
    $title = $query->title; 
    $user = $this->session->userdata('user_id'); 
    if(isset($id)){ $title = 'Edit: '.$title; } else { $title = 'Write new post'; } 

    $vars['title'] = $title; 
    $vars['page'] = 'blog'; 
    $vars['user'] = $user; 
    $vars['id'] = $id; 
    $vars['query'] = $query; 

    $this->load->helper('form'); //loads the CI form helper 
    $this->load->library('form_validation'); //loads the form validation class 
    $this->load->vars($vars); 
    $this->template->write_view('content', 'blog/write'); 
    $this->template->render(); 
} 

答えて

2
<form action="<?=base_url();?>blog/new_post" method="post" name="write_new"> 

..コードのその部分だけがあなたの問題のために実行されていることを確認してください。それ以外の場合は、new_postメソッドが呼び出されます。これまでのところ、POSTデータを挿入するだけです。

+0

それは問題でした。私は新しい/編集古いポストページ(3番目のURIセグメントがあるかどうかによって決まる)を書くか、if/elseを追加するのを忘れました。助けてくれてありがとう! –

0

私はWHERE条件があなたの更新クエリではありませんだと思いますこれは問題の問題の1つになる可能性があります。

は、あなたがあなたのblogコントローラのeditメソッドを呼び出すためにaction属性を変更する必要があり

+2

WHERE条件が指定されていない場合、更新はすべてのレコードに適用されます。 –

1

あなたのフォームのアクションタグ/ attrを参照している上記のコントローラの関連機能が表示されなかったので、いくつかのルートを設定しましたか?あなたがその機能を含んでいなければ、エラーが発生したゲストには難しいです。

私から注目されているのは、フォームの検証、テンプレート、リダイレクト機能をモデルに入れておきます。

関連する問題