2014-12-02 136 views
0

基本上这是我的注册表单。我想这样做,如果所有的字段没有填写它会给出错误。我从这里做什么?我还想将错误显示为$味精如果所有字段未填写,我如何提供错误?

<?php 
include'db.php'; 
$msg=''; 
if (empty($_POST['']) 
|| empty($_POST['password']) 
|| empty($_POST['repassword']) 
|| empty($_POST['user_firstname']) 
|| empty($_POST['user_lastname']) 
){ 
// details sent Form 
$company=mysql_real_escape_string($_POST['company']); 
$address=mysql_real_escape_string($_POST['address']); 
$email=mysql_real_escape_string($_POST['email']); 
$phone=mysql_real_escape_string($_POST['phone']); 
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';  //this defines what a valid email should be 

if(preg_match($regex, $email)) 
{ 
$activation=md5($email.time()); // Encrypted email+timestamp, so randomly generated and unique 

$count=mysql_query("SELECT uid FROM preciousmetals WHERE email='$email'") or die(mysql_error()); 
if(mysql_num_rows($count) < 1) 
{ 
mysql_query("INSERT INTO preciousmetals(company,address,email,phone,activation) VALUES('$company','$address','$email','$phone','$activation');"); 



// sending email 

include 'smtp/Send_Mail.php'; 
$to=$email; 
$subject="Email verification"; 
$body='Hello, we need to make sure you are human. Please verify your email and get started using your Website account. '.$base_url.''.$activation.''; 

Mail($to,$subject,$body); 

$msg= "Registration successful, please activate email.";  


} 
else 
{ 
$msg= '<font color="#cc0000">This email is already in use, please enter a different one.</font>'; 
} 

} 
else 
{ 
$msg = '<font color="#cc0000">The email you have entered is invalid, please try again. </font>'; 
} 


} 
?> 

感谢您的帮助!

+0

'MD5($ email.time())'这不是随机的所有! – 2014-12-02 13:47:54

+0

其唯一的学校,他们建议使用。 – willstaff 2014-12-02 14:02:56

+1

然后我建议你质疑他们教你关于信息安全的一切。当人们不赞同这个建议时,“哈哈,它只是为了学校,我不需要编写好的代码” – 2014-12-02 14:27:05

回答

0

根本就

if (empty($_POST['password']) || empty($_POST['repassword']) || empty($_POST['user_firstname']) || empty($_POST['user_lastname'])){ 
    $msg = 'You have not filled all fiels'; 
} else { 
    // details sent Form 
    ...... 
} 
echo $msg; 

而且还要检查filter_var()用于电子邮件验证其更漂亮比正则表达式 http://php.net/manual/en/filter.examples.validation.php

+0

之后,它现在显示我的味精'这封电子邮件已被使用,请输入其他信息。 “所有时间 – willstaff 2014-12-02 14:00:26

+0

也许是因为您在数据库检查中选择了“uid”而不是“email”? – John 2014-12-02 14:18:45

相关问题