www.webdeveloper.com
Results 1 to 8 of 8

Thread: Help with Comparisons in If/Else Statements Updating Database

  1. #1
    Join Date
    Jul 2012
    Posts
    14

    Angry Help with Comparisons in If/Else Statements Updating Database

    Good afternoon!

    I have spent approximately 15 hours researching this, and tried every solution I could find, and I still don't know what I'm doing wrong. Initially, I was getting SQL errors and blank pages, and empty forms...but I've fixed all of that. So, on to the problem that has been plaguing me for what seems like forever!

    I have a function process_form($Email, $EmailOriginal). I want it to compare the values, and if it was originally empty, insert the value into the database. If the form field originally had data, it should update the database to reflect the new value. Unfortunately, all it does is go through the FIRST "if" statement and adds the data to the database whether the original value was blank or not. Here's the function code:

    PHP Code:
    function process_form($Email$EmailOriginal)
        {
            
    $tempEmail $Email;
            
    $tempOriginal $EmailOriginal;
            
    $tempRecipient $RecipientEmail;
            if (empty(
    $tempOriginal))
            {
                
    $sql "insert into NewOrderEmailNotification (RecipientEmail) values ('$tempEmail')";
                if (!
    mysql_query($sql))
                {
                    die(
    'Error: A' mysqli_error());
                  }
                echo 
    "1 record added";    
            }
            else if (
    $tempOriginal != $tempEmail)
            {
                
    $sql "update NewOrderEmailNotification set 'RecipientEmail' to '$tempEmail' where 'RecipientID' = 1";
                if (!
    mysql_query($sql))
                {
                    die(
    'Error: B' mysqli_error());
                }
                echo 
    "Email successfully updated.";
            }
            
    $tempRecipient $tempEmail;
            return 
    $tempRecipient;
        } 
    I have tried using "if ($tempOriginal != "")", "if ($tempOriginal != NULL)", "if (is_null($tempOriginal))" and none of them have worked either.

    Can anyone tell me why the function is always going into the first "if" statement, even though the variable has a value????

    TIA!!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    My guess is that the real "bug" is in the code calling the function, such that you are getting an empty value for the 2nd arg. Try sticking some debug code in there to find out what is really being received, e.g.:
    PHP Code:
    function process_form($Email$EmailOriginal)
    {
        echo 
    "<pre>DEBUG: '$EmailOriginal'</pre>"// or error_log() it 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2009
    Location
    Miami
    Posts
    118
    Quote Originally Posted by txfamilyof7 View Post

    I have tried using "if ($tempOiginal != "")", "if ($tempOriginal != NULL)", "if (is_null($tempOriginal))" and none of them have worked either.
    Can anyone tell me why the function is always going into the first "if" statement, even though the variable has a value????
    Try using unset($tempOriginal) just prior to the if block to make sure it is "empty." I realize there are lot of other tests (including setting $tempOriginal to the boolean false), but undefining the variable should work. Then check your argument to see what its type is: string, empty string, undefined, etc.

    Another thing, empty() returns a boolean, and so you should explicitly write the boolean test:

    PHP Code:
    if (empty($tempOriginal) == true
    and not just

    PHP Code:
     if (empty($tempOriginal)) 
    This explicit code follows a best practice and eliminates confusion and ambiguity.

  4. #4
    Join Date
    Jul 2012
    Posts
    14
    Thank you so much for your responses!!! I truly appreciate it. :-)

    I did your suggestion, and put in the debug code. The result was: DEBUG: '', which I assume means that the variable is, in fact, null.


    Here's the code where I call the function I mentioned above:

    Code:
    if (!isset($_POST['Submit'])) {
    		
    		$sql = "SELECT * FROM NewOrderEmailNotification where RecipientID = 1";
    		$result = mysql_query($sql);
    		$row=mysql_fetch_object($result);
    		$formFields['RecipientEmail'] = $row->RecipientEmail;
    			$RecipientEmail = $formFields['RecipientEmail'];
    			var_dump($RecipientEmail,$formFields['RecipientEmail']);
    	}
    	//executes when Submit button is pressed
    	else if (isset($_POST['Submit'])) {
    		$Email = $_POST['txtNewOrderEmail'];
    		$EmailOriginal = $formFields['RecipientEmail'];
    		$RecipientEmail = process_form($Email, $EmailOriginal);
    
    		echo $form;
    	}
    As you can see, I did a var_dump of both $RecipientEmail and $formFields['RecipientEmail'], and at that point neither is null and both have the proper values. $formFields[] is an array that was created to hold the original values of the form fields.

  5. #5
    Join Date
    Jul 2012
    Posts
    14
    UPDATE:

    I put a echo "<pre>DEBUG: '$Title'</pre>"; statement at the beginning of the "while" statement, and nothing got printed out...does this mean it isn't going in the "while" loop at all???

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    Should this line in the else block...
    PHP Code:
    $EmailOriginal $formFields['RecipientEmail']; 
    ...be this, instead...
    PHP Code:
    $EmailOriginal $_POST['RecipientEmail']; 
    ...?

    Or save a bit of typing/processing and just do:
    PHP Code:
        else if (isset($_POST['Submit'])) {
            
    $RecipientEmail process_form($_POST['txtNewOrderEmail'], $_POST['RecipientEmail']);
            echo 
    $form;
        } 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jul 2012
    Posts
    14
    Thank you NogDog,

    Unfortunately, that would give me the wrong values. Only one of them is supposed to be the $_POST value; the other one needs to be the original value in the field so I can compare them. So, since the value showed up in the DEBUG as "", I suppose that's where my problem is. :-(

    I declare the variable in the top of the PHP document, I thought that would make it global so I can access it anywhere in the document, regardless of function. I assign the value in the previous "if" statement. so it went like this:

    Code:
    if (!isset($_POST['Submit'])) {
    		
    		$sql = "SELECT * FROM NewOrderEmailNotification where RecipientID = 1";
    		$result = mysql_query($sql);
    		$row=mysql_fetch_object($result);
    		$formFields['RecipientEmail'] = $row->RecipientEmail;
    			$RecipientEmail = $formFields['RecipientEmail'];
    			var_dump($RecipientEmail,$formFields['RecipientEmail']);
    	}
    	//executes when Submit button is pressed
    	else if (isset($_POST['Submit'])) {
    		$Email = $_POST['txtNewOrderEmail'];
    		$EmailOriginal = $formFields['RecipientEmail'];
    		echo "<pre>DEBUG: '$row->RecipientEmail'</pre>"; // or error_log() it
    		$RecipientEmail = process_form($Email, $EmailOriginal);
    
    		echo $form;
    	}
    Unfortunately, it seems that in the second "if" statement, the array is not holding the value I assigned to it in the first if. Is there a way around this?? Do I just need to do the SELECT statement again to get the values from the database again??

    Thanks!

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    Well, the elseif() will only get executed if the first if()'s evaluation fails, so none of that code in the if() will have been executed if you end up in the ifelse().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles