2014-07-13 39 views
0

我想插入一些數據到MySQL數據庫。但它只接受用戶1插入,它是數據庫表中的第一個用戶。無論用戶號碼如何可以插入所有用戶。codeigniter只插入數據爲用戶1

謝謝你的時間。

這裏是在你的數據庫表中插入我的代碼

 <?php 

     class Mdl_chat extends CI_Model{ 

    function Mdl_chat(){ 

    parent::__construct(); 

    } 

     function add_chat_message($chat_id, $user_id, $chat_text){ 
    $query_str = "INSERT INTO chat_message (chat_id, user_id, chat_text) VALUES(?,?,?)"; 
    $this->db->query($query_str, array($chat_id, $user_id, $chat_text)); 
    } 


     function get_chat_messages($chat_id, $last_chat_message_id = 0){ 

    $query_str = "SELECT 
    cm.chat_message_id, 
    cm.user_id, 
    cm.chat_text, 
    DATE_FORMAT(cm.created_at, '%D of %M %Y at %H:%i:%s')as chat_message_timestamp, 
    u.username, 
    u.profile_image_url 
    from chat_message cm 
    right join user as u on cm.user_id = u.user_id 
    WHERE cm.chat_id = ? 
    and cm.chat_message_id > ? 
    ORDER BY cm.chat_message_id asc"; 
    $result = $this->db->query($query_str, array($chat_id, $last_chat_message_id)); 

    return $result; 

     } 



     } 


    below is my user model which I am using to pass the user id that is in session to the sql 
    query in order to retrieve the right user against the chat message. 

    <?php 

    class Mdl_users extends CI_Model{ 



/* 
*call to model class 
*/ 

function __construct() 
{ 
    parent::__construct(); 
} 


/* 
*get userid from email column 
*/ 
function get_userID($email){ 
$this->session->set_userdata('user'); 
$this->db->where('email',$email); 
$query = $this->db->get('user')->row_array(); // retrieves row in session. 

return $query['user_id']; 

} 


    //get username 
     function get_username($email){ 
     $this->db->where('email',$email); 
     $query = $this->db->get('user')->row_array(); // retrieves only first-row. 
    return $query['username']; 
    } 

     public function create_user() 
    { 
    $new_user_data = array(

     'Email'=> $this->input->post('email'), 
     'Username'=> $this->input->post('username'), 
     'Password'=> $this->input->post('password'), 
    ); 

    $insert = $this->db->insert('user',$new_users_data); 
    return $this->db->insert_id(); 

     } 


    } 

回答

0

儘量讓chat_id作爲主鍵並選中自動增量選項在

+0

是自動增量上也許我應該顯示我的用戶模型 – user3272509