2012-01-23 19 views
0

以下代碼供用戶通過此表單提交電子郵件和郵編。它不檢查數據或將數據插入到數據庫中,也不會正確顯示任何錯誤消息。似乎無法得到這個簡單的PHP電子郵件/郵編表單提交工作

<?php 

// If the form submit button is set and the email and zip fields are not empty, proceed and process 
if(isset($_POST['submit']) && !empty($_POST['email']) && !empty($_POST['zip'])) 
{ 
    // Create variables for form input fields 
    $email = $_POST['email']; 
    $zip = $_POST['zip']; 

    // Create an array to capture errors 
    $errors = array(); 

    // Create variable to capture success message 
    $success = "Thanks for signing up!"; 

    // Email Validation 
    // Check to see if user entered a valid email 
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
    { 
    $errors[] = "Invalid email address."; 
    } 
    // Check email length 
    if(strlen($email) < 6) 
    { 
    $errors[] = "Invalid email address."; 
    } 
    // Check email length 
    if(strlen($email) > 50) 
    { 
    $errors[] = "Invalid email address."; 
    } 

    // Zip Code Validation 
    // Check to see if zip code is a number 
    if((!is_numeric($zip) || strlen($zip) != 5)) 
    { 
    $errors[] = "Invalid zip code."; 
    } 

    // Include database config file and establish db connection 
    require("includes/config.php"); 
    mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die("Database Connection Error"); 
    mysql_select_db(DB_NAME) or die("No Database Found"); 

    // Check to see if email already exists in database 
    $email_check_query  = "SELECT email FROM datingshotgun WHERE email ='$email'"; 
    $run_email_check_query = mysql_query($email_check_query); 

    // If MySQL query returns any results, user has already signed up and the script will end 
    if(mysql_num_rows($run_email_check_query) != 0) 
    { 
    $errors[] = "Looks like you already signed up..."; 
    } 

    // If there are no errors above run this block of code 
    if(count($errors) == 0) 
    { 
    // Include database config file and establish db connection 
    require("includes/config.php"); 
    mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die("Database Connection Error"); 
    mysql_select_db(DB_NAME) or die("No Database Found"); 

    // Insert email and password into database 
    $insert_email_query  = "INSERT INTO datingshotgun (email,zip) VALUES ('$email','$zip')"; 
    $run_insert_email_query = mysql_query($insert_email_query); 
    } 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="UTF-8" /> 
    <title>DatingShotgun.com</title> 
    <link rel="stylesheet" href="css/styles.css" /> 
    <!-- TypeKit --> 
    <script type="text/javascript" src="http://use.typekit.com/mtx2hld.js"></script> 
    <script type="text/javascript">try { 
     Typekit.load(); 
    } catch (e) { 
    }</script> 
    <!-- jQuery --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <!-- Custom Script --> 
    <script src="js/scripts.js"></script> 

    </head> 
    <body> 
    <header> 
     <div class="logo"> 
     <h1>Dating Shotgun</h1> 
     </div> 
    </header> 
    <div class="content"> 
     <div class="comingsoon"><p class="comingsoon_image"></p></div> 
     <h1>Sign Up Now</h1> 

     <p class="description">Be the first to receive a weekly dose of eligible<br />bachelors handpicked by two girls on 
          the prowl.</p> 

     <form action="index.php" method="post"> 
     <input type="email" class="email" name="email" maxlength="50" placeholder="Email Address"> 
     <input type="text" class="zip" name="zip" maxlength="5" placeholder="Zip Code"> 
     <input type="submit" class="submit" name="submit" value="Submit"> 

     <p class="errors"> 
      <?php 
      if(count($errors) != 0) 
      { 
      foreach($errors as $error) 
      { 
       echo $error . "<br />"; 
      } 
      } 
      else 
      { 
      echo $success; 
      } 
      ?> 
     </p> 
     </form> 
    </div> 
    <footer> 
     <p class="line"></p> 
     <a href="http://flirtexting.com/" title="Flirtexting"></a> 
    </footer> 
    </body> 
</html> 
+0

我試着複製並粘貼你的腳本,註釋掉數據庫部分 - 所有的錯誤信息都對我完全出來。你有沒有得到任何輸出?或者它只是你看到的同一頁?我們需要更多的幫助。 – Repox

+0

我沒有任何輸出,頁面只是刷新。 –

+0

我相信我的db連接文件config.php是正確的。 –

回答

0

「;」的力量... :)

改變這一行:

$email_check_query  = "SELECT email FROM datingshotgun WHERE email ='$email'"; 

到:

$email_check_query  = "SELECT email FROM datingshotgun WHERE email ='$email';"; 

同樣適用於:

$insert_email_query  = "INSERT INTO datingshotgun (email,zip) VALUES ('$email','$zip')"; 

到:

$insert_email_query  = "INSERT INTO datingshotgun (email,zip) VALUES ('$email','$zip');"; 
0

你知道嗎,我知道了......出於某種原因,我的數據庫中的id字段未設置爲自動遞增。愚蠢的我,這個伎倆。

相關問題