0
点赞
收藏
分享

微信扫一扫

PHP学习笔记(四 实训结束) 实现登录与注册连接数据库

Sky飞羽 2022-06-29 阅读 20


文章目录

  • ​​知识点​​
  • ​​数据库连接​​
  • ​​页面停顿跳转​​
  • ​​获取数据​​
  • ​​login.html​​
  • ​​login.php​​
  • ​​register.html​​
  • ​​register.php​​

知识点

数据库连接

error_reporting(E_ALL & ~E_DEPRECATED);
header('Content-type:text/html;charset=utf8');
$conn = mysql_connect('localhost','root','root')
or die("<script>alert('连接MySQL失败,请重试!');</script>");
mysql_set_charset('utf8');//5.5以下适用
$select = mysql_select_db('user')
or die("<script>alert('连接数据库失败,请重试!');</script>");

页面停顿跳转

echo '<meta http-equiv="refresh" content=3;url=http://localhost:8081/login.html>';

获取数据

$query = mysql_query($sql)
or die("<script>alert('查询失败,请重试!');</script>");
$t = mysql_fetch_array($query,MYSQL_ASSOC);//此时$t是一个数组

login.html

<html>
<head>
<meta charset = "utf-8">
<title>
登录
</title>
</head>
<div style = "height:600px;width:300px;margin:0 auto;text-align:center">
<meta charset = "utf-8">
<div>
<form method = "post" action = "login.php">
用户名:<input type = "text" name = "name" placeholder = "请输入名称"><br>
密码:<input type = "password" name = "password" placeholder = "请输入名称"><br>
<input type = "submit" value = "提交">
<input type = "reset" value = "重置">
</form>
</div>
</div>
</html>

login.php

<?php
$name = $_POST['name'];
$password = $_POST['password'];
error_reporting(E_ALL & ~E_DEPRECATED);
header('Content-type:text/html;charset=utf8');
$conn = mysql_connect('localhost','root','root')
or die("<script>alert('连接MySQL失败,请重试!');</script>");
mysql_set_charset('utf8');//5.5以下适用
$select = mysql_select_db('user')
or die("<script>alert('连接数据库失败,请重试!');</script>");
$sql = "select * from user where name = '{$name}'";
$insert_sql = "insert into student values ('default','张三','23')";
$update_sql = "update student set name = '李四' where id = 7";
$delete_sql = "delete from student where id = 7";
$query = mysql_query($sql)//此时$query是一个数组
or die("<script>alert('查询失败,请重试!');</script>");
$t = mysql_fetch_array($query,MYSQL_ASSOC);
// print_r($t);
if($t['password'] == $password){
echo "登录成功";
}else{
echo "用户名或密码错误";
}
?>

register.html

<html>
<head>
<meta charset = "utf-8">
<title>
注册
</title>
</head>
<div style = "height:600px;width:300px;margin:0 auto;text-align:center">
<meta charset = "utf-8">
<div>
<form method = "post" action = "register.php">
用户名:<input type = "text" name = "name" placeholder = "请输入名称"><br>
密码:<input type = "password" name = "password" placeholder = "请输入密码"><br>
确认密码:<input type = "password" name = "repassword" placeholder = "请再次输入密码"><br>
邮箱:<input type = "email" name = "email" placeholder = "请输入邮箱"><br>
<input type = "submit" value = "提交">
<input type = "reset" value = "重置">
</form>
</div>
</div>
</html>

register.php

<?php
$name = $_POST['name'];
$password = $_POST['password'];
$email = $_POST['email'];
$repassword = $_POST['repassword'];
error_reporting(E_ALL & ~E_DEPRECATED);
header('Content-type:text/html;charset=utf8');
$conn = mysql_connect('localhost','root','root')
or die("<script>alert('连接MySQL失败,请重试!');</script>");
mysql_set_charset('utf8');//5.5以下适用
$select = mysql_select_db('user')
or die("<script>alert('连接数据库失败,请重试!');</script>");
$sql = "select * from user where name = '{$name}'";
$insert_sql = "insert into user values('{$name}','{$password}','{$email}')";
$update_sql = "update student set name = '李四' where id = 7";
$delete_sql = "delete from student where id = 7";
$query = mysql_query($sql)//此时$query是一个数组
or die("<script>alert('查询失败,请重试!');</script>");
// $t = mysql_fetch_array($query,MYSQL_ASSOC);
// print_r($t);s
if(empty($password)){
echo "密码不能为空";
echo '<meta http-equiv="refresh" content=3;url=http://localhost:8081/register.html>';
}elseif(empty($name)){
echo "用户名不能为空";
echo '<meta http-equiv="refresh" content=3;url=http://localhost:8081/register.html>';
}elseif(empty($email)){
echo "邮箱不能为空";
echo '<meta http-equiv="refresh" content=3;url=http://localhost:8081/register.html>';
}elseif($password != $repassword){
echo "两次密码不一致";
echo '<meta http-equiv="refresh" content=3;url=http://localhost:8081/register.html>';
}else{
echo "注册成功";
echo '<meta http-equiv="refresh" content=3;url=http://localhost:8081/login.html>';
mysql_query($insert_sql)
or die("<script>alert('插入失败,请重试!');</script>");//此时$query是一个数组
}
?>


举报

相关推荐

0 条评论