Undefined variable error

Discussion in 'PHP' started by gwh, Apr 7, 2008.

  1. #1
    Hi everyone,

    I've got a form on the home page of a site that allows people to subscribe to a newsletter. There are 2 input boxes and a subscribe button on the form.

    I've included the php code below that I've included on the page. I'm getting an error though when I try to click the subscribe button to insert the record into the table. The error message is as follows:

    Notice: Undefined variable: alreadyRegistered in /Applications/MAMP/htdocs/newsite/templates/index.dwt.php on line 171

    It relates to the following section of code:

    <?php
    if ($_POST && $alreadyRegistered) {
    echo '<span class="warning">The email address '.$_POST['email'].' is already subscribed.</span>';
    }
    ?>
    
    PHP:
    I wonder if someone could tell me what I might have done wrong?

    Appreciate any help offered.



    <?php require_once('../Connections/hthAdmin.php'); ?>
    <?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 != "") ? "'" . $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']);
    }
    $var1_checkEmail = "Dream";
    if (isset($_POST['email'])) {
      $var1_checkEmail = (get_magic_quotes_gpc()) ? $_POST['email'] : addslashes($_POST['email']);
    }
    mysql_select_db($database_hthAdmin, $hthAdmin);
    $query_checkEmail = sprintf("SELECT * FROM newsletter WHERE newsletter.email = '%s'", $var1_checkEmail);
    $checkEmail = mysql_query($query_checkEmail, $hthAdmin) or die(mysql_error());
    $row_checkEmail = mysql_fetch_assoc($checkEmail);
    $totalRows_checkEmail = mysql_num_rows($checkEmail);
    
    
    // assume that no match has been found
    $recordsExist = false;
    
    // check whether recordset found any matches
    if ($totalRows_checkForeign > 0) {
      $recordsExist  = true;
      }
    else {
      // go ahead with Delete Record server behavior
    
    
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertSubscriber")) {
      $insertSQL = sprintf("INSERT INTO newsletter (name, email) VALUES (%s, %s)",
                           GetSQLValueString($_POST['Name'], "text"),
                           GetSQLValueString($_POST['Email'], "text"));
    
      mysql_select_db($database_hthAdmin, $hthAdmin);
      $Result1 = mysql_query($insertSQL, $hthAdmin) or die(mysql_error());
    	}
    }
    PHP:
     
    gwh, Apr 7, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It means that the variable doesn't exist. In other words, you didn't define it (give it a value) before using it in that if statement.
     
    CreativeClans, Apr 7, 2008 IP
  3. gwh

    gwh Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #3
    Thanks,

    I replaced this block of code:

    // assume that no match has been found
    $recordsExist = false;
    
    // check whether recordset found any matches
    if ($totalRows_checkForeign > 0) {
      $recordsExist  = true;
      }
    else {
      // go ahead with Delete Record server behavior
    
    PHP:
    with this:

    // assume that no match has been found
    $alreadyRegistered = false;
    
    // check whether recordset found any matches
    if ($totalRows_checkEmail > 0) {
      $alreadyRegistered  = true;
      }
    else {
      // go ahead with Insert Record server behavior
    
    PHP:
    It works now by inserting the entry into the table but the error checking doesn't seem to be working, ie. I inserted an email address into the table and then tried reinserting it, hoping to get the error alert but the duplicate just continues to be inserted into the table.

    The previous and following code blocks don't seem to be working:

    <?php
    if ($_POST && $alreadyRegistered) {
    echo '<span class="warning">The email address '.$_POST['email'].' is already subscribed.</span>';
    }
    ?>
    
    PHP:
    Do you know why this is happening?
     
    gwh, Apr 7, 2008 IP