Log in

View Full Version : newsletter subscribe form - always invalid - regexp



leonidassavvides
07-18-2008, 09:26 AM
http://www.poliscarhire.com/index.php
Email address (Newsletter Subscribute) always invalid at above, I use both reg exp below (first=current, 18july2008) but always invalid like the info@poliscarhire.com ?
what about addresses like : xxx@yyy.com.cy

reg exp
(eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z\.]{2,4}", $email))
(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))

code(current, 18july2008)


<?php
$email = $_POST['email'];

include("../dbinfo.php");
$linkid = @mysql_connect($hostname,$username,$password) or die( "Unable to connect to Database Server. Please try again later.");
@mysql_select_db($database,$linkid) or die( "Unable to select database. Please try again later,"); // @

if ($_POST['unsubscribute']) {
$query = "SELECT * FROM $NewslettersTable";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
$count = @mysql_num_rows($result);
for ($j=0;$j<$count;$j++) {
$row = mysql_fetch_row($result);
if ($email != $row['email']) {
echo "This email address does NOT exist, in the database !";
break;
}
$query = "DELETE FROM $NewslettersTable WHERE email='$email'";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
echo "You have successfully UnSubscribute from our 'Newsletter (Latest Offers)' as: \n". $email;
}
}
else {
if (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z\.]{2,4}", $email)) { // "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
$query = "SELECT * FROM $NewslettersTable";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
$count = @mysql_num_rows($result);
for ($j=0;$j<$count;$j++) {
$row = mysql_fetch_row($result);
if ($email == $row['email']) {
echo "This email address already exist, in the database !";
break;
}
// mysql_free_result($result);
$query = "INSERT INTO $NewslettersTable VALUES(NULL,'$email')";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
echo "You have successfully Subscribute to our 'Newsletter (Latest Offers)' as: \n". $email;
} // end for
} else {
echo "InValid email address. Please retry !";
} // end if
} // end if main
// mysql_free_result($result);
mysql_close($linkid);
?>

leonidassavvides
07-18-2008, 07:49 PM
Please go to
http://www.poliscarhire.com/index.php
and try, it does appear the correct message , what is wrong in the php/mysql code ?

blm126
07-19-2008, 04:23 PM
This doesn't look like a problem in MySQL this is a regex problem. You seem to be using a regular expression without knowing what it is intended to match. If you just want to match most email addresses try this regular expression


preg_match("^.+@[a-zA-Z0-9_]+(.[a-zA-Z0-9])+",$email);

Untested by the way.

leonidassavvides
07-19-2008, 09:56 PM
I found the error , was non correct refererence to html fields of email from php, but the error now is that what ever you write appears null(only title/heading appear) ?

what about addresses like : xxx@yyy.com.cy or xxx@yyy.kk.jj.hh.edu.us how validate ?

<td align="center" valign="middle"><h1 class="text_normal15px">
<?php
$email = $_POST['email-news'];

include("../dbinfo.php");
$linkid = @mysql_connect($hostname,$username,$password) or die( "Unable to connect to Database Server. Please try again later.");
@mysql_select_db($database,$linkid) or die( "Unable to select database. Please try again later,"); // @

if ($_POST['unsubscribute']) {
$query = "SELECT * FROM $NewslettersTable";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
$count = @mysql_num_rows($result);
for ($j=0;$j<$count;$j++) {
$row = mysql_fetch_row($result);
if ($email != $row['email']) {
echo "This email address does NOT exist, in the database !";
break;
}
$query = "DELETE FROM $NewslettersTable WHERE email='$email'";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
echo "You have successfully UnSubscribute from our 'Newsletter (Latest Offers)' as: \n". $email;
}
}
else {
if (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z\.]{2,4}", $email)) { // "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
$query = "SELECT * FROM $NewslettersTable";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
$count = @mysql_num_rows($result);
for ($j=0;$j<$count;$j++) {
$row = mysql_fetch_row($result);
if ($email == $row['email']) {
echo "This email address already exist, in the database !";
break;
}
// mysql_free_result($result);
$query = "INSERT INTO $NewslettersTable VALUES(NULL,'$email')";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
echo "You have successfully Subscribute to our 'Newsletter (Latest Offers)' as: \n". $email;
} // end for
} else {
echo "InValid email address. Please retry !";
} // end if
} // end if main
// mysql_free_result($result);
mysql_close($linkid);
?>
</td>

leonidassavvides
07-20-2008, 12:38 PM
I change script significantly now the only problem when I insert an email address and press "'subscribute" if it already exist in DB:
DOES NOT APPEAR:
"This email address ( $email ) already exist, in the database !"
BUT APPEAR:
""You have successfully Subscribute to our 'Newsletter (Latest Offers)' as: \n". $email"
well ?


<td align="center" valign="middle"><h1 class="text_normal15px">
<?php
$email = $_POST['email-news'];

include("../dbinfo.php");
$linkid = @mysql_connect($hostname,$username,$password) or die( "Unable to connect to Database Server. Please try again later.");
@mysql_select_db($database,$linkid) or die( "Unable to select database. Please try again later,"); // @

if ($_POST['unsubscribute']) {
$query = "SELECT * FROM $NewslettersTable";
$result = @mysql_query($query,$linkid) or die("Unable to execute query. Please try again later.");
$count = @mysql_num_rows($result);
// echo $count;
$f = "n";
for ($j=0;$j<$count;$j++) {
$row = mysql_fetch_row($result);
if ($email == $row[1]) { // "info2@poliscarhire.com"
$f = "y";
break;
}
}
if ($f != "y") {
echo "This email address ( $email ) does NOT exist, in the database !";
} else {
$query = "DELETE FROM $NewslettersTable WHERE email='$email'";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
echo "You have successfully UnSubscribute from our 'Newsletter (Latest Offers)' as: \n". $email;
}
}
else {
if (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z\.]{2,4}", $email)) { // "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"
$query = "SELECT * FROM $NewslettersTable";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
$count = @mysql_num_rows($result);
$f2 = "n";
if ($count > 0) {
for ($j=0;$j<$count;$j++) {
$row = mysql_fetch_row($result);
if ($email == $row[1]) { // 'email'
echo "This email address ( $email ) already exist, in the database !";
$f2 = "y";
break;
}
} // end for
} // end if
// mysql_free_result($result);
if ($f2 == "n") {
$query = "INSERT INTO $NewslettersTable VALUES(NULL,'$email')";
$result = @mysql_query($query,$linkid) or die( "Unable to execute query. Please try again later.");
echo "You have successfully Subscribute to our 'Newsletter (Latest Offers)' as: \n". $email;
}
} else {
echo "InValid email address. Please retry !";
} // end if
} // end if main
// mysql_free_result($result);
mysql_close($linkid);
?></h1>