http://patti-bee2.dcccd.edu/coleman/wonder%20penguin/php/register.php
这是表单应该如何工作的——它应该为以前没有访问过该站点的人使用粘性标签,并给它一些默认值。当您提交表单时,它应该在页面上验证并在出现问题时刷新,如果一切正确,它应该将您重定向到另一个页面并显示结果。(我不需要放在那里的部分,因为我是 100% 这不是问题。)
你觉得有什么问题?我使它接近我老师的代码。
当您提交代码时,我所做的就是我的页面只是闪烁然后返回。
<?php
ob_start();
if (isset($_POST['submit'])) {
$valid = true;
$validate = true;
$username = trim($_REQUEST['username']);
$email = trim($_REQUEST['email']);
$password = trim($_REQUEST['password']);
}
else
{
$username = "";
$email = "";
$password = "";
$valid = false;
$validate = false;
}
?>
<html>
<body>
<form method="post" action="register.php">
<label for="username"> Username </label><input type="text" name="username" id="username"
value="<?php echo $username ?>">
<?php if ($validate) {
if (empty($username)) {
echo "<p> There must be a username! </p>";
$valid = false;
}
if (strlen($username) > 15) {
echo "<p> This username is too long! It must be 8-15 characters long! </p>";
$valid = false;
}
if (strlen($username) < 8) {
echo "<p> This username is too short! It must be 8-15 characters long! </p>";
$valid = false;
}
}
?>
<label for="email">E-Mail </label><input type="email" name="email" id="email"
value="<?php echo $email ?>">
<?php if ($validate) {
if (empty($email)) {
echo "<p> We need an email! </p>";
$valid = false;
}
}
?>
<label for="password">Password </label><input type="text" name="password" id="password"
value="<?php echo $password ?>">
<?php if ($validate) {
if (empty($password)) {
echo "<p> We need a password! </p>";
$valid = false;
}
}
?>
<input type="submit" value="Register!" />
</form>
<?php
if ($valid) {
require_once 'config/connection.php';
$query = "INSERT INTO user (username, email, password) values ( $username, $email, $password);";
$result = mysql_query($query, $dbConn);
if ($result) {
$userid = mysql_insert_id();
header("Location:success.php?userid=$userid");
ob_end_clean();
exit();
} else {
echo "<p> Unable to Update Database! </p>";
}
}
ob_end_flush();
?>
</body>
</html>