我正在尝试将一些数据插入MySQL数据库。但它只接受用户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 :(得分:0)
尝试将chat_id作为主键并检查自动增量选项On