通过put接口 修改数据库的信息
用代码块功能插入代码,请勿粘贴截图
后端 路由器
router.put("/v1/updateuser",(req,res)=>{
var $uid=req.body.uid;
var $uname=req.body.uname;
var $upwd=req.body.upwd;
var $email=req.body.email;
var $phone=req.body.phone;
var $avatar=req.body.avatar;
var $user_name=req.body.user_name;
var $gender=req.body.gender;
//console.log($gender);
var sql="update xz_user set uname=?,upwd=?,email=?,phone=?,avatar=?,user_name=?,gender=? where uid=?";
pool.query(sql,[$uname,$upwd,$email,$phone,$avatar,$user_name,$gender,$uid],(err,result)=>{
if(err) throw err;
console.log(result);
res.send("1");
});
})
前端
function updateuser(){
var $uid=uid.value;
var $uname=uname.value;
var $upwd=upwd.value;
var $email=email.value;
var $phone=phone.value;
var $avatar=avatar.value;
var $user_name=user_name.value;
var $gender=" ";
if(male.checked){
$gender="1";
}else{
$gender="0";
}
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var result=xhr.responseText;
if(result=="1"){
alert("修改成功");
}else{
alert("修改失败");
}
}
};
xhr.open("put","/por/v1/updateuser",true);
xhr.setRequestHeader("Content_Type","application/x-www-form-urlencoded");
var formdata=`uid=${$uid}&uname=${$uname}&upwd=${$upwd}&email=${$email}&phone=${$phone}&avatar=${$avatar}&user_name=${$user_name}&gender=${$gender}`;
console.log(formdata);
xhr.send(formdata);
}
修改数据后出现影响数据库行数为零
不知道哪里出错了 数据库信息没有修改成功;