Page 1 of 1

should this PHP if else statement work

Posted: Mon Oct 12, 2015 8:24 am
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;
}
}

Re: should this PHP if else statement work

Posted: Mon Oct 12, 2015 8:30 am
by Celauran
Unnecessarily verbose, but looks like it should work. Why not test it in a development environment?

Re: should this PHP if else statement work

Posted: Mon Oct 12, 2015 8:37 am
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

Re: should this PHP if else statement work

Posted: Mon Oct 12, 2015 11:50 am
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
}

Re: should this PHP if else statement work

Posted: Mon Oct 12, 2015 12:02 pm
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.

Re: should this PHP if else statement work

Posted: Mon Oct 12, 2015 12:19 pm
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.