should this PHP if else statement work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

should this PHP if else statement work

Post by jonnyfortis »

I have a if else statement below based on if a certain variable is passed from an IPN . i would just test it but the site is live and wanted a heads up before i upload it.

i want if the $custom is empty to run the first statement if it is not empty run the second statement

Code: Select all

$itemNumber = $_POST['item_number'];
$paymentStatus = $_POST['payment_status'];
$payerStatus = $_POST['payer_status'];
$pendingReason = $_POST['pending_reason'];
$txnID = $_POST['txn_id'];
$receiverEmail = $_POST['receiver_email'];
$custom = $_POST['custom'];

if ($custom == "")
{

// update orders table
if ($itemNumber != "" && $txnID != "" && $paymentStatus != "") {
  mysql_select_db($database_sweep, $sweep);
  $IPN_upd = "UPDATE sweep_orders SET Fulfilled = " . $status . ", TransactID = '" . $txnID . "', TransactResult = '" . $paymentStatus . "', ResponseMsg = '" . $errmsg . "' WHERE OrderID = " . $itemNumber ;
  $IPN_rsId = mysql_query($IPN_upd, $sweep) or die(mysql_error());
  $IPN_rsId = null;
}
} else {
	// update orders table
if ($custom != "" && $txnID != "" && $paymentStatus != "") {
  mysql_select_db($database_sweep, $sweep);
  $IPN_upd = "UPDATE sweep_orders SET Fulfilled = " . $status . ", TransactID = '" . $txnID . "', TransactResult = '" . $paymentStatus . "', ResponseMsg = '" . $errmsg . "' WHERE OrderID = " . $custom ;
  $IPN_rsId = mysql_query($IPN_upd, $sweep) or die(mysql_error());
  $IPN_rsId = null;
}
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: should this PHP if else statement work

Post by Celauran »

Unnecessarily verbose, but looks like it should work. Why not test it in a development environment?
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: should this PHP if else statement work

Post by jonnyfortis »

Celauran wrote:Unnecessarily verbose, but looks like it should work. Why not test it in a development environment?
verbose. good word. yes i think i will thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: should this PHP if else statement work

Post by Christopher »

Indenting would make it clearer. And you should use the filter functions to filter your Request values. Don't use the mysql_ library, use the mysqli_ library.

I'd also recommend reducing repetition whenever possible. That way, changes can happen to one piece of duplicated code and not another.

Code: Select all


$sql = '';
if ($custom == "")
{
	// update orders table
	if ($itemNumber != "" && $txnID != "" && $paymentStatus != "") {
		$sql= "UPDATE sweep_orders SET Fulfilled = " . $status . ", TransactID = '" . $txnID . "', TransactResult = '" . $paymentStatus . "', ResponseMsg = '" . $errmsg . "' WHERE OrderID = " . $itemNumber ;
	}
} else {
	// update orders table
	if ($custom != "" && $txnID != "" && $paymentStatus != "") {
		$sql= "UPDATE sweep_orders SET Fulfilled = " . $status . ", TransactID = '" . $txnID . "', TransactResult = '" . $paymentStatus . "', ResponseMsg = '" . $errmsg . "' WHERE OrderID = " . $custom ;
	}
}
if ($sql) {
	mysql_select_db($database_sweep, $sweep);
	$IPN_rsId = mysql_query($sql, $sweep) or die(mysql_error());
	$IPN_rsId = null;

	// fetch data records
} else {
	// communicate the error
}
(#10850)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: should this PHP if else statement work

Post by Celauran »

Indeed, if you were using something like PDO, you'd only need a single query and could swap the value for one placeholder as appropriate.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: should this PHP if else statement work

Post by jonnyfortis »

Christopher wrote:Indenting would make it clearer. And you should use the filter functions to filter your Request values. Don't use the mysql_ library, use the mysqli_ library.

I'd also recommend reducing repetition whenever possible. That way, changes can happen to one piece of duplicated code and not another.

Code: Select all


$sql = '';
if ($custom == "")
{
	// update orders table
	if ($itemNumber != "" && $txnID != "" && $paymentStatus != "") {
		$sql= "UPDATE sweep_orders SET Fulfilled = " . $status . ", TransactID = '" . $txnID . "', TransactResult = '" . $paymentStatus . "', ResponseMsg = '" . $errmsg . "' WHERE OrderID = " . $itemNumber ;
	}
} else {
	// update orders table
	if ($custom != "" && $txnID != "" && $paymentStatus != "") {
		$sql= "UPDATE sweep_orders SET Fulfilled = " . $status . ", TransactID = '" . $txnID . "', TransactResult = '" . $paymentStatus . "', ResponseMsg = '" . $errmsg . "' WHERE OrderID = " . $custom ;
	}
}
if ($sql) {
	mysql_select_db($database_sweep, $sweep);
	$IPN_rsId = mysql_query($sql, $sweep) or die(mysql_error());
	$IPN_rsId = null;

	// fetch data records
} else {
	// communicate the error
}
ok thanks i will try this.
Post Reply