所用工具phpstudy_pro
下载地址:https://www.xp.cn/
步骤:
启动apache和数据库。
打开网站根目录。
![](https://i-blog.csdnimg.cn/blog_migrate/5a98f5e505bed488b18b4ad53646076a.png)
在管理--打开网站根目录
然后创建一个index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>请登录</title>
</head>
<body>
<form action="login.php" method="POST">
用户名:<input type="text" name="username" placeholder="请输入用户名" required> <br />
密码:<input type="password" name="password"> <br />
<input type="submit" value="登录" >
</form>
<form action="zhuce.html">
<input type="submit" value="注册">
</form>
</body>
</html>
再创建一个login.php文件,代码如下。
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysqli_connect('localhost','root','root','test');
if (mysqli_connect_errno($conn)) {
echo "连接失败";
}
$sql = "select * from users where username = '$username' and password = '$password'";
$res = mysqli_query($conn,$sql);
$arr = mysqli_fetch_array($res);
$sel = "select username from users where username = '$username'";
$res2 = mysqli_query($conn,$sel);
$arr2 = mysqli_fetch_assoc($res2);
if ($arr['username'] == $username) {
echo "登录成功!";
}
elseif ($arr2['username'] != $username) {
echo "用户名不存在,请进行注册";
}
else{
echo "密码错误";
}
?>
接下来是注册界面
创建一个zhuce.html文件,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>注册界面</title>
</head>
<body>
<form action="zhuce.php" method="POST" >
请输入用户名:<input type="text" name="username"><br />
请输入密码:<input type="password" name="password1"><br />
再次输入密码:<input type="password" name="password2"><br />
<input type="submit" value="注册">
</form>
</body>
</html>
创建zhuce.php文件,代码如下:
<?php
$username = $_POST['username'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
if ($password2 != $password1) {
echo "密码不一致,请重新输入!";
}
else {
$conn = mysqli_connect('localhost','root','root','test');
$sel = "select username from users where username = '$username'";
$sql = "insert into users value ('$username','$password1')";
$res = mysqli_query($conn,$sel);
$arr = mysqli_fetch_array($res);
if ($arr['username'] == $username) {
echo "注册失败,该用户已经存在";
}
else {
mysqli_query($conn,$sql);
$res = mysqli_query($conn,$sel);
$arr = mysqli_fetch_array($res);
if ($arr['username'] == $username) {
echo "注册成功!";
}
}
}
?>
然后打开网页,也是在phpstudy_pro中网站--管理--打开网站
![](https://i-blog.csdnimg.cn/blog_migrate/dbe40ec699df6888c34c1ec25173fbc8.png)
就做好了,但是数据库中要有test数据库,users表,表中有username和password字段。不然会与数据库不匹配,会报错。
数据库路径在你安装的php路径下:
phpstudy_pro\Extensions\MySQL5.7.26\bin
然后输入cmd进入数据库。
下面就是数据库的内容。
可以去查看数据库方面的内容。