php - 让我的 PHP 表单成功查询数据库以检查电子邮件是否已存在时出现问题

标签 php mysql sql

由于某种原因,我的表单没有检查数据库以查看电子邮件是否已存在。您能找出我的代码有什么问题吗?

// 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);
    }   
} 
?>

地点

</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";

应该是

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

现在,您总是查询所有电子邮件并将新电子邮件与数据库中的第一封电子邮件进行比较。

编辑:跟踪代码后,您需要:

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

关于php - 让我的 PHP 表单成功查询数据库以检查电子邮件是否已存在时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928587/

相关文章:

php - 仅显示父自定义分类名称?

php - 如何返回 pdf 对象而不是使用 fpdf 保存

javascript - 完整日历的数组

php - 计算列中的值并生成结果表 mysql

sql - SYSDATE 但指定时间

php - 如何在 PHP 中匹配除 "-"以外的所有特殊字符的正则表达式?

mysql - SQLException 捕获 : Column count doesn't match value count at row

javascript - 将 Auth0 身份验证与现有用户数据库集成

mysql - SQL正则表达式过滤器

mysql - 将 %keyword% 转换为匹配(反对)