注意:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>Register</title> </head> <body> <form action="registerHandler.php" method="post"> 注册用户:<input type="text" name="user"/><br> 注册密码:<input type="text" name="pass"/><br> <input type="submit" value="注册"/> </form> </body></html>
<?php $result = mysql_query("create database `sqlTest` character set 'utf8' collate 'utf8_general_ci' "); if ($result) echo "创建成功"; else echo "创建失败" . $result;?>
<?php header("Content-type:text/html;charset=utf-8"); $user = $_POST["user"]; $pass = $_POST["pass"]; $link = mysql_connect("localhost", "root"); // 先选择了这个数据库进行操作 mysql_select_db("N_&_P", $link); // 创建表 $result = mysql_query("create table if not exists `N_&_P` (username varchar(255) primary key, password text) default charset=utf8", $link); mysql_query("set names utf8", $link); if ($result) { echo "成功"; $r = mysql_query("insert into `N_&_P` values ('{$user}', '{$pass}')"); if ($r) echo "成功" . "<hr>"; else echo "失败" . "<hr>"; }?>
2. ログイン関数
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>Register</title> </head> <body> <form action="loginHandler.php" method="post"> 登录用户:<input type="text" name="user"/><br> 登录密码:<input type="text" name="pass"/><br> <input type="submit" value="登录"/> </form> </body></html>
<?php header("Content-type:text/html;charset=utf-8"); $user = $_POST["user"]; $pass = $_POST["pass"]; $link = mysql_connect("localhost", "root"); mysql_select_db("N_&_P", $link); $re = mysql_query("select * from `N_&_P` where username = '{$user}' and password = '{$pass}'"); $obj = mysql_fetch_object($re); if ($obj) echo "登录成功了!"; else echo "登录失败了!";?>