2012-06-26 80 views
1

我是一個初學PHP編碼器。我希望它是如果當他們註冊我的PHP代碼時,它回聲「你已經註冊」,而不是隻顯示一個空白頁。這是我的代碼:MySQL和PHP Basic Form Echo

<?php 
    require('config.php'); 

    if(isset($_POST['submit'])){ 

     //Preform the verification of the nation 
     $email1 = $_POST['email1']; 
     $email2 = $_POST['email2']; 
     $pass1 = $_POST['pass1']; 
     $pass2 = $_POST['pass2']; 

     if($email1 == $email2) { 
     if($pass1 == $pass2) { 
      //All good. Carry on. 

      $name = mysql_escape_string($_POST['name']); 
      $lname = mysql_escape_string($_POST['lname']); 
      $uname = mysql_escape_string($_POST['uname']); 
      $email1 = mysql_escape_string($_POST['email1']); 
      $email2 = mysql_escape_string($_POST['email2']); 
      $pass1 = mysql_escape_string($_POST['pass1']); 
      $pass2 = mysql_escape_string($_POST['pass2']); 
      $pass1 = md5($pass1); 

      $sql = mysql_query("SELECT * FROM `users` WHERE `uname` = '$uname'"); 
      if(mysql_num_rows($sql) > 0) { 
       echo "Sorry, that user already exists!"; 
       exit(); 
      } 
      mysql_query("INSERT INTO `users` (`id`, `name`, `lname`, `uname`, `email`, 
         `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', 
         '$pass1')"); 

      }else{ 
       echo "Sorry, your passwords do not match<br><br>"; 
       exit(); 
     } 
    }else{ 
     echo "Sorry, your emails do not match.<br><br>"; 
    } 
    }else{ 
    $form = <<<EOT 
    <form action="register.php" method="POST"> 
    First Name: <input type="text" name="name" /><br /> 
    Last Name: <input type="text" name="lname" /><br /> 
    Username: <input type="text" name="uname" /><br /> 
    Email: <input type="text" name="email1" /><br /> 
    Confirm Email: <input type="text" name="email2" /><br /> 
    Password: <input type="password" name="pass1" /><br /> 
    Confirm Password: <input type="password" name="pass2" /><br /> 
    <input type="submit" value="Register" name="submit" /> 
    </form> 
    EOT; 
    echo $form; 
    } 
?> 

正如你所看到的,如果一切正常,沒有回聲。請幫助我添加一個回聲,如果他們的註冊登記!

+0

提示:不要檢查是否存在表單字段以確定是否發生了帖子。這是不可靠的。改用'if($ _SERVER ['REQUEST_METHOD'] =='POST'){...}'代替。它是100%可靠的,永遠不會讓你失望。 –

+0

順便說一下,md5不是創建「安全」用戶密碼的方法。這是一種便宜的方式,很容易解密。請檢查使用[PHPass](http://www.openwall.com/phpass/)這將確保一個更安全的環境。不要學習困難的方式 – ntgCleaner

+0

確保它被插入到數據庫中,然後顯示消息! – meWantToLearn

回答

0

只需檢查插入查詢的返回。

$result = mysql_query("INSERT ..."); 
if ($result) { 
    echo "Created!"; 
} else { 
    echo "Uh oh! Something went wrong!"; 
} 
0

在你的情況 - 你可以只輸入:

echo "Congratulations, You've been submitted"; 

或任何你想說的話剛下插入語句

0

前的最後

}else{ 

在代碼放

echo "You have signed up"; 

就是這樣:)