Hello stackoverflow社区。 我目前正在开发一个小型学校项目, 但是无法创建注册。 似乎它没有向我的数据库发布任何数据。 不确定导致这种情况的原因。因为我可以从phpmyadmin手动插入数据! 我现在更喜欢MYSQL和PHP。 是的,我使用php建立了与我的数据库的mysql连接。 没有问题。
以下代码:
<div class="errors-container">
<?php
if (isset($_POST['registerBtn']))
{
$username = $_POST['username'];
$password = $_POST['passwd'];
$repeat = $_POST['rpasswd'];
$email = $_POST['email'];
$terms = $_POST['terms'];
$errors = array();
if (empty($username) || empty($password) || empty($repeat) || empty($email))
{
$errors[] = 'Please fill in all required fields.';
}
$checkUsername = $odb -> prepare("SELECT * FROM `users` WHERE `username`= :username");
$checkUsername -> execute(array(':username' => $username));
$countUsername = $checkUsername -> rowCount();
if ($countUsername != 0)
{
$errors[] = 'The username you have entered is already in use.';
}
$checkEmail = $odb -> prepare("SELECT * FROM `users` WHERE `email`= :email");
$checkEmail -> execute(array(':email' => $email));
$countEmail = $checkEmail -> rowCount();
if ($countEmail0 != 0)
{
$errors[] = 'The email you have entered is already in use.';
}
if (strlen($_POST['passwd']) < 4) {
$errors[] = 'The username you have entered is too short.';
}
if (strlen($_POST['username']) > 15) {
$errors[] = 'The username you have entered is too long.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$errors[] = 'You have entered an invalid e-mail address.';
}
if (!ctype_alnum($username))
{
$errors[] = 'The username you have entered is invalid.';
}
if ($password != $repeat)
{
$errors[] = 'The passwords you have entered does not match.';
}
if ($terms != 'agree')
{
$errors[] = 'You have to agree t.o.s before using our service.';
}
if (empty($errors))
{
$sha = hash("sha512", $password);
$activation = generateRandomString();
$insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0, 0)");
$insertUser -> execute(array(':username' => $username, ':password' => $sha, ':email' => $email));
//Send mail here
echo '<div class="alert alert-success fade in"><button type="button" class="close close-sm" data-dismiss="alert"><i class="fa fa-times"></i></button><strong>Success!</strong> You have registered your account successfully! Redirecting..</div><meta http-equiv="refresh" content="3;url=login.php">';
}
else
{
echo '<div class="alert alert-block alert-danger fade in"><button type="button" class="close close-sm" data-dismiss="alert"><i class="fa fa-times"></i></button><strong>Oops!</strong><br />';
foreach($errors as $error)
{
echo '- '.$error.'<br />';
}
echo '</div>';
}
}
?>
</div>
<form method="post" role="form" id="register">
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" required name="username" id="username" placeholder="Username" autocomplete="off" autofocus/>
</div>
<div class="form-group">
<label for="c-email">Email</label>
<input type="email" class="form-control" required name="email" id="email" placeholder="E-Mail Address" autocomplete="off" />
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" required name="passwd" id="passwd" placeholder="Password" autocomplete="off" />
</div>
<div class="form-group">
<label for="c-pwd">Confirm Password:</label>
<input type="password" class="form-control" required id="rpasswd" name="rpasswd" placeholder="Confirm Password" autocomplete="off" />
</div>
<div class="checkbox">
<label><input type="checkbox" name="terms" value="agree"> I agree to the <a>terms & conditions</a></label>
</div>
<input class="login-btn btn-block" type="submit" name="registerBtn" value="Sign Up"><i class="fa-plus"></i>
</form>
答案 0 :(得分:1)
有一个问题:if($ countEmail0!= 0),你必须把它改成if($ countEmail!= 0),注意你的变量名,这是smtg else试试:
$insertUser = $odb -> prepare("INSERT INTO `users` VALUES(NULL, :username, :password, :email, 0, 0, 0, 0, 0)");
$insertUser->bindParam(':username', $username);
$insertUser->bindParam(':password', $password);
$insertUser->bindParam(':email', $email);
$insertUser->execute();