今天着实是出了点岔子。这是一道课堂练习题目,目标仅仅是提交表单+验证表单的某些地方是否和规矩。
突然脑子想岔了,想实现的是……
将表单提交,并且验证表单的可行性,将表单提交到数据库,返回身份令牌/身份凭证,在网页的每一页都调用它。

为什么这么想?因为有个人突然提了一嘴:期末要交一整个动态网站……

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>表单</title>
<style>
* {
margin: 0;
padding: 0;
}

body {
background-image: linear-gradient(to right, #fa709a 0%, #fee140 100%);
}

#top {
height: 10px;
width: 100%;
}

h1 {
margin-bottom: 10px;
}

form {
width: 300px;
height: 290px;
background-color: red;
color: rgb(255, 255, 255);
margin: 100px auto;
text-align: center;
}

table {
width: 100%;
margin-left: 10px;
}

table td,
table {
border: 1px solid transparent;
}

table tr td:nth-of-type(2n-1) {
text-align: right;
padding-right: 10px;
}

table tr td:nth-of-type(2n) {
text-align: left;
}

#submit {
margin-top: 10px;
width: 100px;
height: 2em;
}
</style>
</head>

<body>
<form action="sqldata.php" method="post">
<div id="top"> </div>
<h1>用户注册页面</h1>
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" placeholder="字母数字下划线"></td>
</tr>
<tr>
<td>用户类型:</td>
<td>
<select id="gender" name="user_type" required>
<option value="0">学生</option>
<option value="1">教师</option>
<option value="2">管理员</option>
</select>
</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td>身份证:</td>
<td><input type="number" name="user_id" placeholder="必须是18位数字"></td>
</tr>
<tr>
<td>手机号码:</td>
<td><input type="number" name="mobile_number"></td>
</tr>
<tr>
<td>用户地址:</td>
<td><input type="text" name="user_addr"></td>
</tr>

</table>
<input type="submit" value="注册" name="submit" id="submit">
<?php
if (isset($_POST["submit"])) {
$verify = 0;
$str = '';
$username = $_POST['username'];
$password = $_POST['password'];
$user_type = $_POST['user_type'];
$email = $_POST['email'];
$user_id = $_POST['user_id'];
$mobile_number = $_POST['mobile_number'];
$user_addr = $_POST['user_addr'];

if (($username == "") | strlen(strlen($username) < 3) | $username == null) {
$str .= '用户名不能空且长度必须大于3!';
$verify = -1;
}
if (preg_match_all('/[0-9]*/', $password < 1) & preg_match_all('/[a-zA-Z]*/', $password) < 1 | strlen($password) > 16 | strlen($password) < 3 | preg_grep('/[^0-9a-zA-Z]/', $password) != 0) {
$str .= "密码必须是字母数字组合且长度必须在8到16之间!";
$verify = -1;
}
if (preg_match_all('/[@\.]/', $email) < 1 | preg_match_all('/[@\.]/', $email) > 3) {
$str .= "电子邮件格式输入错误!";
$verify = -1;
}
if (strlen($user_id) != 18) {
$str .= "身份证号码必须等于18位!";
$verify = -1;
}
if (strlen($mobile_number) != 11) {
$str .= "电话号码必须等于11位!";
$verify = -1;
}
$flag = 0;
if ($verify == -1) {
$flag = -1;
echo "<script>alert(' 注册失败');</script>";
echo "<script>alert(\"" . $str . "\");</script>";
} else {
$flag = 1;
echo "<script>alert('注册成功');</script>";
}
if ($flag == 1) {
$conn = mysqli_connect("127.0.0.1", "root", "123456", "mydatabase");

// 这里写入数据库
$tableName = "users";
$result = mysqli_query($conn, "SHOW TABLES LIKE '$tableName'");
mysqli_num_rows($result) == 1;
$sqltable = "INSERT INTO users (username,password,user_type,email,user_id,mobile_number,user_addr) VALUES ('$username','$password',$user_type,'$email','$user_id','$mobile_number','$user_addr')";
mysqli_query($conn, $sqltable);
mysqli_close($conn);
}
}
?>
</form>
</body>

</html>