我正在尝试创建一个注册表单,以检查用户输入的电子邮件和密码是否匹配,所有表单是否完整,以及该电子邮件是否以前用于注册。当实际上有错误时,我希望代码在注册表顶部显示错误消息。
但是,我无法弄清楚如何使错误消息显示在注册输入上方。相反,发生的情况是错误消息替换了注册输入并单独出现(要求用户刷新页面以再次尝试注册)。这是我在 stackoverflow 上的第一篇文章,如果我的编码术语不正确,请见谅!下面是我一直在尝试使用的代码。
<?php
// Connect to database server
mysql_connect("localhost", "root") or die (mysql_error ());
// Select database
mysql_select_db("mydatabase") or die(mysql_error());
//Checks to make sure form has been submitted
if (isset($_POST['submit'])) {
//Checks to make sure all fields are complete
if (!$_POST['FirstName'] | !$_POST['LastName'] | !$_POST['Username']| !$_POST['Username2']| !$_POST['Password']| !$_POST['Password2'] ) {
die('You did not complete all of the required fields');
}
//Check to see if email has already been used to reigster
if (!get_magic_quotes_gpc()) {
$_POST['Username'] = addslashes($_POST['Username']);
}
$usercheck = $_POST['Username'];
$check = mysql_query("SELECT Username FROM people WHERE Username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//Email has already been used to register message
if ($check2 != 0) {
die('Sorry, the Email '.$_POST['Username'].' has already been used to register.');
}
//Confirm that Emails match
if ($_POST['Username'] != $_POST['Username2']) {
die('The Email addresses you entered do not match. ');
}
//Confirm that passwords match
if ($_POST['Password'] != $_POST['Password2']) {
die('The passwords you entered do not match. ');
}
//Encrypt the password and add slashes if needed
$_POST['Password'] = md5($_POST['Password']);
if (!get_magic_quotes_gpc()) {
$_POST['Password'] = addslashes($_POST['Password']);
$_POST['Username'] = addslashes($_POST['Username']);
}
// now we insert it into the database
$insert = "INSERT INTO people(FirstName,LastName,Username,Password) VALUES ('" . $_POST["FirstName"] . "','" . $_POST["LastName"] . "','" . $_POST["Username"] . "','" . $_POST["Password"] . "')";
$add_member = mysql_query($insert);
?>
挂号的
谢谢,您已注册 - 您现在可以登录了。
<?php
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>First Name:</td><td>
<input type="text" name="FirstName" maxlength="60">
</td></tr>
<tr><td>Last Name:</td><td>
<input type="text" name="LastName" maxlength="60">
</td></tr>
<tr><td>Email Address:</td><td>
<input type="text" name="Username" maxlength="60">
</td></tr>
<tr><td>Confirm Email Address:</td><td>
<input type="text" name="Username2" maxlength="60">
</td></tr>
<tr><td>Password (8 character minimum):</td><td>
<input type="password" name="Password">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="Password2">
</td></tr>
<tr><th colspan=2>
<input type="submit" name="submit" value="Register"></th></tr> </table>
</form>
<?php
}
?>