我的注册表格有问题。它正在完美地注册新用户,尽管验证导致了我的问题。当具有相同用户名的用户尝试注册验证时,完美地选择了这一点,但我仍然会弹出确认用户已成功注册的diagloue框。当发生这种情况时我检查数据库,并且用户在执行验证时没有重新编译。我该如何阻止这种情况发生?
基本上我想要实现的是当新用户尝试输入已经存在的用户名时,消息停止说'成功',因为验证正在提取 这是我的代码
<?php
if ($_POST["register"]=="yes") {
//Declares the variables that will be posted to the database
$username = $_POST['username'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$password = md5($_POST['password']);
//Checks the database to see if username exists already
$query = "SELECT * FROM users WHERE username = '$username'";
$result = mysqli_query($db_connection, $query);
$nums = mysqli_num_rows($result);
//Checks the database to see if email address exists already
$query2 = "SELECT * FROM users WHERE email = '$email'";
$result2 = mysqli_query($db_connection, $query2);
$nums2 = mysqli_num_rows($result2);
if ($nums >= 1)
//informs user if username already exists
echo "Username already exists, click <a href = 'register1.php'>HERE </a> to try again";
else if ($nums2 >=1)
//informs user if email already exists
echo "Email Address already exists, click <a href = 'register1.php'>HERE </a> to try again";
else
{
$insert = 'INSERT INTO users(username, first_name, last_name, email, password,role_id, disable, activate) VALUES("'.$username.'","'.$first_name.'","'.$last_name.'","'.$email.'","'.$password.'", 2,0,0)';
mysqli_query($db_connection, $insert);
mysqli_close($db_connection);
("Location: mainpage.php");
}
{
$i = 1;
if ($i > 0) {
die;
}
else
"<script>alert (\"Registration Successful, your account has been sent for approval, and will be activated within 24 hours. \");</script>";
echo "<meta http-equiv='refresh' content='0'>";
}
}
?>
<!-- Registration form -->
<form name="register" id="register" method="post" action="register1.php">
<input type='hidden' name='register' value='yes'>
<table border = '0'>
<tr><td><strong> First Name:</strong></td><td><input type = "text" name = "first_name" size="30" value=""></td></tr>
<tr><td><strong> Last Name:</strong></td><td><input type = "text" name = "last_name" size="30" value=""></td></tr>
<tr><td><strong> Username:</strong></td><td> <input type = "text" name = "username" size="30" value=""> </td></tr>
<tr><td><strong> Email: </strong></td><td><input type = "text" name = "email" size="30" value=""> </td></tr>
<tr><td><strong> Password:</strong></td><td><input type = "password" name = "password" size="30" value=""> </td></tr>
<tr><td> <strong>Verify Password:</strong></td><td><input type = "password" name = "password2" size="30" value=""> </td></tr>
</table>
<p>
<input type ="button" value="Register" input class='button' onclick="verifyForm()"/>
</p>
</form>
答案 0 :(得分:1)
您的条件语句DO打印所需的结果,但不会停止程序执行。
例如:
$i = 1;
if ($i > 0) {
echo 'I want to stop the program, you have entered invalid $i';
}
Register();
但是,您将打印该消息,但Register()
也会执行。因为if()
块不一定意味着停止执行。
您必须在函数/方法中使用die()
,exit
或return
E.g:
$i = 1;
if ($i > 0) {
die('I want to stop the program, you have entered invalid $i');
}
Register();