我试图在codeigniter中写一个UPDATE查询,但由于某种原因,它正在插入一个具有默认值的新记录

I am trying to update an existing message. A message has a message_id, category, subject and the message. Upon clicking on edit, an AJAX call is made ...

Here's my AJAX call from .js file (VIEW)

if(checkEditMessage()){  // checkEditMessage is to check for validation
            console.log("EDIT");
            var cat_id = $("#cat_id").val();
            var commentsubject = $("#commentsubject").val();
            var chat_post_message = $("#chat_post_message").val();
            $.ajax({
                 url: base_url + "chat/editMessage",
                 async: false,
                 type: "POST",
                 data: { 
                    "v_id" : id,
                    "v_cat_id" : cat_id,
                    "v_commentsubject" : commentsubject,
                    "v_chat_post_message" : chat_post_message
                },
                error: function(err){
                    console.log(err);
                },
                 success: function (result) {
                     console.log(result);
                 }
             });
        }

Here's my chat.php (CONTROLLER)

function editMessage(){
    $posted_data = $this->input->post();
    if(isset($posted_data) && !empty($posted_data))
    {
        $id = $posted_data['v_id'];
        $cat_id = $posted_data['v_cat_id'];
        $commentsubject = $posted_data['v_commentsubject'];
        $chat_post_message = $posted_data['v_chat_post_message'];
        $data = $this->chat->updateMessage($id, $cat_id, $commentsubject, $chat_post_message);
        echo $data;
    }
}

Here's my (MODEL)

function updateMessage($id, $cat_id, $commentsubject, $chat_post_message){
    $data=array('cat_id'=>$cat_id,'subject'=>$commentsubject,'message'=>$chat_post_message);
    $this->db->where('message_id',$id);
    $this->db->update('chat_message',$data);
    $err = $this->db->_error_message();
    if(empty($err))
    {
        return "EDIT COMPLETE";
    }
    return false;       
}

Your question is not totally clear, because I don't see any insert, and in order to insert it's really needed the insert clause in your query. I can only tell you some improvements you may want to do.

Which version are you using of codeigniter? Because I don't see you using the word _model which is necessarily.

$message['v_id']                = $posted_data['v_id'];
$message['v_cat_id']            = $posted_data['v_cat_id'];
$message['v_commentsubject']    = $posted_data['v_commentsubject'];
$message['v_chat_post_message'] = $posted_data['v_chat_post_message'];
// `chat_model` and not `chat`
$result = $this->chat_model->updateMessage($message['v_id'], $message);

On the other hand, all you need to do in your model function is

public function updateMessage($id, $data)
{
   $this->security->xss_clean($data);
   $this->db->where('message_id', $id)
            ->update('chat_message', $data);

   return empty($this->db->_error_message());
}

In you ajax change this

  url: "<?php echo base_url() ?>chat/editMessage",
  async: false,// remove this
  data: { 
       "v_id : id,
       v_cat_id : cat_id,
       v_commentsubject : commentsubject,
       v_chat_post_message"} ,

and your model and controller looks ok

You can try the following:

  • Make sure your message_id is getting posted properly. i.e. echo $posted_data['v_id']; in editMessage() to see if the correct value is being received.

  • Make sure the id you are trying to update exists, and that message_id in your database is the PRIMARY KEY field.