...

View Full Version : Resolved do these nested IFs make any sense



low tech
11-07-2010, 04:34 AM
Hi all

I'm trying to validate against paypal isent nfo BUT as usual I'm lost in my logic and if statements.

Does this make any sense?

How far out am I??

Any help greatly welcomed. Do I need to rearrange?

LT



if (strcmp ($res, "VERIFIED") == 0) {

// check the payment_status is Completed
// WHAT TO DO IF ONE OF THESE IS INCORRECT???? error notify??
if (($payment_status == ‘Completed’) &&
($receiver_email == $mypaypalemail) && // receiver_email === yr account email
($mc_gross == $amount_required ) && //payment === yr reqd payment
($mc_currency == $mycurrency)) {

///////////////////////////////////////////////////////////////
//create MySQL connection
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());

//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
//////////////////////////////////

//check if transaction ID has been processed before
// WHAT TO DO IF PROCESSED BEFORE???? error notify??
$checkquery = "SELECT txn_id FROM users WHERE txn_id='".$txn_id."'";
$result = mysql_query($checkquery) or die("Duplicate txn id check query failed:<br>" . mysql_error() . "<br>" . // mysql_errno());
$nm = mysql_num_rows($result);
if ($nm == 0) {
mysql_query("INSERT INTO users (CODE);

//////////////////////////////////////
// PAYMENT VALIDATED & VERIFIED!

$mail_From = "From: me@mybiz.com";
// all mail details
mail($mail_To, $mail_Subject, $message . "\n\n" . $mail_From, $headers);
} else{
// notify myself??
}
}else {
// error notify person and myself ??
}
}

else if (strcmp ($res, "INVALID") == 0)

Keleth
11-07-2010, 04:38 AM
And this is why colors are good :)


mysql_query("INSERT INTO users (CODE);

Is it safe to assume CODE is actually something else? Because if not, that's a big bug right there... plus, the quote isn't closed nor the function closed. Also, any reason you put every comparison in parens? Its not necessary, and I know for myself, only muddles the issues at hand.

low tech
11-07-2010, 05:15 AM
Hi Keleth
Thanks fo reply


Is it safe to assume CODE is actually something else?

Yep this part of code is done tried and tested --- so I just cut it short here --- no need to waste time to look at it.

Its all the part above it that I'm trying to sort out ---- I don't know if my logic is ok and if my nesting is correct ---


Also, any reason you put every comparison in parens?

No reason other than I'm new.

Are my 'else' positions in the right place ti deal with error if something returns false --- ie if the check id returns duplicate --- then I should jump to error right???

LT

Keleth
11-07-2010, 05:23 AM
As far as I can tell, yah, the nesting looks good. You indented, which is a good way to make sure nesting is working properly. The only thing I'd say to do, at the end you have "else if". Change that to "elseif". By doing "else if", you're summoning two commands... else and if. By doing "elseif", you do one command. They do the same thing really, and it won't affect page load time unless you have thousands upon thousands of lines of code, but its just good habit.

I guess if you're just looking for advice on your code, any reason you have "nn" in the middle of your mail code?

low tech
11-07-2010, 05:37 AM
Thanks Keleth

nice feedback

I'll chenge the else if to elseif ---- never knew there was a difference -- although from looking at other scripts I had noticed the use --- just figured must be same either way.

and the nn in original script is backslash n ----- new line yeh? It works when mail is sent anyway.

So if any "if" conditio(s) fail --- then I should write what to do in the appropiate corrosponding "else" position ---- ie send mail either to myself and or person making payment to noify error???

my first time to try anything like this, so sorry for stupid questions.

LT

Keleth
11-07-2010, 06:36 AM
Yah, if the "if" is essential to the flow, then you should have an else, which both feeds out an error to the user and to yourself (by whatever way you want, a mail to you, store to a file, etc).

low tech
11-07-2010, 07:33 AM
Hi Keleth

thanks of rall the help so far --- magic


I know I can do this

example:


$mail_From = "From: me@mybiz.com";
$mail_To = "admin@englishfreaks.com";
$mail_Subject = "VERIFIED IPN";

$message = "

Thank you for registering ";

$headers = 'From:xyz@abc.com' . "\r\n";
mail($mail_To, $mail_Subject, $message . "\n\n" . $mail_From, $headers);
}
BUT I have this var set earlier
$notify_email = "admin@mysite.com";

so can I do this in my else part


else {
// DUPLICATE: error notify
mail($notify_email, "VERIFIED DUPLICATED TRANSACTION", "$user_email\n $result\n $payment_status\n $receiver_email\n $mc_gross\n $mc_currency");
}

IF I can thenI also want to send a mail to user--- do I just make two?


mail ($notify_email, abc, etc etc );
mail ($user_email, abc, have a message in a var and put var here?? );

LT

poyzn
11-07-2010, 08:00 AM
IF I can thenI also want to send a mail to user--- do I just make two?


mail ($notify_email, abc, etc etc );
mail ($user_email, abc, have a message in a var and put var here?? );


Yes, you can run any number of mail function.

low tech
11-07-2010, 08:05 AM
Hi Poyzn

excellent

time for the big test then:-)

IS this idea correct then --- should I store the body massage in a var and place as in code???


mail ($user_email, abc, have a message in a var and put var here?? );

LT
ps
Just thinking how the format will look from users end

for myself is ok.

poyzn
11-07-2010, 08:17 AM
You can even store your messages in html-rich format. Like this:


$message = '<h1>Hello,</h1><p>Thank you for making a donation ...</p><p>LT</p>';

but then you should pass next header to mail function:


$headers .= 'Content-Type: text/html; charset=utf-8'."\r\n" // set your charset
mail($to, $subject, $message, $headers);

low tech
11-07-2010, 08:23 AM
ahahaha

Yeh cool ---

i'll try that once i get past my current testing point --

magic:-)

LT



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum