0

我正在 PHP 中创建一个电子邮件订阅表单,并希望检查有效地址以及电子邮件是否已存在于我的数据库中。

我的代码正在连接到我的数据库并插入,但验证以及检查现有电子邮件不起作用。

无论我在表单中输入什么内容,即使我没有输入任何内容,它也会将其插入到我的数据库中。

这是我的所有代码:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset>

<legend>Subscribe to Our Newsletter &nbsp;</legend>

<?php if ($feedback!='')
echo('<p>'.$feedback.'</p>'); ?>

<label>Name: <input name="name" type="text" value="<?php echo $name; ?>" /></label>

<label>Email: <input name="email" type="text" value="<?php echo $email; ?>" /></label> 

<label><input type="submit" value="Sign Up!" /></label>

</fieldset>
</form>

<?php



$feedback='';
if (!$email) {

$feedback .= '<strong>Please enter your email address</strong><br />';

}

if (!$name) {

$feedback .= '<strong>Please enter your name</strong><br />';
}



list($username, $mailDomain) = explode("@", $email);

if (!@checkdnsrr($mailDomain, "MX")) {


$feedback .= '<strong>Invalid email domain</strong><br />';
}


if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)) {


$feedback .= '<strong>Your email address doesn\'t appear to be valid - please check and try again';

}



function cleaninput($value, $DB) {
    if (get_magic_quotes_gpc()) {
        $value = stripslashes( $value );
}
return mysql_real_escape_string( $value, $DB );

}


$name=$_POST['name'];
$email=$_POST['email'];

include_once "connect.php";


$sql = mysql_query("SELECT * FROM subscribers WHERE email='$email'");
$numRows = mysql_num_rows($sql);
    if ($numRows>0) {


$feedback = '<strong>That email address is already subscribed.</strong>';

}

$insertresult = mysql_query("INSERT INTO subscribers (name, email) VALUES('$name', '$email')") or die (mysql_error());


    if ($insertresult) {
        $completed = true;
    }



if($competed=false) {
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?> method="post">
        <fieldset>
    <legend>Subscribe to OUr Newsletter &nbsp;</legend>
<?php 

if ($feedback!='')
    echo('<p>'.$feedback.'</p>'); ?>
<label>Name: <input name="name" type="text" value="<?php echo $name; ?>" /></label>

<label>Email: <input name="email" type="text" value="<?php echo $email; ?>" /></label>

<label><input type="submit" value="Sign Up!" /></label>

        </fieldset>
</form>

<?php

}
else {



echo('Thanks - you have subscribed to our newsletter successfully. You can unsubscribe at any time by clicking the link at the bottom of each email we send.');

}



?>

我脚本中的最后一个回声也总是在那里。它总是显示在我的表单下。不知道为什么会这样。也许我把它放在我的代码中的错误位置。

else {



echo('Thanks - you have subscribed to our newsletter successfully. You can unsubscribe at any time by clicking the link at the bottom of each email we send.');

}

谢谢!

4

1 回答 1

0

老实说,这段代码有点乱 :) 读起来有点困难,但我至少可以看到两个问题:你写$competed而不是$completed在你的一个if语句中,而且你实际上没有INSERT查询if块:它将始终执行。尝试将其放在检查地址是否已在数据库中的else块之后的块中,如下所示:if

$sql = mysql_query("SELECT * FROM subscribers WHERE email='$email'");
$numRows = mysql_num_rows($sql);

if ($numRows>0) {
  $feedback = '<strong>That email address is already subscribed.</strong>';
}
else { 
  $insertresult = mysql_query("INSERT INTO subscribers (name, email) VALUES('$name',   '$email')") or die (mysql_error());
}

您也不需要同时使用addslashesand mysql_real_escape_string; 只有后者会做。而且我不确定为什么您的代码中有两次相同的形式。肯定一次应该做吗?:)

于 2012-05-13T19:56:13.607 回答