Paypal IPN auhtorize script

I have a Paypal IPN script that logs all the transactions perfectly into a database log. I am trying to add a script that is “required” from the original script after all the authorizations are completed in the original script.

The script works fine except that I am drawing a blank on how to format the logic to only effect a single value in the User data base without affecting all of the other values in the database that are set by the IPN transaction.

I need to only make changes when the appropriate values are made for the appropriate product. I having only been writing PHP code for a month and I apologize for not being able to find the proper methods.

Here is the code:
[php]<?php
require(‘createconnect.php’);

if(isset($_POST[‘submitted’]))

if(empty($_POST[‘item_name’]))
die(‘You failed to receive item information.’);

$User = $_POST['custom'];	
$Product = $_POST['item_name'];
$Payment = $_POST['mc_fee'];

if($Product == Emp && $Payment == 10.00) {
$Emp = ‘1’;
} else if ($Product == Emp && $Payment == 15.00){
$Emp = ‘2’;
} else if ($Product == Emp && $Payment == 20.00){
$Emp = ‘3’;
}

$query = (‘UPDATE Users ‘.
‘SET EMP = "’.mysqli_real_escape_string($dbcon, $Emp).’" ‘.
‘WHERE User_Id = "’.mysqli_real_escape_string($dbcon, $User).’"’);

if($Product == Biz && $Payment == 10.00) {
$Biz = ‘1’;
} else if ($Product == Biz && $Payment == 15.00){
$Biz = ‘2’;
} else if ($Product == Emp && $Payment == 20.00){
$Biz = ‘3’;
}

$query = (‘UPDATE Users ‘.
‘SET BIZ = "’.mysqli_real_escape_string($dbcon, $Biz).’" ‘.
‘WHERE User_Id = "’.mysqli_real_escape_string($dbcon, $User).’"’);

if(!mysqli_query($dbcon, $query)) {
die(‘error inserting into database. Please email [email protected].’);
} // end of nested if statement

?>[/php]

Based on the code posted you are already restricting the update to “WHERE User_id” so I can’t visualize the problem you are having with “affecting all other values”

Thanks for the response!

If the buyer did not purchase the product signified by $Emp and did buy the product signified by $Biz, the database record for EMP was updated to 0.

I abandoned the code and started over with a new method when I could not fix the problem.

Do you still want help with this code?

If someone has time. I am perplexed what I was doing wrong with my formatting.

If I understand, you don’t want to overwrite ‘BIZ’ in order to set ‘EMP’ (and vice versa)? So you should really only be doing 1 query instead of 2.

Also, what is this, are these ‘Emp’ and ‘Biz’ values constants?

[php]
if($Product == Emp && $Payment == 10.00)
[/php]

I believe this is what you wanted to do. I used switches because I thought it would better demonstrate the logic. You could replace these with if/else statements if you wish. I personally like using switches, I think they are cleaner looking :slight_smile:

[php]
if (isset($_POST[‘submitted’])) {
if (empty($_POST[‘item_name’])) {
die(‘You failed to receive item information.’);
}

$User = $_POST['custom'];
$Product = $_POST['item_name'];
$Payment = floatval($_POST['mc_fee']); // use floatval() to ensure a proper float type

// set empty values for both $Emp and $Biz
$Emp = 0;
$Biz = 0;

// switch $Product value
switch($Product) {
	// match 'Emp'
	case 'Emp':
		// switch $Payment float value
		switch($Payment) {
			case 10.00:
				$Emp = 1;
			break;
			case 15.00:
				$Emp = 2;
			break;
			case 20.00:
				$Emp = 3;
			break;
		}
	break;
	
	// match 'Biz'
	case 'Biz':
		// switch $Payment float value
		switch($Payment) {
			case 10.00:
				$Biz = 1;
			break;
			case 15.00:
				$Biz = 2;
			break;
			case 20.00:
				$Biz = 3;
			break;
		}
	break;
	
	default:
		die('An invalid \$Product was specified.'); // $Product value was not matched
	break;
}

// make sure one of the values are set
if (!$Emp && !$Biz) {
	die('An invalid \$Payment was specified.'); // $Payment value was not matched
}

// build update query
$query = "UPDATE `Users` SET " . ($Emp ? "`EMP` = " . $Emp : "") . ($Biz ? "`BIZ` = " . $Biz : "") . " WHERE `User_Id` = " . mysqli_real_escape_string($dbcon, $User);

// execute query
if (!mysqli_query($dbcon, $query)) {
	die('error inserting into database. Please email [email protected].');
}

}
[/php]

Absolutely! Thanks so much! This solves my issue. I can see now how to do this logic. Thank-you all!

Sponsor our Newsletter | Privacy Policy | Terms of Service