目录结构:
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用户登陆</title>
<style>
form,h2{
margin: 0 auto;
width: 300px;
line-height: 30px;
}
</style>
</head>
<body>
<h2>登陆</h2>
<form action="login.php" method="post">
username:<input type="text" name="username" placeholder="用户名"><br>
password:<input type="password" name="password" placeholder="密码"><br>
<input type="submit" value="登陆">
<input type="button" onclick="window.location.href='register.html'" value="注册">
</form>
</body>
</html>
login.php:
<?php
header("Content-type: text/html; charset=utf-8");
$conn = mysql_connect("localhost:3306","用户名","密码");
if($conn){echo '连接成功'.'<br>';};
$select = mysql_select_db("数据库名",$conn);
if($select){echo '选择成功'.'<br>';};
$abc = mysql_query("set names utf8");
if($abc){echo '字符集成功'.'<br>';};
$username = $_POST['username'];
if($username){echo 'username成功'.'<br>';}else echo 'username失败'.'<br>';
$password = $_POST['password'];
if($username){echo 'password成功'.'<br>';}else echo 'password失败'.'<br>';
$sql = "select username,password from tb_atest where username = '$username' and password = '$password'";
$result = mysql_query($sql,$conn);
$row = mysql_fetch_object($result);
do{
echo 'username:'.$row->username.'<br>';
echo 'password:'.$row->password.'<br>';
}while ($row=mysql_fetch_object($result));//这里必须写mysql_fetch_object
mysql_free_result($result);
mysql_close($conn);
echo '登陆完成';
register.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>用户注册</title>
<style>
form,h2{
margin: 0 auto;
width: 300px;
line-height: 30px;
}
</style>
</head>
<body>
<h2>注册</h2>
<form action="register.php" method="post">
username:<input type="text" name="username" placeholder="用户名"><br>
password:<input type="password" name="password" placeholder="密码"><br>
ConfirmPW:<input type="password" name="ConfirmPW" placeholder="确认密码"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
register.php
<?php
header("Content-type: text/html; charset=utf-8");
$conn = mysql_connect("localhost:3306","用户名","密码");
mysql_select_db("数据库名",$conn);
mysql_query("set names utf8");
$username = $_POST['username'];
$password = $_POST['password'];
$ConfirmPW = $_POST['ConfirmPW'];
if ($password != $ConfirmPW){
// echo '<script>alert("两次输入密码不一致")';
exit("输入的密码与确认密码不相等!");};
$sql = "insert into tb_atest values(null,$username,$password)";
mysql_query($sql,$conn);
$result = mysql_query("select username from tb_atest where username = '$username'",$conn);
if($result){echo '注册成功';}
mysql版本尽量在5.0左右