/    Sign up×
Community /Pin to ProfileBookmark

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:

[code=php]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;
}[/code]

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!!

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 28.2012 — 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 [i]really[/i] being received, e.g.:
[code=php]
function process_form($Email, $EmailOriginal)
{
echo "<pre>DEBUG: '$EmailOriginal'</pre>"; // or error_log() it
[/code]
Copy linkTweet thisAlerts:
@mavigozlerSep 28.2012 — 

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????
[/QUOTE]


Try using [B]unset($tempOriginal)[/B] 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 [B]false[/B]), but undefining the variable should work. Then check your argument to see what its type is: string, empty string, undefined, etc.

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

[code=php]if (empty($tempOriginal) == true)[/code]

and not just

[code=php] if (empty($tempOriginal))[/code]

This explicit code follows a best practice and eliminates confusion and ambiguity.
Copy linkTweet thisAlerts:
@txfamilyof7authorSep 29.2012 — 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:

if (!isset($_POST['Submit'])) {

<i> </i> $sql = "SELECT * FROM NewOrderEmailNotification where RecipientID = 1";
<i> </i> $result = mysql_query($sql);
<i> </i> $row=mysql_fetch_object($result);
<i> </i> $formFields['RecipientEmail'] = $row-&gt;RecipientEmail;
<i> </i> $RecipientEmail = $formFields['RecipientEmail'];
<i> </i> var_dump($RecipientEmail,$formFields['RecipientEmail']);
<i> </i>}
<i> </i>//executes when Submit button is pressed
<i> </i>else if (isset($_POST['Submit'])) {
<i> </i> $Email = $_POST['txtNewOrderEmail'];
<i> </i> $EmailOriginal = $formFields['RecipientEmail'];
<i> </i> $RecipientEmail = process_form($Email, $EmailOriginal);

<i> </i> echo $form;
<i> </i>}


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.
Copy linkTweet thisAlerts:
@txfamilyof7authorSep 29.2012 — 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???
Copy linkTweet thisAlerts:
@NogDogSep 29.2012 — Should this line in the else block...
[code=php]
$EmailOriginal = $formFields['RecipientEmail'];
[/code]
...be this, instead...
[code=php]
$EmailOriginal = $_POST['RecipientEmail'];
[/code]
...?

Or save a bit of typing/processing and just do:
[code=php]
else if (isset($_POST['Submit'])) {
$RecipientEmail = process_form($_POST['txtNewOrderEmail'], $_POST['RecipientEmail']);
echo $form;
}
[/code]
Copy linkTweet thisAlerts:
@txfamilyof7authorSep 29.2012 — 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:

if (!isset($_POST['Submit'])) {

<i> </i> $sql = "SELECT * FROM NewOrderEmailNotification where RecipientID = 1";
<i> </i> $result = mysql_query($sql);
<i> </i> $row=mysql_fetch_object($result);
<i> </i> $formFields['RecipientEmail'] = $row-&gt;RecipientEmail;
<i> </i> $RecipientEmail = $formFields['RecipientEmail'];
<i> </i> var_dump($RecipientEmail,$formFields['RecipientEmail']);
<i> </i>}
<i> </i>//executes when Submit button is pressed
<i> </i>else if (isset($_POST['Submit'])) {
<i> </i> $Email = $_POST['txtNewOrderEmail'];
<i> </i> $EmailOriginal = $formFields['RecipientEmail'];
<i> </i> echo "&lt;pre&gt;DEBUG: '$row-&gt;RecipientEmail'&lt;/pre&gt;"; // or error_log() it
<i> </i> $RecipientEmail = process_form($Email, $EmailOriginal);

<i> </i> echo $form;
<i> </i>}


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!
Copy linkTweet thisAlerts:
@NogDogSep 29.2012 — 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().
×

Success!

Help @txfamilyof7 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...