用 PHP和html做一个简单的注册页面

用 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>
        密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" id="">
        <br>
        年&nbsp;&nbsp;&nbsp;龄:<input type="number" name="age" id="">
        <br>
        性&nbsp;&nbsp;&nbsp;别:<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;

效果

相关推荐
龙哥·三年风水9 小时前
群控系统服务端开发模式-应用开发-个人资料
分布式·php·群控系统
顾菁寒12 小时前
WEB第二次作业
前端·css·html
Qhumaing12 小时前
html第一个网页
网络·html·html5
Dingww101113 小时前
梧桐数据库中的网络地址类型使用介绍分享
数据库·oracle·php
Genius Kim16 小时前
SpringCloud Sentinel 服务治理详解
spring cloud·sentinel·php
前端Hardy16 小时前
HTML&CSS:爱上班的猫咪
前端·javascript·css·vue.js·html
前端Hardy20 小时前
超萌!HTML&CSS:打造趣味动画卡通 dog
前端·css·html·css3
原机小子20 小时前
城镇保障性住房管理:SpringBoot系统解决方案
数据库·spring boot·php
Komorebi⁼20 小时前
JavaScript的对象事件监听处理,交互式网页的关键!
开发语言·前端·javascript·css·html
kali-Myon21 小时前
NewStarCTF2024-Week5-Web&Misc-WP
前端·python·学习·mysql·web安全·php·web