Before we introduce the text, we introduce the consistent operation methods supported by the data Update method are:
In the previous article we implemented the data deletion and the bulk deletion, this article we will implement the data update.
The first is still the expected effect chart:
Click Modify to enter the Modi.html page , and then modify it so that the real name of the property is modified:
Click to Save:
The above is the effect to implement, the following is the implementation of the specific code:
The first is still the code in the view:
<form role= "form" method= "POST" action= "__module__/admin/user/doadd" > <div class= "Input-group" > <span class= "Input-group-addon" > Households Name:</span> <input type=" text "class=" Form-control "placeholder=" "name=" username "> </div> <div class=" Input-group "> <span class=" Input-group-addon "for=" inputWarning1 "> real name:</span> <input type=" text "class=" Form-control "placeholder = "" id= "input" name= "Realname" > </div> <div class= "Input-group" > <span class= "Input-group-addon"
> Mobile number:</span> <input type= "text" class= "Form-control" placeholder= "" Name= "Telphone" > </div> <div class= "Input-group" > <span class= "Input-group-addon" > E-Mailbox:</span> <input type= "Text" class = "Form-control" placeholder= "" name= "email" > </div> <div class= "Input-grou"P "> <span class=" Input-group-addon "> Add time:</span> <input type=" text "class=" Form-control " Placeholder= "2014-05-22" name= "Resgistertime" > </div> <div class= "Input-group" > <span class= " Input-group-addon "> Set password:</span> <input type=" text "class=" Form-control "placeholder=" 123456 "Name="
Password "> </div> <div class=" Input-group "> <span class=" input-group-addon "> Confirm password:</span> <input type= "text" class= "Form-control" placeholder= "123456" name= "Repassword" > </div> <div class= " Input-group "> <button type=" Submit "class=" Btn btn-primary "> guaranteed Save </button> </div> </form>
Next is the controller: it needs to be explained that there is a need to write an unmodified value here, primarily to make it easier for users to remember, and therefore need to be judged by an if branch:
Public Function Modi () {
if (is_post) {
$adminUsersModel = D ("Adminusers");
$adminUsersModel->create ();
Var_dump ($adminUsersModel);
Exit ("Create success!") ");
if ($adminUsersModel->save ()) {
$this->success ("modified successfully", U ("admin/user/lists"));
}
else {
$this->error ($adminUsersModel->geterror ());
}
}
else {
$id = isset ($_get[' id ')]? Intval ($_get[' id ')): ';
if ($id = = ") {
exit (" Bad param! Please enter ID ");
}
$adminUsersModel =d ("Adminusers");
$adminUsers = $adminUsersModel->find ($id);
Var_dump ($adminUsers);
$this->assign ("Adminusers", $adminUsers);
$this->display ();
}
Here are five ways to update the database under thinkphp
The first method:
$ model->where (' id=1 ')->save ($data);
The second method:
$ model->where (' id=1 ')->data ($data)->save ();
The third method:
$ model->create ();
$ model->save ();
The form must contain a hidden field with the primary key name
The fourth method:
$ model->where (' id=5 ')->setfield (' name ', ' thinkphp ');
$ model->where (' id=5 ')->setfield (Array (' name ', ' email '), array (' TP ', ' TP@163.com '));
The fourth method, pass the array time, I can not achieve ...
The Fifth method:
$ model->setinc (' score ', ' id=5 ', 3); Integral plus 3
$ model->setinc (' score ', ' id=5 '); Integral plus 1
$ model->setdec (' score ', ' id=5 ', 5); Points minus 5
$ model->setdec (' score ', ' id=5 '); Points minus 1