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正直なところ、このコードは少しごちゃごちゃしています :) 読むのは少し難しいですが、少なくとも 2 つの問題が見られ$completedます。ブロック: 常に実行されます。次のように、アドレスが既にデータベースにあるかどうかをチェックするブロックの後に、ブロックに入れてみてください。ifINSERTifelseif

$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());
}

addslashesmysql_real_escape_string;の両方を使用する必要もありません。後者だけで十分です。また、コードに同じフォームが 2 回含まれている理由がわかりません。きっと一度やるべき?:)

于 2012-05-13T19:56:13.607 に答える