2016-12-25 39 views
1

我是新來的CodeIgniter和我建立一個簡單的I/O網站。我只有一個數據庫,說test只有一個表results,看起來像這樣:插入一行使用不同的型號/頁面 - CodeIgniter

截圖表的「結果」

enter image description here

我有兩個觀點personal.phpsongs.php。在第一個中,我收集要插入到字段2,3和4中的數據值,其餘值將在第二個視圖中收集。然後將它們通過相關型號personal_modelsongs_model插入表格中。 現在很明顯,他們將被插入到兩個不同的行,這是不是我想要的。這裏有什麼竅門?我應該如何管理它?到目前爲止,我已經想過得到最後一個ID,但我不知道該怎麼做。提前致謝!

personal.php(第一視圖)

<?php echo validation_errors(); ?> 
<?php echo form_open('data/personal'); ?> //passes the data to the controller that loads the personal_model.php 
"some input fields" 
<button type="submit" name="submit">Submit Data</button> 

songs.php(第二視圖)

<?php echo validation_errors(); ?> 
<?php echo form_open('data/songs'); ?> //passes the data to the controller that loads the songs_model.php 
"some input fields" 
<button type="submit" name="submit">Submit Rating</button> 

personal_model.php(第一模型)

<?php 
class Personal_model extends CI_Model { 
    public function __construct() 
    { 
     $this->load->database(); 
    } 

    public function insert_personal() 
    { 
     $this->load->helper('url'); 

     $data = array(
      'age' => $this->input->post('user_age'), 
      'education' => $this->input->post('user_edu'), 
      'twitter' => $this->input->post('user_twitter'), 
      'facebook' => $this->input->post('user_facebook') 
     ); 

     return $this->db->insert('results', $data); 
    } 
} 

songs_model.php(第二模型)

<?php 
class Ratings_model extends CI_Model { 
    public function __construct() 
    { 
     $this->load->database(); 
    } 

    public function insert_ratings() 
    { 
     $this->load->helper('url'); 
     #$this->load->database(); 
     $data = array(
      'score' => $this->input->post('rating'), 
      'song1' => $this->input->post('rnd1'), 
      'song2' => $this->input->post('rnd2') 
     ); 

     return $this->db->insert('results', $data);  
    } 
} 
+0

這是一些像親子一樣的事情。這意味着根據你的問題'personal.php'是父母,'songs.php'是孩子。因此,插入第一個父數據並保存,然後使用父代的ID加載它。希望它的幫助 –

+0

你不需要有兩個模型,因爲你在這兩個文件中做同樣的事情(插入行)。你最好將數據從你的控制器傳遞給你的模型,在你的模型中有'public function insert_data($ data)'並且在你的控制器中構建你的數組,而不是在你的模型中。 – Dan

+0

@AbdullaNilam非常感謝!它給了我一些見解。 –

回答

1

您的控制器功能應該是這樣的。

public function personal() 
    { 
     $lastInsertedID = $this->Personal_model->insert_personal(); 
     $this->session->set_userdata("personalID",$lastInsertedID); 
    } 

將上次插入的ID設置到上述控制器函數中的session中,該函數應該從您的Personal_model中返回。這是代碼。

public function insert_personal() 
{ 
    $this->load->helper('url'); 

    $data = array(
     'age' => $this->input->post('user_age'), 
     'education' => $this->input->post('user_edu'), 
     'twitter' => $this->input->post('user_twitter'), 
     'facebook' => $this->input->post('user_facebook') 
    ); 

    $this->db->insert('results', $data); 
    return $this->db->insert_id(); 
} 

然後更新您的insert_ratings函數中的現有行而不是插入記錄。這是代碼。

public function insert_ratings() 
    { 
     $data = array(
      'score' => $this->input->post('rating'), 
      'song1' => $this->input->post('rnd1'), 
      'song2' => $this->input->post('rnd2') 
     ); 
     $personalID = $this->session->userdata("personalID"); 
     $this->db->where("id",$personalID); 
     $this->db->update('results', $data); 
     return $this->db->affected_rows();  
    } 

,則任何新記錄將插入到表中,同時提交您的song.php形成,將更新現有的一個。

+0

非常感謝!所以基本思想是我必須跟蹤用戶的唯一值(ID或IP),以便我可以更新行而不是插入新行。現在我已經熟悉了CI中的會話課程。我想我可以讓它工作。如果我找到問題,我會再次在這裏發表評論! –

+0

太棒了!是的,你可以隨時發表評論。如果這對你有幫助,不要忘記接受這個答案。乾杯! –

+0

它當然有幫助!聖誕快樂,再次感謝! –

相關問題