<>首先设计数据库的格式:

1:在test数据库中创建表:user(也可以在别的地方进行创建,只要对准好位置即可)
2:创建username(varchar(30)),password(varchar(30))
<>注册html代码:(signup.html) <!doctype html> <html lang="en"> <head> <meta
charset="UTF-8"> <title>用户注册页面</title> </head> <body> <form action="signup.php"
method="post"> <p>用户名:<input type="text" name="name"></p> <p>密 码: <input
type="text" name="password"></p> <p><input type="submit" name="submit"
value="注册"></p> </form> </body> </html>
PS:
lang="en"的作用:
这是一个W3C的标准;lang代表语言,en是英语;改成zh-cn指简体中文

<>注册PHP代码:(signup.php) <?php header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){ exit("错误执行"); }//判断是否有submit操作
$name=$_POST['name'];//post获取表单里的name
$password=$_POST['password'];//post获取表单里的password
include('connect.php');//链接数据库 $q="insert into user(username,password) values
('$name','$password')";//向数据库插入表单传来的值的sql $reslut=mysql_query($q,$con);//执行sql
if (!$reslut){ die('Error: ' . mysql_error());//如果sql执行失败输出错误 }else{ echo
"注册成功";//成功输出注册成功 } mysql_close($con);//关闭数据库 ?> PS: header("Content-Type:
text/html; charset=utf8");的作用:
header("Content-type: text/html; charset=utf-8");
这一句前不能向页面输出任何内容,意思是不能使用echo等语句,也不能输出html标签或文字,如果在header前输出了等标签或文字那肯定会报错
实际上你用了header("Content-type: text/html; charset=utf-8"); 这一句的话就不需要用了,同一个道理 <>
登录html代码:(login.html) <!doctype html> <html lang="en"> <head> <meta
charset="UTF-8"> <title>登陆</title> </head> <body> <form name="login"
action="login.php" method="post"> <p>用户名<input type=text name="name"></p> <p>密
码<input type=password name="password"></p> <p><input type="submit"
name="submit" value="登录"></p> </form> </body> </html> <>登录PHP代码:(login.php)
<?PHP header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){ exit("错误执行"); }//检测是否有submit操作
include('connect.php');//链接数据库 $name = $_POST['name'];//post获得用户名表单值 $passowrd
= $_POST['password'];//post获得用户密码单值 if ($name && $passowrd){//如果用户名和密码都不为空 $sql
= "select * from user where username = '$name' and
password='$passowrd'";//检测数据库是否有对应的username和password的sql $result =
mysql_query($sql);//执行sql $rows=mysql_num_rows($result);//返回一个数值 if($rows){//0
false 1 true header("refresh:0;url=welcome.html");//如果成功跳转至welcome.html页面 exit;
}else{ echo "用户名或密码错误"; echo " <script>
setTimeout(function(){window.location.href='login.html';},1000); </script>
";//如果错误使用js 1秒后跳转到登录页面重试,让其从新进行输入 } }else{//如果用户名或密码有空 echo "表单填写不完整"; echo "
<script> setTimeout(function(){window.location.href='login.html';},1000);
</script>"; //如果错误使用js 1秒后跳转到登录页面重试,让其从新进行输入 } mysql_close();//关闭数据库 ?> <>
登录成功html代码:(welcome.html) <!doctype html> <html lang="en"> <head> <meta
charset="UTF-8"> <title>登陆成功</title> </head> <body> 欢迎光临 </body> </html> <>
链接数据库的代码:(connect.php) <?php $server="localhost";//主机的IP地址,你也可以选填127.0.0.1
$db_username="root";//数据库用户名 $db_password="root";//数据库密码 $con =
mysql_connect($server,$db_username,$db_password);//链接数据库 if(!$con){ die("can't
connect".mysql_error());//如果链接失败输出错误 }
mysql_select_db('test',$con);//选择数据库(前边已经说是test数据库) ?>

技术
今日推荐
PPT
阅读数 126
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信