简体   繁体   English

使我的PHP表单成功查询数据库以检查并查看电子邮件是否存在的问题

[英]Problems getting my PHP form to successfully query database to check and see if email already exists

For some reason my form is not checking the database to see if the email already exists. 由于某种原因,我的表单没有检查数据库以查看电子邮件是否已经存在。 Are you able to identify anything wrong with my code? 您能识别出我的代码有什么问题吗?

// 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[] = "Sorry your email is too short.";
    }
    // Check email length
    if (strlen($email) > 50) {
        $errors[] = "Sorry your email is too long.";
    }

    // Zip Code Validation
    // Check to see if zip code is a number
    if (!is_numeric($zip)) {
        $errors[] = "Zip code must be a number.";
    }
    // Check to see if zip code equals 5 characters
    if (strlen($zip) != 5) {
        $errors[] = "Sorry not a valid zip code.";
    }

    // Include database config file and establish db connection
    require("includes/config.php");
    $connection = mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD) or die("Database Connection Error");
    $database = 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 shotgun";
    $run_email_check_query = mysql_query($email_check_query);

    // If MySQL query returns any results, user has already signed up
    if (mysql_fetch_assoc($run_email_check_query) == $email) {
        $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");
        $connection = mysql_connect(DB_HOST,DB_USERNAME,DB_PASSWORD) or die("Database Connection Error");
        $database = mysql_select_db(DB_NAME) or die("No Database Found");

        // Insert email and password into database
        $insert_email_query = "INSERT INTO shotgun (email,zip) VALUES ('$email','$zip')";
        $run_insert_email_query = mysql_query($insert_email_query);
    }   
} 
?>

Site 现场

</head>
<body>
    <header>
        <div class="logo">
            <h1>Site</h1>
        </div>
    </header>
    <div class="content">
        <div class="comingsoon"></div>
        <h1>Sign Up Now</h1>
        <p class="description">Description</p>
        <form action="index.php" method="post">
            <input type="email" class="email" name="email" placeholder="Email Address">
            <input type="text" class="zip" name="zip" max="5" placeholder="Zip Code">
            <input type="submit" class="submit" name="submit" value="Submit">
            <span class="errors">
            <?php 
                if (count($errors) != 0) { 
                    foreach($errors as $error) { 
                        echo $error . "<br />";
                    }
                } else {
                    echo $success; 
                }
            ?>
            </span>
        </form>
    </div>
    <footer><a href="test.com" title="test"></a></footer>
</body>

$email_check_query = "SELECT email FROM datingshotgun";

should be 应该

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

Right now you allways query ALL emails and compare the new email to the first in the DB. 现在,您始终查询所有电子邮件,并将新电子邮件与数据库中的第一封电子邮件进行比较。

EDIT: After tracking your code, you need: 编辑:跟踪您的代码后,您需要:

$dbemail=mysql_real_escape_string($email, $connection);
$email_check_query = "SELECT email FROM datingshotgun WHERE email='$dbemail'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM