Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

实验四 B/S 体系结构风格-参考答案(PHP 版)

//用户表示层
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="loginaction.php" method="post">
<table>

<tr>
<td width="60px">用户名:</td>
<td><input name="name" type='text' size="15" style="width:150px;border:1px
solid #ccc;"></td>
</tr>

<tr>
<td >密码:</td>
<td><input name="psw" type='password' size="15"
style="width:150px;border:1px solid #ccc;""></td>
</tr>

<tr>
<td colspan="2" align='center'><input name="s" type=submit value='登
陆'style="width:80px;border:0;height:25px;"></td>
</tr>
</table>
</form>
</body>
</html>

//业务逻辑层
<?php
if(isset($_POST['s'])){
//
$name=$_POST['name'];
$psw=$_POST['psw'];
if($name==''||$psw==''){
echo "<script>alert('请输入用户名或密码!');
history.go(-1);</script>";//返回上一个页面
}else{
queryDatabase($name,$psw);
}

}?>

//数据访问层
<?php
function queryDatabase($name,$psw){
$conn = new mysqli('localhost', 'root', '12345678', 'test');
if ($conn -> connect_errno) {
printf("Connect failed: %s\n", $conn->connect_error);
exit();
}
$sql = "select * from userInfo where name='$name'";
$query = $conn->query($sql);
if($row = $query->fetch_array()){
if($row['password']==$psw){
echo "<script>alert('用户登陆成功');</script>";}
else{
echo "<script>alert('用户密码不正确');
history.go(-1);</script>";
}
}else{
echo "<script>alert('不存在该用户,请注册或用其他账号登
陆');history.go(-1);</script>";
}
mysqli_close($conn);
}

?>

结果:
//数据库

You might also like