<?php
include ('database_connection.php');
include ('navigs.php');
if (isset($_POST['formsubmitted'])) {
$error = array();//Declare An Array to store any error message
if (empty($_POST['firstname'])) {//if no name has been supplied
$error[] = 'Please enter your firstname ';//add to array "error"
} else {
$firstname = $_POST['firstname'];//else assign it a variable
}
if (empty($_POST['nickname'])) {
$error[] = 'Please enter your nickname';
} else {
$nickname = $_POST['nickname'];
}
if (empty($_POST['email'])) {
$error[] = 'Please enter your e-mail ';
} else {
if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['email'])) {
//regular expression for email validation
$email = $_POST['email'];
} else {
$error[] = 'Your email address is invalid ';
}
}
if (empty($_POST['altemail'])) {
$error[] = 'Please enter your alternative email';
} else {
if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['altemail'])) {
$altemail = $_POST['altemail'];
} else {
$error[] = 'Not a valid email ';
}
}
if (empty($_POST['password'])) {
$error[] = 'Please enter a password ';
} else {
$password = $_POST['password'];
}
if (empty($error)) //send to Database if there's no error '
{ // If everything's OK...
// Make sure the email address is available:
$query_verify_email = "SELECT * FROM users WHERE Email ='$email' OR Altemail='$altemail' OR nickname='$nickname'";
$result_verify_email = mysqli_query($dbc, $query_verify_email);
if (!$result_verify_email) {//if the Query Failed ,similar to if($result_verify_email==false)
echo ' Database Error ';
}
if (mysqli_num_rows($result_verify_email) == 0) { // IF no previous user is using this email .
// Create a unique activation code:
$activation = md5(uniqid(rand(), true));
$query_insert_user = "INSERT INTO `users` ( `firstname`, `nickname`, `email`, `activation`, `altemail`, `password` ) VALUES ( '$firstname', '$nickname', '$email', '$activation', '$altemail', ENCRYPT('$password'))";
$result_insert_user = mysqli_query($dbc, $query_insert_user);
if (!$result_insert_user) {
echo 'Erreur SQL ';
}
if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.
// Send the email:
$message = " Pour activer ton compte, click sur ce lien:\n\n";
$message .= WEBSITE_URL . 'activate.php?email=' . urlencode($email) . "&key=$activation";
mail($altemail, 'Registration Confirmation', $message, 'From: noreply@mysite.org');
// Flush the buffered output.
// Finish the page:
echo '<div class="success">An email has been sent to the following addres: '.$altemail.' Please click on the link to activate your account </div>';
} else { // If it did not run OK.
echo '<div class="errormsgbox">An error has occurred please try again later .</div>';
}
} else { // The email address is not available.
echo '<div class="errormsgbox" >Either the nickname is already taken, the email address is already taken, or the alternative email you supplied is already on our system</div>';
}
} else {//If the "error" array contains error msg , display them
echo '<div class="errormsgbox"> <ol>';
foreach ($error as $key => $values) {
echo ' <li>'.$values.'</li>';
}
echo '</ol></div>';
}
mysqli_close($dbc);//Close the DB Connection
} // End of the main Submit conditional.
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Create you email account</title>
<style type="text/css">
body {
font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
.registration_form {
margin:0 auto;
width:500px;
padding:14px;
}
label {
width: 10em;
float: left;
margin-right: 0.5em;
display: block
}
.submit {
float:right;
}
fieldset {
background:#EBF4FB none repeat scroll 0 0;
border:2px solid #B7DDF2;
width: 500px;
}
legend {
color: #fff;
background: #80D3E2;
border: 1px solid #781351;
padding: 2px 6px
}
.elements {
padding:10px;
}
p {
border-bottom:2px solid #B7DDF2;
color:#666666;
font-size:13px;
margin-bottom:20px;
padding-bottom:9px;
}
a{
color:#0099FF;
font-weight:bold;
}
/* Box Style */
.success, .warning, .errormsgbox, .validation {
border: 1px solid;
margin: 0 auto;
padding:10px 5px 10px 50px;
background-repeat: no-repeat;
background-position: 10px center;
font-weight:bold;
width:450px;
}
.success {
color: #4F8A10;
background-color: #DFF2BF;
background-image:url('images/success.png');
}
.warning {
color: #9F6000;
background-color: #FEEFB3;
background-image: url('images/warning.png');
}
.errormsgbox {
color: #D8000C;
background-color: #FFBABA;
background-image: url('images/error.png');
}
.validation {
color: #D63301;
background-color: #FFCCBA;
background-image: url('images/error.png');
}
</style>
</head>
<body>
<form action="index.php" method="post" class="registration_form">
<fieldset>
<legend>Créer Un Compte Mail </legend>
<p>Create an email account <span style="background:#EAEAEA none repeat scroll 0 0;line-height:2;margin-left:220px;;padding:7px 7px;">Tu as un compe? <a href="http://www.mysite.net/webmail/">Login</a></span> </p>
<div class="elements">
<label for="firstname">Firstname / Initiale :</label>
<input type="text" id="firstname" name="firstname" size="25" />
</div>
<div class="elements">
<label for="nickname">NickName :</label>
<input type="text" id="nickname" name="nickname" size="25" />
</div>
<div class="elements">
<label for="email">E-mail :</label>
<input type="text" id="email" name="email" size="25" />
</div>
<div class="elements">
<label for="altemail">Email de verification :</label>
<input type="text" id="altemail" name="altemail" size="25" />
</div>
<div class="elements">
<label for="password">Your Password :</label>
<input type="password" id="password" name="password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Submit" />
</div>
</fieldset>
</form>
Go Back to <a href="index.php">Account Verification on sign up</a>
</body>
大家好我在这里想要实现的是:
当用户输入用户名,电子邮件地址和备用电子邮件时,可以检查数据库中是否已存在这些条目并相应地返回错误。
例如,如果已经使用了用户名,它将警告用户选择另一个用户名,依此类推。 目前它只返回一个错误,如果找到三个条目中的任何一个,我怎么能这样做才能根据表中的条目返回错误。
作为更新,我的问题最初是检查表格中的两个单独的字段,
首先,此表单允许用户创建自己的电子邮件帐户,但为此,他们还需要提供现有的电子邮件地址,以便向他们发送激活帐户的链接。
我可以成功检查三个字段,但我遇到的问题是如何分别为每个字段返回错误。
目前,我只能为任何字段返回相同的错误。
首先,它会检查电子邮件地址的有效性
其次,它将检查系统中是否已存在电子邮件地址,用户正在尝试创建的电子邮件地址以及他作为备用电子邮件地址提供的电子邮件地址
第三,它将检查昵称是否已被采用。
现在它完成了上述所有操作,我遇到的唯一问题是如何让它为每个错误返回错误。
因此,用户不会感到困惑,并且他/她将知道要更改哪个字段以完成他/她的注册。
如果你可以帮我解决这个问题
} else { // The email address is not available.
echo '<div class="errormsgbox" >Either the nickname is already taken, the email address is already taken, or the alternative email you supplied is already on our system</div>';
}
感谢大家的帮助,非常感谢
答案 0 :(得分:1)
在不同的通话之间做一些print_r($error)
。或者使用xdebug来逐步执行代码。
无论如何,我会建议你使用CMS或框架来构建,而不是将所有内容混合在一个文件中。
答案 1 :(得分:1)
我建议使用像Kohana
这样的框架,让您的生活更轻松。这也使您能够确保只有指定的字段进入数据库并进行清理。不想处理大规模的任务(我将来会弯腰)
为可用字段创建一组规则
$rules = array(
'email' => array(
'valid_email',
'not_empty'
),
'nickname' => array(
'not_empty',
)
)
编写一个方法来检查规则并获取消息或将其返回true。
foreach ($_POST as $field)
{
if (isset($rules[$field]))
{
// Check the rules
foreach ($rules[$field] as $rule)
{
$check = call_user_func(array(Valid, $rule), $field);
if ( !$check->isValid )
{
$this->errors[] = $check->message;
}
else
{
// Insert or do whatever you need to with the data
}
}
}
}