<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>register.php</title>
<link rel="stylesheet" type="text/css" href="css/register.css" />
</head>
<body>
<div class="register">
<h2>注册</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"
onsubmit="upload()">
<div class="register_flex">
<div class="register_flex_box">
<input type="text" name="username" placeholder="请注册用户名" />
</div>
<div class="register_flex_box">
<input type="password" name="password" placeholder="请自定义密码" />
</div>
<div class="register_flex_box">
<input type="text" name="tip" placeholder="请自定义密保标题:" />
</div>
<div class="register_flex_box">
<input type="text" name="tip_content" placeholder="请输入密保内容:" />
</div>
<div class="register_flex_box">
<input class="register_button" type="submit" value="注册"/>
</div>
<div class="register_flex_box">
<button class="register_button" name="register" onclick='window.open("04.php")'>登录</button>
</div>
</div>
</form>
</div>
<script>
function upload(){
<?php
$username=$password=$tip=$tip_content="";
if($_SERVER["REQUEST_METHOD"]=="POST"){
if (empty($_POST["username"])) {
echo "<script>alert('username is required!');</script>";
} else {
$name = check_input($_POST["name"]);
}
$username=check_input($_POST['username']);
$password=check_input($_POST['password']);
$tip=check_input($_POST['tip']);
$tip_content=check_input($_POST['tip_content']);
}
function check_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// 连接数据库
$con=mysqli_connect("localhost","test_connect_db","YNxb8Tj","test_connect_db");
if (!$con)
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
$sql="insert into user (username,password,tip,tip_content) values ('$username','$password','$tip','$tip_content')";
if (mysqli_query($con, $sql)) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "
" . mysqli_error($con);
}
mysqli_close($con);
?>
}
</script>
</body>
</html>
PHP提交表单出现一个问题,每次会提交两遍,哪个地方不对啊?