2014-12-02 47 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

相關問題