KRISKEE'S BLOG[GO]

使用XAMPP创建建议登录操作

  使用 XAMPP 工具建立本地服务器,进行登录注册操作,需要使用到工具:

   XAMPPCoda

  1.启动 XAMPP 的 Apache 服务器, MySQL 数据库以及 ProFTPD

  

 

  2.打开数据库:http://localhost/phpmyadminhttp://127.0.0.1/phpmyadmin

  创建数据库(test_data)和表(user)

  在表(user)中创建三个字段(uid, username, password), uid设置为主键( INT无符号 自增 )

    

 

  3.打开Coda创建登录/注册页面html以及php文件

 1 <!-- index.html -->
 2 
 3 <html >
 4     <head >
 5         <meta charset="utf-8"/>
 6         <title >My Web</title>
 7     </head>
 8     
 9 <!--------------------------------------->
10     
11     <body>    
12         <form method="post" >
13             <p align="center">用户名:<input type="text" name="username"> </p>
14             <br/>
15             <p align="center">密 码:<input type="text" name="password"> </p>
16             <br/>
17             <p align="center">
18 <!-- 使用 onclick="javascript:this.form.action = 'http://a.php'" 时表单跳转到不同的页面 -->
19                 <input type="submit" value="登录" onclick="javascript:this.form.action = 'http://localhost/login.php'"/>
20                 <input type="submit" value="注册" onclick="javascript:this.form.action = 'http://localhost/regist.php'">    
21             </p>
22         </form>
23     </body>
24 </html>

 

  login.php 登录界面

 1 <?php
 2     // 引入类并创建对象
 3     include("MySQL_Class.php");
 4     $mysql = new MySQL_Class();
 5     header("Content-type:text/html; charset=utf-8");
 6 
 7     $datas = array();
 8 
 9     // 判断参数    
10     if(isset($_POST["username"]) && isset($_POST["password"])){
11         // 接收form表单数据
12         $username = $_POST["username"];
13         $password = $_POST["password"];
14         
15         // 链接数据库
16         $connet = mysql_connect("localhost", "root", "");
17         if($connet){
18             // 选择数据库
19             mysql_select_db("test_data");
20             $result = mysql_query("SELECT * FROM user WHERE username = '".$username."' AND password = '".$password."'");
21             
22             if(mysql_fetch_assoc($result)){
23                 $warn = "登陆成功";
24 //                             $mysql->mysqlCRUD("", true, "数据查询");
25                 // 输出所有数据:每fetch一次,输出一行数据
26                 $result = mysql_query("SELECT * FROM user");
27                 while($data = mysql_fetch_assoc($result)){
28                     array_push($datas, $data);
29                 }
30                 
31             }else{
32                 $warn = "登录失败:用户名或密码有误/为空";
33             }
34             
35             // 关闭数据库
36             mysql_close($connet);
37         }else{
38             $warn = "数据库链接错误<br/>".strval(mysql_error());
39         }
40     }else{
41         $warn = "登录失败:参数有误";
42     }
43 //     echo($warn);    
44 ?>
45 
46 
47 <html >
48     <head>
49         <meta charset="utf-8">
50         <title >login page</title>
51         <style type="text/css">
52             .table{background:#000000; }
53             .table tr{background:#FFFFFF;}
54         </style>
55     </head>
56     
57     <body >
58         <p align="center" style="font-size: 20px; color: red">
59             <?php
60                 echo($warn."<br/>");
61             ?>
62             
63             <table class="table">
64                 <?php
65                     if($datas){
66                         echo ("
67                             <tr>
68                                 <th width=\"100px\">用户</th>
69                                 <th width=\"100px\">密码</th>
70                             </tr>
71                         ");
72                     
73                         foreach($datas as $value){
74                             echo("
75                                 <tr>
76                                     <td align=\"center\">".$value["username"]."</td>
77                                     <td align=\"center\">".$value["password"]."</td>
78                                 </tr>
79                             ");    
80                         }
81                     }    
82                 ?>
83             </table>
84             <br/>
85             <a href="index.html">返回</a>
86         </p>
87     </body>
88 </html>

 

   register.php 注册界面

 1 <?php
 2     // 引入类并创建对象
 3     include("MySQL_Class.php");
 4     $mysql = new MySQL_Class();
 5     header("Content-type:text/html; charset=utf-8");
 6     
 7     
 8     $datas = array();
 9     // 判断参数
10     if(isset($_POST["username"]) && isset($_POST["password"])){
11         // 接收form表单数据
12         $username = $_POST["username"];
13         $password = $_POST["password"];
14             
15         // 链接数据库
16         $connet = mysql_connect("localhost", "root", "");
17         if($connet){
18             // 选择数据库
19             mysql_select_db("test_data");
20             
21             // 查看用户是否已存在?
22             $fetchResult = mysql_query("SELECT * FROM user WHERE username = '".$username."'");
23             
24             if(mysql_fetch_assoc($fetchResult)){
25                 $warn = "用户[ ".$username." ]已存在";
26             }else{
27             
28                 // 添加用户
29                 $result = mysql_query("INSERT INTO user(username, password) VALUES('".$username."', '".$password."')");
30             
31                 if($result){
32                     $warn = "注册成功";
33 //                             $mysql->mysqlCRUD("", true, "数据查询");
34                 }else{
35                     $warn = "请填写用户名或密码";
36                 }
37             }
38             
39             // 关闭数据库
40             mysql_close($connet);
41         }else{
42             $warn = "数据库链接错误<br/>".strval(mysql_error());
43         }
44     }else{
45         $warn = "操作失败:参数有误";
46     }
47 //     echo($warn);    
48 ?>
49 
50 
51 <html >
52     <head>
53         <meta charset="utf-8">
54         <title >regist page</title>
55     </head>
56     
57     <body >
58         <p align="center" style="font-size: 20px; color: red">
59             <?php
60                 echo($warn."<br/>");
61             ?>
62             <br/>
63             <a href="index.html">返回</a>
64         </p>
65     </body>
66 </html>

 

 

 1 # MySQL_Class.php
 2 
 3 <?php
 4     include("MySQL_Class.php");
 5     $mysql = new MySQL_Class();
 6     
 7     header("Content-type:text/html; charset=utf-8");
 8     
 9     // 链接数据库
10     $connect = mysql_connect("localhost", "root", "");
11     if($connect){
12         // 选择数据库
13         mysql_select_db("test_data");
14         
15 //         mysql_CRUD("INSERT INTO user(username, password) VALUES('sishen', '234')", false, "添加数据");
16         
17 //         $mysql->mysql_CRUD("UPDATE user SET password = 'kkkk' WHERE username = 'sishen'", false, "修改数据");
18 
19         $mysql->mysql_CRUD("DELETE FROM user WHERE username = 'sishen'", false, "删除数据");
20         
21         echo("<br/><br/>");
22         $mysql->mysql_CRUD("SELECT * FROM user", true, "查询数据");
23         
24         // 关闭数据库
25         mysql_close($connect);
26         
27     }else{
28         echo("链接数据库错误<br/>");
29         echo(mysql_error());
30     }
31 ?>

 

  4.页面操作:点击登录

  

  因为之前数据库中已经存在username = 'kriskee', password = '123'这条数据,所以:

  

  点击注册:

  

 

  5.总结,使用php进行数据库操作步骤:

1.链接数据库
    $connect = mysql_connect("localhost", "root", "123");
2.选择数据库
    mysql_select_db("test_data");
3.CRUD操作
    3.1.操作SQL语句
        $result = mysql_query("SELECT * FROM user");
    3.2.如果进行数据查询,需要使用fetch函数逐条查询
        while($data = mysql_fetch_assoc($result)){
            print_r($data);
        }    
4.关闭数据库
    mysql_close($connect);

 

posted @ 2016-04-22 15:12  Kriskee  阅读(927)  评论(0编辑  收藏  举报