用 PHP和html做一个简单的注册页面
index.html的设计
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="./in.php" method="post" style="width:220px; margin:300px auto;">
用户名:<input type="text" name="username" id="" >
<br>
密 码:<input type="password" name="password" id="">
<br>
年 龄:<input type="number" name="age" id="">
<br>
性 别:<select name="gender" id="">
<option value="0">女</option>
<option value="1">男</option>
</select>
<br>
<input type="submit" name="" id="">
</form>
</body>
</html>
in.php
php
<?php
header('content-type:text/html;charset=utf-8');
$host = "localhost";
$user = "root";
$pass = "";
$db = "mydb";
$mysqli = mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
echo "连接失败" . mysqli_connect_error();
}
$mysqli->set_charset("utf8");
//执行读取用户列表
select($mysqli);
//注册
insert($mysqli);
$mysqli->close();
//自定义函数function
function insert($mysqli)
{
$sql = "INSERT INTO demo(username,password,age,gender)VALUES(?,?,?,?)";
$mysqli_stmt = $mysqli->prepare($sql);
$username = $_POST["username"];
$password = $_POST["password"];
$age = $_POST["age"];
$gender = $_POST["gender"];
//ssii代表四个字符串,sii代表三个字符串
$mysqli_stmt->bind_param("ssii", $username, $password, $age, $gender);
if ($mysqli_stmt->execute()) {
echo "编号".$mysqli_stmt->insert_id."注册成功";
echo PHP_EOL;
} else {
echo "失败" . $mysqli_stmt->errno;
}
//释放结果集
$mysqli_stmt->free_result();
$mysqli_stmt->close();
};
//自定义函数function
function select($mysqli)
{
$sql = "SELECT uid,username,password,age,gender FROM demo";
$mysqli_stmt = $mysqli->prepare($sql);
$uid=null;
$username = null;
$password = null;
$age = null;
$gender = null;
if ($mysqli_stmt->execute()) {
$mysqli_stmt->bind_result($uid,$username, $password, $age, $gender);
while ($mysqli_stmt->fetch()) {
echo "编号" . $uid . "<br>";
echo "姓名" . $username . "<br>";
echo "密码" . $password . "<br>";
echo "年龄" . $age . "<br>";
$gender = $gender == 1 ? "男" : "女";
echo "性别" . $gender . "<br>";
}
}else{
echo"查询失败";
}
//释放结果集
$mysqli_stmt->free_result();
$mysqli_stmt->close();
};
?>
数据库的设计
sql
DROP TABLE IF EXISTS `demo`;
CREATE TABLE IF NOT EXISTS `demo` (
`uid` int NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`username` varchar(20) COLLATE utf8mb4_bin NOT NULL COMMENT '用户姓名',
`password` varchar(18) COLLATE utf8mb4_bin NOT NULL COMMENT '用户密码',
`age` int NOT NULL COMMENT '用户年龄',
`gender` int NOT NULL COMMENT '用户性别',
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
效果