注册和登陆与数据库的链接
发布日期:2021-10-17 00:06:22 浏览次数:23 分类:技术文章

本文共 1585 字,大约阅读时间需要 5 分钟。

注册和登陆其实是从一个表中进行提取和写入数据

1.(1)先建立一个注册页面

1
2
3
4
5
6
7
8
9
<body>
        
<h1>注册页面</h1>
        
<form action=
"./zhucechuli.php" 
method=
"post"
>     
//链接到的文件,就是登陆的处理页面
            
<div>用户名:<input type=
"text" 
name=
"uid"
/></div>
            
<div>密码:<input type=
"text" 
name=
"pwd"
/></div>
             
<div>姓名:<input type=
"text" 
name=
"nm"
/></div>
            
<div><input type=
"submit" 
value=
"注册" 
/></div>
        
</form>
    
</body>

(2)创建注册处理页面(也就向数据库的一个表中写入数据)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$uid 
$_POST
[
"uid"
]; 
//用的什么方法就用什么,这里注册中是用的post,所以这里用post
$pwd 
$_POST
[
"pwd"
];
$nm 
$_POST
[
"nm"
];<br>
//造数据库
$db 
new 
MySQLi(
"localhost"
,
"root"
,
"123"
,
"test2"
);
//写sql语句
$sql 
"insert into huiyuan values('{$uid}','{$pwd}','{$nm}')"
;
//执行语句
$r 
$db
->query(
$sql
);
//判断是否登陆成功
if
(
$r
)
{
  
echo 
"注册成功!"
;
}
else
{
  
echo 
"注册失败!"
;
}
 
?>

2.登陆和注册差不多,(1)建立登陆页面

1
2
3
4
5
6
7
8
<body>
        
<h1>登陆页面</h1>
        
<form action=
"./dengluchuli.php" 
method=
"post"
>
            
<div>用户名:<input type=
"text" 
name=
"uid" 
/></div>
            
<div>密码:<input type=
"password" 
name=
"pwd" 
/></div>
            
<div><input type=
"submit" 
value=
"登陆" 
/></div>
        
</form>
    
</body>

(2)登陆的处理页面(从一个表中提取数据)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
$uid 
$_POST
[
"uid"
];
$pwd 
$_POST
[
"pwd"
];
//造数据库
$db 
new 
MySQLi(
"localhost"
,
"root"
,
"123"
,
"test2"
);
//sql语句
$sql 
"select mima from huiyuan where yonghu='{$uid}'"
;  
//用这个语句可以简单的避免用户名不对也可以登陆
//执行sql语句
$result 
$db
->query(
$sql
);<br>
//取值
$attr 
$result
->fetch_row();<br>
//判断
if
(
$attr
[0]==
$pwd 
&& !
empty
(
$pwd
))
{
    
echo 
"登陆成功!"
;  
}
else
{
    
echo 
"登录失败!"
;  
}
?>

SQL注入攻击
1.过滤用户的输入
2.使用预处理语句
3.写代码的时候尽量避免

转载地址:https://blog.csdn.net/sulan2131/article/details/69101100 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:数据访问
下一篇:增删改查的数据访问

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月13日 17时29分20秒