最近学做小程序时,因为主要是用来公司内部使用的,有些信息可能不太适合给外部人员展示,所以需要给用户分类,通过手动一个个更改会比较累,于是就想到是否可以在大家首次登记的时候直接判断,然后进行用户分类,以下是相应的代码(可能会有冗余,Sham作为菜鸟,只有这水平了):
<?php
/**这个用来确认是在经过判断是否为FMC员工之后提交登记信息,被存入不同的用户类别*/
include_once("dbcon.php");
header("Content-Type:text/html;charset=utf-8");
if(isset($_GET['id'])){
$id = $_GET["id"];
$name = $_GET["name"];
$phone = $_GET["phone"];
$openid = $_GET["openid"];
$nickname = $_GET["nickname"];
$useravatar = $_GET["useravatar"];
$loginSql = <<<login
select * from staff where id="{$id}" and name = "{$name}";
login;
$res = mysqli_query($con, $loginSql);
if($row = mysqli_fetch_array($res)){
$usertype=$row['usertype'];
$ctcommitee = $row['ctcommitee'];
$dormitory = $row['dormitory'];
$sql="INSERT INTO user (id,name,phone,openid,nickname,usertype,useravatar,ctcommitee,dormitory) VALUES ('$id','$name','$phone','$openid','$nickname','$usertype','$useravatar','$ctcommitee','$dormitory')";
if($result = $con->query($sql)){
echo "成功";
}else{
echo "失败", $con ->error;
}
}else{
$sqls="INSERT INTO user (id,name,phone,openid,nickname,usertype,useravatar,ctcommitee,dormitory) VALUES ('$id','$name','$phone','$openid','$nickname','游客','$useravatar','','')";
if($result = $con->query($sqls)){
echo "成功";
}else{
echo "失败", $con ->error;
}
}
}else{
echo "非法访问!";
}
?>
这样, 登记时就可以通过连接“staff”数据库,判断是否为FMC员工:
如是,则读取对应的usertype,ctcommitee,dormitory数值,并与其他用户信息一并存入“user”中;
如果不是,则usertype为“游客”,ctcommitee和dormitory为空值.