Home >Backend Development >PHP Tutorial >thinkphp failed to modify database data
<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?
<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.
Do not use $_POST
or $_Get
directly to prevent SQL注入
. You should use the I
function.
Debugging sql should use the $obj->getLastsql()
method.
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
Print M()->getLastsql()
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);