2013-09-24 35 views
-2

我正在創建一個管理員頁面,其中管理員可以爲其創建用戶帳戶。這個想法是,一旦表格完成,當點擊'提交'時,必須發送一封電子郵件給用戶(包含所選帳戶的ID和名稱)。在同一行動中,表格也必須首先驗證,如果驗證有任何錯誤,則不應將數據提交給數據庫。這一切都沒有發生,但我不明白爲什麼。驗證表格並在提交時發送確認電子郵件

  • 沒有被髮送的電子郵件,
  • 插入數據,即使有錯誤數據庫,並在加載頁面,
  • 將顯示所有表單字段
  • 錯誤,即使提交按鈕沒有被點擊。

任何幫助,建議或鏈接到可能的來源/教程將不勝感激。

下面是我的代碼:(請注意,我只在PHP,HTML工作,使用MySQL數據庫)

<html> 
<head> 
    <title> 
     User Registration 
    </title> 
    <?PHP 


    include_once 'includes\functions.php'; 
    connect(); 

    error_reporting(E_ERROR | E_PARSE); 


//Assign variables 
    $accounttype=mysql_real_escape_string($_POST['accounttype']); 
    $sname = mysql_real_escape_string($_POST['sname']); 
    $fname = mysql_real_escape_string($_POST['fname']); 
    $email = mysql_real_escape_string($_POST['email']); 
    $address = mysql_real_escape_string($_POST['address']); 
    $contact_flag = mysql_real_escape_string($_POST['contact_flag']); 


//Validating form(part1)   

    $error='';   


//Connect to database     
     $SQL= 
      "INSERT INTO student 
         (
          sname,fname,email, address, contact_flag 
         ) 
      VALUES 
         (

         '$sname', '$fname', '$email', '$address', '$contact_flag' 
         ) 
      "; 
     if (!mysql_query($SQL)) 
      { 
       print'Error: '.mysql_error(); 
      } 

     mysql_close($db_handle); 
//Validate form(part 2) 
    if (isset($_POST['sname'], $_POST['fname'],$_POST['email'],$_POST['address'])); 
     { 
      $errors=array(); 


       $accounttype= mysql_real_escape_string($_POST['accounttype']); 
       $sname = mysql_real_escape_string($_POST['sname']); 
       $fname = mysql_real_escape_string($_POST['fname']); 
       $email = mysql_real_escape_string($_POST['email']); 
       $address = mysql_real_escape_string($_POST['address']); 
       $contact_flag = mysql_real_escape_string($_POST['contact_flag']); 

// form validation 
         if(strlen(mysql_real_escape_string($sname))<1) 
         { 
          $errors[]='Your surname is too short!'; 
         } 

         if(strlen(mysql_real_escape_string($fname))<1) 
         { 
          $errors[]='Please insert you full first name'; 
         } 


         if(filter_var($email, FILTER_VALIDATE_EMAIL)===FALSE) 
         { 
          $errors[]='Please insert your valid email address'; 
         } 
         if(strlen(mysql_real_escape_string($address))<8) 
         { 
          $errors[]='Please insert your postal address'; 
         } 

          echo'<pre>'; 
          print_r($errors); 
          echo'</pre>'; 
     } 


//confirmation email 

    // Subject of confirmation email. 
     $conf_subject = 'Registration confirmed'; 

    // Who should the confirmation email be from? 
     $conf_sender = 'PHP Project <[email protected]>'; 

    $msg = $_POST['fname'] . ",\n\nThank you for registering. \n\n You registered for account:".$accounttype."\n\n Your account number:".mysql_insert_id; 

     mail($_POST['email'], $conf_subject, $msg, 'From: ' . $conf_sender); 



    ?> 
</head> 
<body> 


    </br> 
    <form name ="form0" Method="POST" Action="<?PHP echo $_SERVER['PHP_SELF']; ?>"> 
    </br> 
     </br> 
     <b>Select the course you wish to register for:</b></br> 
       <select name="accounttype"> 
        <?PHP query() ?> 

       </select> 
        <?PHP close() ?> 


    </form> 


    <form name ="form1" Method="POST" Action="<?PHP echo $_SERVER['PHP_SELF']; ?>"> 

     </br> 
     </br> 
     <Input type ="" Value = "Surname" Name = "sname"></br> 
     <Input type ="" Value = "First name" Name = "fname"></br> 
     <b>Email:</b> <Input type ="" Value = "" Name = "email"></br> 
     <b>Address:</b> </br> 
     <textarea rows="4" cols="20" Name="address">Please provide your postal address here </textarea></br> 
     <b>Tick to receive confinmation email:</b> <Input type ="checkbox" Value = "1" Name = "contact_flag"></br> 

     <Input type = "Submit" Value="Submit"> 

    </form> 





</body> 
</html> 
+0

爲什麼分配變量之前,兩次? –

+0

由於某種原因,如果我只插入一次數據,數據將不會發送到數據庫。我不斷收到錯誤,指出變量未分配。但是如果你能告訴我我做錯了什麼,我會很樂意刪除額外的腳本! – Jackie69

回答

0

,如果你想堅持到PHP只有一個錯誤,我可以看到是

that you have kept the **validation code below the `INSERT`** 

查詢這意味着插入查詢將被執行的第一其將數據存儲在數據庫中第一,然後它會去確認...... 所以保持INSERT語句上述驗證代碼..第二件事使用exit()方法vaidating每一個領域,如果它給你錯誤...這將停止執行後, PHP的代碼的其餘部分,如果任何字段給出驗證過程中的錯誤....等等它也將防止存儲到數據庫中的數據,如果它發現退出方法每當發現錯誤例如

if(strlen(mysql_real_escape_string($sname))<1) 
         { 
          $errors[]='Your surname is too short!'; 
          echo '//whatever you want to echo'; 
          exit(); 
         } 

         if(strlen(mysql_real_escape_string($fname))<1) 
         { 

          $errors[]='Please insert you full first name'; 
         echo '//whatever you want to echo'; 
         exit(); 
         } 
+0

他正在請求一個PHP驗證......如果瀏覽器老了會發生什麼? – Mauro

+0

非常感謝您解決我的問題!我刪除了一組分配的變量。然後將表單驗證部分向上移動,以便在分配變量之後出現,但在'INSERT'**代碼之前,我按照建議添加了退出方法。但是,現在我得到了以下一大堆「注意:未定義的索引:sname ...」,而其餘的頁面是空白的i.o.w無形式。原因是什麼? (順便說一句,這也保持了以前的狀態,但是一旦我第二次分配變量就停下來。 – Jackie69

+0

首先給你提交的按鈕命名......使用name =「submit」,然後保存所有的php代碼除了包含在if(isset($ _ POST ['submit'])){//將所有php代碼保存在這裏}中的表單中包含的php代碼之外......只有當您提交表單時,此代碼纔會執行....當前代碼的問題在於代碼正在搜索$ _POST ['something'],即使您未提交表單 –

0

首先,在驗證表單之前,您的查詢會被執行。

後您的

echo'<pre>'; 
print_r($errors); 
echo'</pre>'; 

移動你的SQL和環繞它與

if(!$errors){} 

這將防止您的查詢的,如果有任何錯誤執行。 (您也可以刪除你的變量的第一assignement)

關於電子郵件的問題,測試用一個簡單的消息

mail('[email protected]', 'subject', 'message'); 

如果您遇到任何錯誤,可能是你的郵件服務器沒有設置您的連接右

+0

謝謝,我會嘗試查看我是否可以將它工作! – Jackie69

1
<?PHP 
if(isset($_POST['submit'])) 
{ 
    include_once 'includes\functions.php'; 
    connect(); 

// your rest of the code 

     mail($_POST['email'], $conf_subject, $msg, 'From: ' . $conf_sender); 

} 

    ?> 

,並保持該代碼了<html>標籤,但它

+0

謝謝,謝謝你,謝謝!!這是有道理的,我必須承認,我真的開始覺得隧道盡頭有光!我按你的建議做了,現在我有下一個問題。 ,我的網頁上唯一顯示的是下拉菜單,沒有別的,但我會繼續調查g,讓我知道如果我拿出任何東西... – Jackie69

+0

你總是歡迎我的朋友:) –