Home >Backend Development >PHP Tutorial >thinkphp failed to modify database data

thinkphp failed to modify database data

WBOY
WBOYOriginal
2016-07-06 13:53:412401browse

<code>    
    public function update(){
        $room=M('room');
//         $room_id=$_GET['room_id'];
        $room_id=$_POST['room_id'];
       
        $roominfo['room_id']=$_POST['room_id'];
        $roominfo['room_type']=$_POST['room_type'];
        $roominfo['room_status']=$_POST['room_status'];
        $roominfo['room_area']=$_POST['room_area'];
        $roominfo['room_cost']=$_POST['room_cost'];
        $roominfo['description']=$_POST['description'];
         

        print_r($roominfo);
//         $room->where("room_id=$room_id")->save($roominfo);
        
         
         echo "<script>alert('修改成功')</script>";
//          $this->redirect("showRoom");
  
    }
</code>

My modification function code is as above, it prints the value of the $room_info object, and it is indeed modified, but why does the database data not change after execution? May I ask why?

Reply content:

<code>    
    public function update(){
        $room=M('room');
//         $room_id=$_GET['room_id'];
        $room_id=$_POST['room_id'];
       
        $roominfo['room_id']=$_POST['room_id'];
        $roominfo['room_type']=$_POST['room_type'];
        $roominfo['room_status']=$_POST['room_status'];
        $roominfo['room_area']=$_POST['room_area'];
        $roominfo['room_cost']=$_POST['room_cost'];
        $roominfo['description']=$_POST['description'];
         

        print_r($roominfo);
//         $room->where("room_id=$room_id")->save($roominfo);
        
         
         echo "<script>alert('修改成功')</script>";
//          $this->redirect("showRoom");
  
    }
</code>

My modification function code is as above, it prints the value of the $room_info object, and it is indeed modified, but why does the database data not change after execution? May I ask why?

// $room->where("room_id=$room_id")->save($roominfo);

Should the comment symbol in front of it be removed? . .

In addition: "room_id=$room_id", the author can inject sql from Baidu.

M()->getLastsql()

Looks like it should be thinkphp. Let me give you some comments.

  1. Do not use $_POST or $_Get directly to prevent SQL注入. You should use the I function.

  2. Debugging sql should use the $obj->getLastsql() method.

  3. The output object applies its own dump function.

If you are sure that there is no problem with the code, it may also be a problem with your database. For example, your account does not have write permissions, you are reading the database and cannot perform update operations, etc. Let’s check what the problem is first

  1. Print M()->getLastsql()

  2. if possible
  3. If your room_id is the primary key of the table, you can use the following code

    <code>$roominfo['room_id']=$_POST['room_id'];
    $roominfo['room_type']=$_POST['room_type'];
    $roominfo['room_status']=$_POST['room_status'];
    $roominfo['room_area']=$_POST['room_area'];
    $roominfo['room_cost']=$_POST['room_cost'];
    $roominfo['description']=$_POST['description'];
    $room->save($roominfo);</code>

$room->where("room_id=".$room_id)->save($roominfo);

Let’s try letting go of your gaze

Add this sentence below $room_id: $roominfo = $room->getByID($room_id);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn