似乎无法将这个简单的PHP电子邮件/ zip表单提交给工作

时间:2012-01-23 14:29:01

标签: php jquery mysql sql

以下代码供用户提交电子邮件并压缩此表单。它没有检查数据或将数据插入数据库,也没有正确显示任何错误消息。

<?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>

2 个答案:

答案 0 :(得分: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');";

答案 1 :(得分:0)

你知道吗,我想出来......由于某种原因,我的数据库中的id字段没有设置为自动递增。愚蠢的我,这就行了。