0
<?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>';
  }

谢谢大家的帮助,非常感谢

4

2 回答 2

1

print_r($error)在不同的调用之间做一些。或者使用 xdebug 来单步执行您的代码。

无论如何,我会建议您使用 CMS 或框架来构建,而不是将所有内容混合在一个文件中。

于 2012-10-30T18:05:45.797 回答
1

我建议使用类似的框架Kohana,将使您的生活更轻松。这也将使您能够确保只有指定的字段通过数据库并进行了清理。不想处理批量分配(我是未来的弯管机)

为可用字段创建一组规则

$rules = array(
  'email' => array(
     'valid_email',
     'not_empty'
   ),
   'nickname' => array(
     'not_empty', 
   )
)

编写一个方法来检查规则并获取消息或将其返回为真。

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
           }
       }

   }

}
于 2012-10-30T18:06:21.627 回答