Hi. I'm developing a Mailing List and am building in a feature where the script checks for an e-mail address in the database and displays an error message if that e-mail address is already in it. I can get this to work using the following code, which I have put at the top of the code before the <html> tag:
<?php require_once('../Connections/connMailing.php'); ?>
<?php mysql_select_db($database_connMailing, $connMailing);
$query_rsInsertSubscriber = "SELECT * FROM ml_users ORDER BY user_id ASC";
$rsInsertSubscriber = mysql_query($query_rsInsertSubscriber, $connMailing) or die(mysql_error());
$row_rsInsertSubscriber = mysql_fetch_assoc($rsInsertSubscriber);
$totalRows_rsInsertSubscriber = mysql_num_rows($rsInsertSubscriber);
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . date("Y-m-d",strtotime($theValue)) . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $SERVER['PHP_SELF'];
if (isset($SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($POST["MM_insert"])) && ($POST["MM_insert"] == "frmSubscribe")) {
$insertSQL = sprintf("INSERT INTO ml_users (firstname, lastname, email, country, referral) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($POST['firstname'], "text"),
GetSQLValueString($POST['lastname'], "text"),
GetSQLValueString($POST['email'], "text"),
GetSQLValueString($POST['Country'], "text"),
GetSQLValueString($_POST['referral'], "text"));
mysql_select_db($database_connMailing, $connMailing);
$email_check=mysql_query("SELECT email FROM ml_users WHERE email='$_POST[email]'");
if(mysql_num_rows($email_check)>=1) {
print("This e-mail address has already been subscribed. Please contact us if you believe this to be incorrect.");
} else {
$Result1 = mysql_query($insertSQL, $connMailing) or die(mysql_error());
header("Location: thank_you.php");
exit;
}
}
?>
This is fine and displays the error message: "This e-mail address has already been subscribed. Please contact us if you believe this to be incorrect." But it displays it at the top of the page. What I want it to do is display it just above the subscribe form. I tried putting the PHP Code above inside the form and it works OK, displays the error message and then you can try again. But when someone actually does sign up with a correct e-mail address it says that the headers have already been sent as it tries to display the 'Thank You' page. It still adds the e-mail address to the database but it just doesn't look very good.
How do I modify this so that the error message displays where I want it to?
Hope someone can help me out.