The username can be saved, but the password cannot be saved. . . I just learned to write like a cat and a tiger, and I don’t know where I went wrong. .
First of all: $("#psw").val() can get the value.
js code:
function regist(){
$.ajax({
url:"/h51702/cui/shuai/regist4ajax",
type:"get",
data:{
username:$("#username").val(),
psw:$("#psw").val(),
},
success:function(res){
alert(res);
if(res=="成功"){
location.href = "/h51702/cui/shuai/login";
}else{
//alert(res.msg);
}
}
})
}else{
alert("信息输入有误,请重新填写")
}
}
php code:
public function regist4ajax($username="",$psw=""){
$userItem = Db::query('select * from users where username=?',[$username]);
//$userItem = db("users")->where('username',$username)->find();
if(empty($userItem)){
$result = Db::execute('insert into users(username,psw) values (?,?)',[$username,$psw]);
if($result){
return "成功";
}
return"失败";
}else{
return "用户名已经注册";
}
}
In the database:
id int(11) AI PK
username varchar(45)
psw varchar(45)
creat_date datetime
There are many things that can go wrong, including your form and database structure.
Let’s talk about the basic error locating method. There are several steps in the entire registration:
Get data in the page》Ajax transfer to the execution page》Execution page requests database changes》Database storage
Take your data step by step to confirm, for example, whether $("#psw").val() really obtains the password information, and then whether the ajax receiving page really receives this data, and then executes the code What kind of query was executed, and whether this query can be successfully executed directly in the database. By following this step by step and finding which step you are stuck on, it will be easier to find out where the problem lies.
There may be errors in the following places, please check the specific functions yourself.
$("#psw").val()
Whether the value of the password input box is really obtainedSince you said that the username can be saved, I think you should have obtained the data in the background. As for why the password is not saved, please check whether the password access field in your database is
psw
. This should be consistent with'insert into users(username, psw) values (?, ?)'
Thepsw
here correspondsIn addition, check whether the
psw
field in your database is a string type such asVARCHAR
,CHAR
orTEXT
type.Why do we need to use the get method to submit the username and password?
Use post instead, and then see if post receives the data.