Question

What's wrong with this PHP, SQL code?

Asked by: EMB01

The attached code doesn't seem to update my database. Is there something noticeably wrong with it?

// Define the function
function processOrder($data)
{
	$id = $_POST['item_number'];
	$amount = $_SESSION['make_payment_amount'];
	$query = "SELECT * FROM projects WHERE `id_prj` = '$id'";
	$paid = $query['paid_prj'] + $amount;
	$due = $query['due_prj'] - $amount;
	$notes = $query['notes_prj'] . "A payment for " . $amount . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
	insertQuery("UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = '$id' LIMIT 1");
}
 
// Process the order
processOrder($_POST);
exit;

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-08-27 at 10:47:57ID23682820
Tags

PHP

,

Internet Explorer

,

http://www.emarketbuilders.com

Topics

PHP Scripting Language

,

PHP and Databases

,

Miscellaneous Web Development

Participating Experts
3
Points
500
Comments
60

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. how to copy/update a table in SQL server to a ACCESS d…
    I have a table in my SQL server database. The table needs to be copied to a MS access database in a network drive. How to do it? I tried to use schedule a export task in SQL, it turned out that the records were appended to the Access database. I really want to update the tabl...
  2. Password Expiration notices wrong
    Server 2003 network, XP SP2 Clients. Occasionally some of my users are getting password expiration notices that are totally wrong, like for 90 days or 200 days etc. They should just be getting them 14 days before the expiration. Any help for this?
  3. Notice Use of undefined constant
    I am trying to get this tutorial script i found on phpfreaks to work. I've put in the code below and i get the following errors: Notice: Use of undefined constant db_host - assumed 'db_host' in C:\Inetpub\vhosts\republicncc.com\httpdocs\export.php on line 2 Notice: Use of...
  4. Fix undefined offset notice
    I need to solve this undefined offset notice. It seems to be caused by the last line in the reported code

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: hieloPosted on 2008-08-27 at 10:52:44ID: 22327425

You are NOT executing this query:
$query = "SELECT * FROM projects WHERE `id_prj` = '$id'";

try:

function processOrder($data)
{
      $id = $_POST['item_number'];
      $amount = $_SESSION['make_payment_amount'];
      $result = mysql_query("SELECT * FROM projects WHERE `id_prj` = '$id'") or die(mysql_error());
	 $query = mysql_fetch_assoc($result);
      $paid = $query['paid_prj'] + $amount;
      $due = $query['due_prj'] - $amount;
      $notes = $query['notes_prj'] . "A payment for " . $amount . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
      insertQuery("UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = '$id' LIMIT 1");
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: EMB01Posted on 2008-08-27 at 11:07:28ID: 22327574

Thanks, I rarely program SQL by hand - usually through Dreamweaver, etc. The database still isn't updated. The insertQuery function definition has been attached. All connections are made and variables exist. Maybe there's something wrong with the SQL?

// MySQL query operations
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id);
	return $messages;
}

                                              
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

 

by: hieloPosted on 2008-08-27 at 11:25:50ID: 22327730

echo your query so you can  verify what it is trying to execute.  Copy and paste this:

// MySQL query operations
function insertQuery($query)
{
echo "executing: " . $query;
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: yodercmPosted on 2008-08-27 at 11:50:05ID: 22327966

You are not retrieving the resultset ($messages).

Either
(1)  use the statement

$result = insertQuery($query);

to call the function,
OR
(2) make the $messages variable global in the function

function insertQuery($query)
{
global $messages;
echo "executing: " . $query;
      $link_id = db_connect();
      $messages = mysql_query($query, $link_id) or die(mysql_error());
      return $messages;
}
in which case you don't need to return it.

 

by: EMB01Posted on 2008-08-27 at 11:51:20ID: 22327975

It echoes:
Notice: Undefined index: item_number in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 123
executing: UPDATE projects SET `paid_prj` = '1', `due_prj` = '-1', `notes_prj` = 'A payment for 1 was made on Wednesday, August 27, 2008 at 11:47 AM PDT. ' WHERE `id_prj` = '' LIMIT 1

Why isn't the item_number being received by post? Is it because it's in a function such as this and needs to be declared in the function (function name($var, $var, $var))?

 

by: hieloPosted on 2008-08-27 at 12:18:53ID: 22328228

in processOrder you have:
$id = $_POST['item_number'];

which may not be picking up the value. Try this:

function processOrder($data)
{
      $id = $_REQUEST['item_number'];
      $amount = $_SESSION['make_payment_amount'];
 $q="SELECT * FROM projects WHERE `id_prj` = '$id'";
echo "processOrder::executing - " . $q;
      $result = mysql_query($q) or die(mysql_error());
	 $query = mysql_fetch_assoc($result);
      $paid = $query['paid_prj'] + $amount;
      $due = $query['due_prj'] - $amount;
      $notes = $query['notes_prj'] . "A payment for " . $amount . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
      insertQuery("UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = '$id' LIMIT 1");
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

 

by: yodercmPosted on 2008-08-27 at 12:20:42ID: 22328241

Basically, you don't understand how to use mysql yet.

First of all, forget the functions, they are a waste of time and trouble.  

Here is a simple straight-line code to do what you are showing.


 
 
//Retrieve input parameters
	$id = $_POST['item_number'];
	$amount = $_SESSION['make_payment_amount'];
//Connect to the database
	$link_id = db_connect();
//Make the inputs safe from SQL Injection hacking
	$amount = mysql_real_escape_string($amount);
	$id = mysql_real_escape_string($id);
//Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = '$id'";
	$result1 = mysql_query($query);
	echo mysql_error();
//Retrieve the query data and compute
	$row = mysql_fetch_array($result);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment for " . $amount . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
//Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = '$id' LIMIT 1";
	$result2 = mysql_query($query2, $link_id);
	echo mysql_error();

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

Select allOpen in new window

 

by: EMB01Posted on 2008-08-27 at 12:37:51ID: 22328408

I tried both scripts. hielo, yours produces no errors but the database still isn't updated. yodercm, yours produces the following errors and the database is not updated...
Notice: Undefined index: item_number in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 114

Notice: Undefined variable: result in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 126

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 126

 

by: hieloPosted on 2008-08-27 at 13:05:10ID: 22328635

Line 16 of yodercm post:
$row = mysql_fetch_array($result);

should be:
$row = mysql_fetch_array($result1);

 

by: hieloPosted on 2008-08-27 at 13:08:28ID: 22328663

For the insertQuery, just try:
// MySQL query operations
function insertQuery($query)
{
echo "executing: " . $query;
      mysql_query($query) or die(mysql_error());
}

Were you seeing the correct sql querystring?

 

by: EMB01Posted on 2008-08-27 at 13:18:01ID: 22328764

That insertQuery produces a blank page. Moreover, yodercm's straight-line code produces the following errors:
Notice: Undefined index: item_number in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 114

Notice: Undefined index: make_payment_amount in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 115

 

by: EMB01Posted on 2008-08-27 at 13:18:23ID: 22328773

The querystring was missing the $id variable.

 

by: hieloPosted on 2008-08-27 at 13:44:56ID: 22329038

these "undefined index" error messeage are an indication that the array "key" you are using to dereference an array do not exist. So look at the fields names in your table and make sure that they match. Ex you have:
SELECT * FROM projects...

That * does NOT tell ME what are the names of the fields, but the posted code uses:
$paid = $row['paid_prj'] + $amount;

the assumption is that there is a field named paid_prj. YOU need to verify the correctness of these field name against what is being used in the code. NOTE: upper case vs lower case DOES matter. You have to provide the EXACT name

 

by: EMB01Posted on 2008-08-27 at 14:07:41ID: 22329239

In my database, for the "projects" table there exists the following relevent fields:
id_prj
name_prj
notes_prj
paid_prj
due_prj

 

by: yodercmPosted on 2008-08-27 at 14:28:58ID: 22329394

This

Notice: Undefined index: item_number in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 114

Notice: Undefined index: make_payment_amount in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 115

means that your field names item_number and make_payment_amount are not in the table.  Check the spelling and existance, and correct the code to use the right field names.

Hielo, thanks for catching the $result1 error :)

 

by: hieloPosted on 2008-08-27 at 14:32:31ID: 22329413

>>Notice: Undefined index: item_number in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 114

What is line 114 on what you posted?

 

by: EMB01Posted on 2008-08-27 at 14:35:38ID: 22329433

Even when I take those parameters out (use static varaibles) and stick with the code attached, the database isn't updated but no errors are received.

//Retrieve input parameters
	$id = 1;
	$amount = 1;
//Connect to the database
	$link_id = db_connect();
//Make the inputs safe from SQL Injection hacking
	$amount = mysql_real_escape_string($amount);
	$id = mysql_real_escape_string($id);
//Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = '$id'";
	$result1 = mysql_query($query, $link_id);
	echo mysql_error();
//Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment for " . $amount . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
//Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = '$id' LIMIT 1";
	$result2 = mysql_query($query2, $link_id);
	echo mysql_error();

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

Select allOpen in new window

 

by: hieloPosted on 2008-08-27 at 14:42:22ID: 22329496

>>$link_id = db_connect();
Are you doing a mysql_select_db() in that function as well?

>>...WHERE `id_prj` = '$id'
is the data type of `id_prj`  an int? If yes, do NOT use apostrophes around the value:
WHERE `id_prj` = $id

 

by: EMB01Posted on 2008-08-27 at 15:03:56ID: 22329721

See,
function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if (!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host.";
return 0;
}
else if (empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}

I took the apostrophes off but it still doesn't work...
WHERE `id_prj` = $id

 

by: hieloPosted on 2008-08-27 at 15:08:28ID: 22329766

Assuming that the name of your database (not the table) is MyProjects, right after:
$link_id = db_connect();
put
mysql_select_db ( 'MyProjects', $link_id] ) or die(mysql_error());

 

by: EMB01Posted on 2008-08-27 at 15:20:15ID: 22329872

Even without all that, $link_id evaluates to true. The following errors appear:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 125

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 128

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/vhosts/emarketbuilders.com/httpdocs/clients/make_payment.php on line 134

 

by: EMB01Posted on 2008-08-27 at 15:27:35ID: 22329913

After I fixed a syntax error, here's what happens when I echo the two queries...
SELECT * FROM projects WHERE `id_prj` = 1
UPDATE projects SET `paid_prj` = '15', `due_prj` = '-15', `notes_prj` = 'This is a test.A payment for 1 was made on Wednesday, August 27, 2008 at 1:41 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 1:41 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 2:05 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 2:05 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 2:59 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 2:59 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 2:59 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:00 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:01 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:01 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:13 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:13 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:21 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:21 PM PDT. A payment for 1 was made on Wednesday, August 27, 2008 at 3:22 PM PDT. ' WHERE `id_prj` = 1 LIMIT 1

This is really wierd as the values seem to be changing, but the database remains unchanged. Notice the `paid_prj` = '15', `due_prj` = '-15', and the numerous `notes_prj` entries. Something is being updated, but when I check the database the values remain unchanged.

 

by: hieloPosted on 2008-08-27 at 15:28:52ID: 22329923

Most likely this is a scope issue. Those line numbers:
 line 125

do not make sense to me. I don't know what you are looking at. So....
First and Final offer. Attach ALL your code (minus the sensitive info of course.)

 

by: EMB01Posted on 2008-08-27 at 15:39:31ID: 22329987

That's pretty much it for this page. It has been attached. If you go to http://www.emarketbuilders.com/clients/make_payment.php?project=2 you'll notice the values beneath the logo, such as "You've paid" and "You owe" are pulled from the same table on the same database and do not read the same as the echo'ed query. Thanks for your help.

function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if (!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host.";
return 0;
}
else if (empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
} 
else return $link_id;
}
//Retrieve input parameters
	$id = 1;
	$amount = 1;
//Connect to the database
	$link_id = db_connect();
	if ($link_id = true)
	{ echo "TRUE"; } else { echo "FALSE"; }
//Make the inputs safe from SQL Injection hacking
	$amount = mysql_real_escape_string($amount);
	$id = mysql_real_escape_string($id);
//Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	echo $query;
	$result1 = mysql_query($query);
	echo mysql_error();
//Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment for " . $amount . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
//Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
	echo $query2;
	$result2 = mysql_query($query2);
	echo mysql_error();

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:

Select allOpen in new window

 

by: hieloPosted on 2008-08-27 at 15:49:07ID: 22330043

>>if ($link_id = true){ echo "TRUE"; } else { echo "FALSE"; }
Here you are assigning a value to $link_id instead of TESTING it. The correct syntax is:
if ($link_id == true)
      { echo "TRUE"; } else { echo "FALSE"; }
try the attached code but provide YOUR db name

<?php
mysql_connect($dbhost, $dbusername, $dbuserpassword) or die( mysql_error() );
mysql_select_db('MyDatabaseName') or die( mysql_error() );
 
//Retrieve input parameters
	$id = 1;
	$amount = 1;
 
//Make the inputs safe from SQL Injection hacking
	$amount = mysql_real_escape_string($amount);
	$id = mysql_real_escape_string($id);
//Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	echo $query;
	$result1 = mysql_query($query) or die(mysql_error());
 
//Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment for " . $amount . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
//Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
	echo $query2;
	$result2 = mysql_query($query2) or die(mysql_error());
 
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen in new window

 

by: EMB01Posted on 2008-08-27 at 16:00:15ID: 22330099

I don't know. I'm very confused - if this stuff isn't being stored in the database, then how and where is it being stored? The number's and text keep increasing from their previous values as the "database" is updated, but it isn't really updated (verfiable when I use another recordset to access the data).

 

by: EMB01Posted on 2008-09-01 at 09:24:03ID: 22361369

hielo, you were right. It does work! However, when I put the code into the "big" code for my PayPal IPN, it does not. Here it is, maybe there is a logical reason it won't update the DB from this new page... Note: The lack of functionality is referring to the "Process the order" section. The points have been increased by 250 totaling 500.

// MySQL query operations
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
 
// Row count function
function rowCount($query)
{
	$link_id = db_connect();
	$result = mysql_query($query);
	$rowCount = mysql_num_rows($result);
	return $rowCount;
}
 
// Validating the IPN with PayPal
function verifyIPN($data)
{
	$postdata = "";
	$response = array();
	
	foreach($data as $var=>$val)
	{
		$postdata .= $var . "=" . urlencode($val) . "&";
	}
	$postdata .="cmd=_notify-validate";
	$fp=@fsockopen("ssl://www.sandbox.paypal.com" ,"443" , $errnum, $errstr, 30);
	if(!$fp)
	{
		return "$errnum: $errstr";
	} else
	{
		fputs($fp, "POST /cgi-bin/webscr HTTP/1.0\r\n");
		fputs($fp, "Host: www.sandbox.paypal.com\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ".strlen($postdata)."\r\n");
		fputs($fp, "Connections: close\r\n\r\n");
		fputs($fp, $postdata . "\r\n\r\n");
		while(!feof($fp)) { $response[]=@fgets($fp, 1024); }
		fclose($fp);
	}
	$response = implode("\n", $response);
	if(eregi("VERIFIED", $response))
	{
		return true;
	} else
	{
		return false;
	}
}
 
// Confirming product information
function confirmProduct($id, $name, $amount)
{
	if (!(ctype_digit("$id") && is_numeric("$amount")))
	{
		return false;
	} else
	{
		$name = "Payment for " . mysql_escape_string("$name");
	}
	$query = "SELECT id_prj FROM projects WHERE `id_prj` = '$id' AND `name_prj` = '$name' LIMIT 1";
	if (rowCount($query) == 1)
	{
		return true;
	} else
	{
		return false;
	}
}
 
// Pending payment confirmation
function paymentPendingThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. However, PayPal is still processing your payment at this time. Once PayPal confirms that they have completed processing your payment we will contact you again to confirm the details.\n\n If you have any questions please do not hesitate to contact us.\n\n ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Completed payment confirmation
function paymentCompletedThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Process the order
function processOrder($id, $amount)
{
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	$result1 = mysql_query($query) or die(mysql_error());
 
	// Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
	// Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
	$result2 = mysql_query($query2) or die(mysql_error());
}
 
// Step 0. Record the transaction
ob_start();
echo date("D M j G:i:s T Y") . "\n";
print_r($_SERVER);
print_r($_POST);
$body = ob_get_clean();
file_put_contents("logs/IPN.txt", $body, FILE_APPEND);
 
// Step 1. Verify IPN with PayPal
$result = verifyIPN($_POST);
if ($result == 0)
{
	$subject = "FAKE IPN RECEIVED";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
} else if ($result != 1)
{
	$subject = "Unable to validate IPN";
	$body = "If this payment notification is valid it will need to be manually processed\n $result\n $body";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 1.5. Check payment status
switch ($_POST['payment_status'])
{
	case "Completed":
		paymentCompletedThankYou($_POST['payer_email']);
		break;
	case "Pending":
		paymentPendingThankYou($_POST['payer_email']);
		break;
	default:
		$body = "Hi, an IPN was received that was neither a complete payment nor a pending payment. Please contact us to confirm this transaction against our records.";
		$body .= $post;
		$subject = "IPN Received";
		$address = "admin@emarketbuilders.com";
		$headers = 
			"From: admin@emarketbuilders.com\r\n" . 
			"Reply-To: donotreply@emarketbuilders.com\r\n" . 
			"X-Mailer: PHP/" . phpversion();
		mail($address, $subject, $body, $headers);
		exit;
}
 
// Step 2. Confirm product information
$result = confirmProduct($_POST['item_number'], $_POST['item_name'], $_POST['mc_gross']);
if ($result == false)
{
	$subject = "Product Name/ID/Price Error";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" .
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 3. Process the order
processOrder($_POST['item_number'], $_POST['mc_gross']);
exit;
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:

Select allOpen in new window

 

by: MasonWolfPosted on 2008-09-05 at 04:17:55ID: 22397348

You have a bug here:

$row = mysql_fetch_array($result1);

$row is being assigned a numeric-indexed array, but you're using column names for the keys. I've come across this as well - for some reason PHP does not throw an error, it simply gives 'null' for each of those array values you're trying to access with invalid keys.

Use this instead and I think your script will work:

$row = mysql_fetch_assoc($result1);

 

by: EMB01Posted on 2008-09-05 at 05:00:20ID: 22397678

Thanks for your response. The same script works on another page, so I don't think there's a problem with the "// Process the order" code itself; but something else happens when it's all put together that seems to mess things up. Maybe, I should be using the "insertQuery" function as it appears to connect to the database. I tried it but it didn't work either - the code has been attached.

// MySQL query operations
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
 
// Process the order
function processOrder($id, $amount)
{
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	insertQuery($query);
 
	// Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
	// Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
	insertQuery($query2);
	exit;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:

Select allOpen in new window

 

by: MasonWolfPosted on 2008-09-05 at 07:39:17ID: 22399427

Your insertQuery function looks fine, assuming the 'db_connect()' function works as expected. I was wrong about mysql_fetch_array. I looked it up and it turns out that the default behavior is to give both associative and numeric indices.

I think the problem may lie in the fact that you're trying to add a number to a string. PHP is not strongly typed, but you still might want to try:

$paid = intval($row['paid_prj']) + $amount;
$due = intval($row['due_prj']) - $amount;

Do you know if the notes get updated? Also, if the problem really is with insertQuery, it's probably because db_connect isn't working the way it looks like it ought to. Without sharing your database login credentials, why don't you show us that code?

 

by: EMB01Posted on 2008-09-05 at 11:17:58ID: 22401985

Certainly. I think the connection is okay because "// Confirming product information" uses it and it seems to work fine. Note: $dbhost, $dbusername, $dbuserpassword, $default_dbname have been removed from the snippet.

// SQL error reporting
function sql_error()
{
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
 
// Connection information
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if (!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host.";
return 0;
}
else if (empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
} 
else return $link_id;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:

Select allOpen in new window

 

by: MasonWolfPosted on 2008-09-05 at 15:24:25ID: 22404368

Did using intval() have any effect?

I know you may have explained it in a previous comment, but what actually breaks here? Does the database  not actually update, or does it simply fail to update the way you expect.

The db_connect function looks fine.

 

by: EMB01Posted on 2008-09-05 at 16:14:53ID: 22404560

The "// Process the order" script updates the database on another page, but once I put it into my big script it doesn't work - meaning it doesn't update the database. I havn't tried the intval(), but like I said, I don't think there's a problem with the script itself as it works; it just doesn't work when combined with the big validation code previously attached.

 

by: EMB01Posted on 2008-09-06 at 09:24:53ID: 22408025

Here, I'll try to make it even clearer by re-stating the question. The "// Process the order" code works on this page:
<?php require_once('../Connections/emb.php'); ?>
<?php require_once('../Connections/emb.php'); ini_set ('display_errors', 1);
error_reporting (E_ALL); ?>
<?php
// Load the common classes
require_once('../includes/common/KT_common.php');

// Require the MXI classes
require_once ('../includes/mxi/MXI.php');
?>
<?php
if (!session_id()) session_start();
if(($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_SERVER["HTTP_REFERER"]) && strpos(urldecode($_SERVER["HTTP_REFERER"]), urldecode($_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"])) > 0) && isset($_POST))     {
  $_SESSION["make_payment_id"] = "".$row_rsProjects['id_prj']  ."";
}
?>
<?php
if (!session_id()) session_start();
if(($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_SERVER["HTTP_REFERER"]) && strpos(urldecode($_SERVER["HTTP_REFERER"]), urldecode($_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"])) > 0) && isset($_POST))     {
  $_SESSION["make_payment_amount"] = "".((isset($_POST["amount"]))?$_POST["amount"]:"")  ."";
}
?>
<?php


// Load the tNG classes
require_once('../includes/tng/tNG.inc.php');

$colname_rsProjects = "-1";
if (isset($_GET['project'])) {
  $colname_rsProjects = (get_magic_quotes_gpc()) ? $_GET['project'] : addslashes($_GET['project']);
}
mysql_select_db($database_emb, $emb);
$query_rsProjects = sprintf("SELECT * FROM projects WHERE id_prj = %s", $colname_rsProjects);
$rsProjects = mysql_query($query_rsProjects, $emb) or die(mysql_error());
$row_rsProjects = mysql_fetch_assoc($rsProjects);
$totalRows_rsProjects = mysql_num_rows($rsProjects);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Coming Soon!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Keywords" content="EMB, web design, web development, seo, ecommerce, emarketing, full service" />
<meta name="Description" content="The all-new EMB Client Center is under development." />
<script src="/includes/cssmenus2/js/cssmenus.js" type="text/javascript"></script>
<script type="text/javascript" src="/includes/nifty/equalcolumns.js"></script>
<script type="text/javascript" src="/includes/nifty/tabpane.js"></script>
<link rel="stylesheet" type="text/css" href="/page_style.css" />
<link href="/includes/cssmenus2/skins/emb/horizontal.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="/includes/nifty/tab.emb.css" />
<style type="text/css">
table{margin: 10px 0px 10px 10px}
tr{border-bottom: solid 1px #cccccc}
th{padding: 10px 10px 10px 0px; color:#333333; border-bottom: solid 1px #cccccc}
td{padding: 10px 10px 10px 0px; border-bottom: solid 1px #cccccc}
p.arrowlink{margin: 10px 0px 10px 10px; background: url(../includes/cssmenus2/skins/emb/img/arrow_orange.gif) no-repeat center left; text-decoration: none; padding-left: 14px; /*adjust bullet image padding*/ color: #71716C}
</style>
</head>
<body>
<div id="container">
  <div class="header">
    <div class="logot"><a href="/index.php" title="EMB Web Design"><img src="/logos/logo_100.gif" alt="EMB Web Design" width="100" height="42" border="0" /></a> </div>
    <div class="navigation">
      <?php
  mxi_includes_start("../components/nav.php");
  require(basename("../components/nav.php"));
  mxi_includes_end();
?>
</div>
  </div>
  <div class="leftpanel" id="leftcolumn">
    <p class="breadcrumb"><a href="http://www.emarketbuilders.com/">EMB</a> <a href="http://www.emarketbuilders.com/clients/">Clients</a> Coming Soon! </p>
    <h1>Coming Soon! </h1>
    <p><em>The all-new Client Center is nearly complete.</em></p>
    <p>It will provide clients with:<?php function sql_error()
{
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if (!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host.";
return 0;
}
else if (empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}
 
//Retrieve input parameters
      $id = 1;
      $amount = 1;
 
//Make the inputs safe from SQL Injection hacking
      $amount = mysql_real_escape_string($amount);
      $id = mysql_real_escape_string($id);
//Query for the data
      $query = "SELECT * FROM projects WHERE `id_prj` = $id";
      echo $query;
      $result1 = mysql_query($query) or die(mysql_error());
 
//Retrieve the query data and compute
      $row = mysql_fetch_array($result1);
      $paid = $row['paid_prj'] + $amount;
      $due = $row['due_prj'] - $amount;
      $notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
//Update the database
      $query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
      echo $query2;
      $result2 = mysql_query($query2) or die(mysql_error());
 ?>
?></p>
          <table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <th scope="col"></th>
      </tr>
    </table>
    <p>&nbsp;<img src="<?php echo tNG_showDynamicImage("../", "../images/", "{rsProjects.image_prj}");?>" /></p>
    <?php
// Show IF Conditional region2
if (@$row_rsProjects['started_prj'] != "") {
?>
    <p class="arrowlink">Project started on <?php echo KT_formatDate($row_rsProjects['started_prj']); ?>.</p>
    <?php
// else Conditional region2
} else { ?>
<p class="arrowlink">This project has not yet been started.</p>
<?php }
// endif Conditional region2
?>
    <?php
// Show IF Conditional region1
if (@$row_rsProjects['finished_prj'] != "") {
?>
    <p class="arrowlink">Project completed on <?php echo KT_formatDate($row_rsProjects['finished_prj']); ?>.</p>
   
    <?php }
// endif Conditional region1
?>
    <p class="arrowlink">You've paid $<?php echo number_format($row_rsProjects['paid_prj'], 2); ?>.</p>
    <p class="arrowlink">You owe $<?php echo number_format($row_rsProjects['due_prj'], 2); ?>.</p>
    <?php
// Show IF Conditional region3
if (@$row_rsProjects['notes_prj'] != "") {
?>
    <p class="arrowlink">It is noted that &quot;<?php echo $row_rsProjects['notes_prj']; ?>"</p>
    <?php }
// endif Conditional region3
?>
    <p>If you would like to learn more about our new system and when it will be ready, <a href="/contact_us.php" class="p">contact us</a>. Or... </p>
    <p><span>If you would like more details on how to move your company forward, fill out the quick <a href="/contact_form.php" class="p"><strong>contact form</strong></a>.</span></p>
</div>
  <div class="rightpanel" id="rightcolumn">
    <h3>Request Free Quote</h3>
      <?php
  mxi_includes_start("../components/rfq.php");
  require(basename("../components/rfq.php"));
  mxi_includes_end();
?>
<h4>More</h4>
            <?php
  mxi_includes_start("../components/solutions_and_services.php");
  require(basename("../components/solutions_and_services.php"));
  mxi_includes_end();
?>
        <h4>Advertisement</h4>
            
        <?php
  mxi_includes_start("../components/ads.php");
  require(basename("../components/ads.php"));
  mxi_includes_end();
?>
  </div>
  <div class="footer">
    <?php
  mxi_includes_start("../components/footer.php");
  require(basename("../components/footer.php"));
  mxi_includes_end();
?>
<div class="copyright"><span class="f">Copyright &copy; 2007 EMB. <a href="/terms_of_use.php" class="f">All rights reserved.</a><br />
            <a href="http://jigsaw.w3.org/css-validator/check/referer" class="f" style="background-color: #FFF"> <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" width="32" height="32" border="0" align="bottom" style="border:0;width:88px;height:31px" /> </a> <a href="http://validator.w3.org/check?uri=referer" class="f" style="background-color: #FFF"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" align="bottom" style="border:0;width:88px;height:31px" /></a> </span></div>
      <div class="logo"><img src="/logos/logo_footer.gif" alt="EMB Web Design" width="101" height="57" align="right" /></div>
  </div>
</div>
</body>
</html>
<?php
mysql_free_result($rsProjects);
?>

But it does not work on the page attached...

<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL);
 
// SQL error reporting
function sql_error()
{
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
 
// Connection information
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if (!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host.";
return 0;
}
else if (empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
} 
else return $link_id;
}
 
// MySQL query operations
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
 
// Row count function
function rowCount($query)
{
	$link_id = db_connect();
	$result = mysql_query($query);
	$rowCount = mysql_num_rows($result);
	return $rowCount;
}
 
// Validating the IPN with PayPal
function verifyIPN($data)
{
	$postdata = "";
	$response = array();
	
	foreach($data as $var=>$val)
	{
		$postdata .= $var . "=" . urlencode($val) . "&";
	}
	$postdata .="cmd=_notify-validate";
	$fp=@fsockopen("ssl://www.sandbox.paypal.com" ,"443" , $errnum, $errstr, 30);
	if(!$fp)
	{
		return "$errnum: $errstr";
	} else
	{
		fputs($fp, "POST /cgi-bin/webscr HTTP/1.0\r\n");
		fputs($fp, "Host: www.sandbox.paypal.com\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ".strlen($postdata)."\r\n");
		fputs($fp, "Connections: close\r\n\r\n");
		fputs($fp, $postdata . "\r\n\r\n");
		while(!feof($fp)) { $response[]=@fgets($fp, 1024); }
		fclose($fp);
	}
	$response = implode("\n", $response);
	if(eregi("VERIFIED", $response))
	{
		return true;
	} else
	{
		return false;
	}
}
 
// Confirming product information
function confirmProduct($id, $name, $amount)
{
	if (!(ctype_digit("$id") && is_numeric("$amount")))
	{
		return false;
	} else
	{
		$name = "Payment for " . mysql_escape_string("$name");
	}
	$query = "SELECT id_prj FROM projects WHERE `id_prj` = '$id' AND `name_prj` = '$name' LIMIT 1";
	if (rowCount($query) == 1)
	{
		return true;
	} else
	{
		return false;
	}
}
 
// Pending payment confirmation
function paymentPendingThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. However, PayPal is still processing your payment at this time. Once PayPal confirms that they have completed processing your payment we will contact you again to confirm the details.\n\n If you have any questions please do not hesitate to contact us.\n\n ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Completed payment confirmation
function paymentCompletedThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Process the order
function processOrder($id, $amount)
{
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	insertQuery($query);
 
	// Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
	// Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
	insertQuery($query2);
	exit;
}
 
// Step 0. Record the transaction
ob_start();
echo date("D M j G:i:s T Y") . "\n";
print_r($_SERVER);
print_r($_POST);
$body = ob_get_clean();
file_put_contents("logs/IPN.txt", $body, FILE_APPEND);
 
// Step 1. Verify IPN with PayPal
$result = verifyIPN($_POST);
if ($result == 0)
{
	$subject = "FAKE IPN RECEIVED";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
} else if ($result != 1)
{
	$subject = "Unable to validate IPN";
	$body = "If this payment notification is valid it will need to be manually processed\n $result\n $body";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 1.5. Check payment status
switch ($_POST['payment_status'])
{
	case "Completed":
		paymentCompletedThankYou($_POST['payer_email']);
		break;
	case "Pending":
		paymentPendingThankYou($_POST['payer_email']);
		break;
	default:
		$body = "Hi, an IPN was received that was neither a complete payment nor a pending payment. Please contact us to confirm this transaction against our records.";
		$body .= $post;
		$subject = "IPN Received";
		$address = "admin@emarketbuilders.com";
		$headers = 
			"From: admin@emarketbuilders.com\r\n" . 
			"Reply-To: donotreply@emarketbuilders.com\r\n" . 
			"X-Mailer: PHP/" . phpversion();
		mail($address, $subject, $body, $headers);
		exit;
}
 
// Step 2. Confirm product information
$result = confirmProduct($_POST['item_number'], $_POST['item_name'], $_POST['mc_gross']);
if ($result == false)
{
	$subject = "Product Name/ID/Price Error";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" .
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 3. Process the order
processOrder($_POST['item_number'], $_POST['mc_gross']);
exit;
 
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:

Select allOpen in new window

 

by: hieloPosted on 2008-09-14 at 00:17:32ID: 22471435

EMB01,
First of all, my apologies for the late response. I tend to answer quite a few questions and the questions quickly scroll off my screen.

>>	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
OK, so that clearly shows the query you intend to execute
 
>>	insertQuery($query);
now here you are invoking a function. Let's digress here for a second and see what that function does:
 
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
 
OK, so it connects to the db, executes the query string in the $query variable AND
RETURNS a resource! To clarify, it returns a "pointer" to a "db recordset". So let's go back to processOrder() and keep following the logic of the execution.
 
>>	// Retrieve the query data and compute
>>	$row = mysql_fetch_array($result1);
OK so per your comment, your intention is clear, but that cannot possibly work. Where did you get $result1 from? If you look back at your previous statement, you just have => insertQuery($query);
 
there is NOTHING receiving the result of the query! So that statment should have been:
$result1 = insertQuery($query);
 
That is the bug in that function. Since you are NOT "catching/receiving" the resource from insertQuery, then $row is NOT getting/seeing any values.
 
On another note, I see that at the end of that function you have:
>>$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
>>insertQuery($query2);
 
The "buggy" statement outlined previously was using insertQuery to "SELECT" records. Now you are using the same function to "UPDATE" records. However, an UPDATE statement does not return records. So I suggest you modify the function so that if you are executing a select, you get back the "recordset", but if you are doing something other than a select, have the function return the number of affected records. You also need to do error checking.
 
Having said that, here are the updated and commented functions so you can follow along:
 
 
 
 
// MySQL query operations
function executeQuery($query)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	$link_id = db_connect();
	
	//check for connection problems
	if(!$link_id)
	{	//simply return null since db_connect() already set the
		//$MYSQL_ERRNO and $MYSQL_ERROR;
		return NULL;
	}
 
	$query =trim($query);
	$messages = mysql_query($query, $link_id);
 
	//check if query failed and update global variables accordingly
	//return NULL so you know query failed. Then at the place where you are callin
	//executeQuery you need to decide whether you want to script to "die" or not
	if( !$messages )
	{
		$MYSQL_ERRNO =mysql_errno();
		$MYSQL_ERROR = 'Problems encountered while executing <br/>'. $query . '<br/>Error:' .  mysql_error();
	return NULL;
	}
 
	//if you made it this far, there were not problems executing the script
	//so now check if a SELECT query was requested and if so, return the recordset
	if( preg_match('/^SELECT\s+/i',$query )
	{
		return $messages;
	}
 
	//Get the number of affected rows by the last INSERT, UPDATE, REPLACE or 
	//DELETE query associated with link_identifier
	//Example: if you executed an update statement, this will tell you how many
	//records were actually updated, which could be 1 or 100 or even zero!
	if( preg_match('/^(INSERT|UPDATE|DELETE|REPLACE)\s+/',$query) )
	{
		return mysql_affected_rows($link_id); 
	}
 
//for any other command, we just know the command succeeded
return TRUE;
}
 
 
 
// Process the order
function processOrder($id, $amount)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
 
	//this will be used to send back the overall return status of this function
	//assume that initially the function will fail
	$result=NULL;
 
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	
	//save the returned value
	$result1 = executeQuery($query);
 
	//check for errors
	if( NULL === $result1 )
	{
		echo $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
	}
	else//query succeeded
	{
		// Retrieve the query data and compute
		$row = mysql_fetch_array($result1);
		$paid = $row['paid_prj'] + $amount;
		$due = $row['due_prj'] - $amount;
		$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
		// Update the database
		$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
 
		//save the returned value. If this succeeds, the return value will the
		//the total number of updated records
		$result = executeQuery($query2);
		
		//check for errors
		if(NULL===$result)
		{
			echo $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
		}
	}
return $result;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:

Select allOpen in new window

 

by: EMB01Posted on 2008-09-16 at 06:39:10ID: 22488221

That's okay. Thanks for outlining the changes; it makes more sense to me now. However, I replaced the insertQuery and ProcessOrder functions but when I try to send a PayPal IPN via the sandbox simulator, I receive an error "IPN delivery failed. HTTP error code 500: Internal Server Error." Is there an error in the code - because without this new addition it goes through.

Note: I also tried to simply replace the one statement with "$result1 = insertQuery($query);" but it didn't update the database. Moreover, keep in mind that the code snippet attached does work, but the similar code (for validating the IPN, etc.) as attached in previous posts does not work.

<?php function sql_error()
{
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if (!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host.";
return 0;
}
else if (empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
} 
else return $link_id;
}
 
//Retrieve input parameters
      $id = 1;
      $amount = 1;
 
//Make the inputs safe from SQL Injection hacking
      $amount = mysql_real_escape_string($amount);
      $id = mysql_real_escape_string($id);
//Query for the data
      $query = "SELECT * FROM projects WHERE `id_prj` = $id";
      echo $query;
      $result1 = mysql_query($query) or die(mysql_error());
 
//Retrieve the query data and compute
      $row = mysql_fetch_array($result1);
      $paid = $row['paid_prj'] + $amount;
      $due = $row['due_prj'] - $amount;
      $notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
//Update the database
      $query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
      echo $query2;
      $result2 = mysql_query($query2) or die(mysql_error());
 ?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:

Select allOpen in new window

 

by: hieloPosted on 2008-09-16 at 12:37:18ID: 22492400

>>HTTP error code 500: Internal Server Error."
That is a very generic error description you need to be more specific. Probably there's a semicolon missing some where. Start your file with the following so you can get more details about the error:

<?php
error_reporting(E_ALL);
...

                                              
1:
2:
3:

Select allOpen in new window

 

by: EMB01Posted on 2008-09-16 at 12:41:46ID: 22492452

No, I mean from the PayPal Developer interface (developer.paypal.com) when I run the IPN simulator pointing to the ipn.php URL I get the error (from the PayPal website). It is not an error from direct access to the page. I only get this error with the new addition of the script, otherwise it works and PayPal confirms the success of the IPN transmission.

 

by: EMB01Posted on 2008-09-18 at 09:35:29ID: 22512726

Not to be at all pushy, but it seems your one of the top experts and if you intend to respond to this question I only ask that you do so as quickly as possible. Otherwise, I can repost (which I would rather not); I would just like this to be resolved as soon as possible. Please let me know what you want me to do. Thanks for your help.

 

by: hieloPosted on 2008-09-19 at 08:20:01ID: 22522095

>>No, I mean from the PayPal Developer interface
Ok, now I'm confused. I thought you were trying to update your db.
"The attached code doesn't seem to update my database..."

Is the db on your network? if yes, does it update your db? Are you getting any sql error messages?

 

by: EMB01Posted on 2008-09-19 at 09:26:21ID: 22522885

The script is a PayPal IPN validation script. After the script has validated the IPN, it is to update the database. Notice: Step 3. Yes, the DB is on my network and; no, I'm not getting any SQL error messages that I know of. As pointed out above, the exact update DB script works standalone, but when I put it into the IPN validation script (attached) it doesn't update the DB. Thank you.

<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL);
 
// SQL error reporting
function sql_error()
{
global $MYSQL_ERRNO, $MYSQL_ERROR;
if(empty($MYSQL_ERROR))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
 
// Connection information
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if (!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host.";
return 0;
}
else if (empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
} 
else return $link_id;
}
 
// MySQL query operations
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
 
// Row count function
function rowCount($query)
{
	$link_id = db_connect();
	$result = mysql_query($query);
	$rowCount = mysql_num_rows($result);
	return $rowCount;
}
 
// Validating the IPN with PayPal
function verifyIPN($data)
{
	$postdata = "";
	$response = array();
	
	foreach($data as $var=>$val)
	{
		$postdata .= $var . "=" . urlencode($val) . "&";
	}
	$postdata .="cmd=_notify-validate";
	$fp=@fsockopen("ssl://www.sandbox.paypal.com" ,"443" , $errnum, $errstr, 30);
	if(!$fp)
	{
		return "$errnum: $errstr";
	} else
	{
		fputs($fp, "POST /cgi-bin/webscr HTTP/1.0\r\n");
		fputs($fp, "Host: www.sandbox.paypal.com\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ".strlen($postdata)."\r\n");
		fputs($fp, "Connections: close\r\n\r\n");
		fputs($fp, $postdata . "\r\n\r\n");
		while(!feof($fp)) { $response[]=@fgets($fp, 1024); }
		fclose($fp);
	}
	$response = implode("\n", $response);
	if(eregi("VERIFIED", $response))
	{
		return true;
	} else
	{
		return false;
	}
}
 
// Confirming product information
function confirmProduct($id, $name, $amount)
{
	if (!(ctype_digit("$id") && is_numeric("$amount")))
	{
		return false;
	} else
	{
		$name = "Payment for " . mysql_escape_string("$name");
	}
	$query = "SELECT id_prj FROM projects WHERE `id_prj` = '$id' AND `name_prj` = '$name' LIMIT 1";
	if (rowCount($query) == 1)
	{
		return true;
	} else
	{
		return false;
	}
}
 
// Pending payment confirmation
function paymentPendingThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. However, PayPal is still processing your payment at this time. Once PayPal confirms that they have completed processing your payment we will contact you again to confirm the details.\n\n If you have any questions please do not hesitate to contact us.\n\n ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Completed payment confirmation
function paymentCompletedThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Process the order
function processOrder($id, $amount)
{
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	insertQuery($query);
 
	// Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
	// Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
	insertQuery($query2);
	exit;
}
 
// Step 0. Record the transaction
ob_start();
echo date("D M j G:i:s T Y") . "\n";
print_r($_SERVER);
print_r($_POST);
$body = ob_get_clean();
file_put_contents("logs/IPN.txt", $body, FILE_APPEND);
 
// Step 1. Verify IPN with PayPal
$result = verifyIPN($_POST);
if ($result == 0)
{
	$subject = "FAKE IPN RECEIVED";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
} else if ($result != 1)
{
	$subject = "Unable to validate IPN";
	$body = "If this payment notification is valid it will need to be manually processed\n $result\n $body";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 1.5. Check payment status
switch ($_POST['payment_status'])
{
	case "Completed":
		paymentCompletedThankYou($_POST['payer_email']);
		break;
	case "Pending":
		paymentPendingThankYou($_POST['payer_email']);
		break;
	default:
		$body = "Hi, an IPN was received that was neither a complete payment nor a pending payment. Please contact us to confirm this transaction against our records.";
		$body .= $post;
		$subject = "IPN Received";
		$address = "admin@emarketbuilders.com";
		$headers = 
			"From: admin@emarketbuilders.com\r\n" . 
			"Reply-To: donotreply@emarketbuilders.com\r\n" . 
			"X-Mailer: PHP/" . phpversion();
		mail($address, $subject, $body, $headers);
		exit;
}
 
// Step 2. Confirm product information
$result = confirmProduct($_POST['item_number'], $_POST['item_name'], $_POST['mc_gross']);
if ($result == false)
{
	$subject = "Product Name/ID/Price Error";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" .
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 3. Process the order
processOrder($_POST['item_number'], $_POST['mc_gross']);
exit;
 
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:

Select allOpen in new window

 

by: hieloPosted on 2008-09-19 at 10:56:54ID: 22523712

Hmmm, this started as a db issue, but has not turned into an IPN issue. I'm not familiar with IPN so I can't help you there. The only thing that stands out is that you are using Connections (plural) instead of Connection (singular):

fputs($fp, "Connections: close\r\n\r\n");

it should be Connection. If that still does not resolve your problem, I suggest you open another problem. I'm not sure if there is a "paypal" zone - you may have to ask in community support.

 

by: EMB01Posted on 2008-09-19 at 12:58:42ID: 22524931

The IPN validation function works, all emails are sent to their intended reciepts, and all other functions seem to work - except for the updating of the database. You think I should change the "fputs($fp, "Connections: close\r\n\r\n");" line? Otherwise, repost as a new question..?

 

by: hieloPosted on 2008-09-19 at 17:33:25ID: 22527207

The last code you posted ( ID: 22522885 ) does NOT reflect the NEEDED changes I meticulously outlined for you earlier. Try the attached code. Copy and paste would be nice:

<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL);
 
// SQL error reporting
function sql_error()
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	if(empty($MYSQL_ERROR))
	{
		$MYSQL_ERRNO = mysql_errno();
		$MYSQL_ERROR = mysql_error();
	}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
 
// Connection information
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
	global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
	if (!$link_id)
	{
		$MYSQL_ERRNO = 0;
		$MYSQL_ERROR = "Connection failed to the host.";
	return 0;
	}
	else if (empty($dbname) && !mysql_select_db($default_dbname))
	{
		$MYSQL_ERRNO = mysql_errno();
		$MYSQL_ERROR = mysql_error();
		return 0;
	} 
	else
		return $link_id;
}
 
/*
// MySQL query operations
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
*/
// MySQL query operations
function executeQuery($query)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	$link_id = db_connect();
	
	//check for connection problems
	if(!$link_id)
	{	//simply return null since db_connect() already set the
		//$MYSQL_ERRNO and $MYSQL_ERROR;
		return NULL;
	}
 
	$query =trim($query);
	$messages = mysql_query($query, $link_id);
 
	//check if query failed and update global variables accordingly
	//return NULL so you know query failed. Then at the place where you are callin
	//executeQuery you need to decide whether you want to script to "die" or not
	if( !$messages )
	{
		$MYSQL_ERRNO =mysql_errno();
		$MYSQL_ERROR = 'Problems encountered while executing <br/>'. $query . '<br/>Error:' .  mysql_error();
	return NULL;
	}
 
	//if you made it this far, there were not problems executing the script
	//so now check if a SELECT query was requested and if so, return the recordset
	if( preg_match('/^SELECT\s+/i',$query )
	{
		return $messages;
	}
 
	//Get the number of affected rows by the last INSERT, UPDATE, REPLACE or 
	//DELETE query associated with link_identifier
	//Example: if you executed an update statement, this will tell you how many
	//records were actually updated, which could be 1 or 100 or even zero!
	if( preg_match('/^(INSERT|UPDATE|DELETE|REPLACE)\s+/',$query) )
	{
		return mysql_affected_rows($link_id); 
	}
 
//for any other command, we just know the command succeeded
return TRUE;
}
 
 
// Row count function
function rowCount($query)
{
	$link_id = db_connect();
	$result = mysql_query($query);
	$rowCount = mysql_num_rows($result);
	return $rowCount;
}
 
// Validating the IPN with PayPal
function verifyIPN($data)
{
	$postdata = "";
	$response = array();
	
	foreach($data as $var=>$val)
	{
		$postdata .= $var . "=" . urlencode($val) . "&";
	}
	$postdata .="cmd=_notify-validate";
	$fp=@fsockopen("ssl://www.sandbox.paypal.com" ,"443" , $errnum, $errstr, 30);
	if(!$fp)
	{
		return "$errnum: $errstr";
	} else
	{
		fputs($fp, "POST /cgi-bin/webscr HTTP/1.0\r\n");
		fputs($fp, "Host: www.sandbox.paypal.com\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ".strlen($postdata)."\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $postdata . "\r\n\r\n");
		while(!feof($fp))
		{
			$response[]=@fgets($fp, 1024);
		}
		fclose($fp);
	}
	$response = implode("\n", $response);
	if(eregi("VERIFIED", $response))
	{
		return true;
	}
return false;
}
 
// Confirming product information
function confirmProduct($id, $name, $amount)
{
	if (!(ctype_digit("$id") && is_numeric("$amount")))
	{
		return false;
	}
	else
	{
		$name = "Payment for " . mysql_escape_string("$name");
	}
	$query = "SELECT id_prj FROM projects WHERE `id_prj` = '$id' AND `name_prj` = '$name' LIMIT 1";
	if (rowCount($query) == 1)
	{
		return true;
	}
return false;
}
 
// Pending payment confirmation
function paymentPendingThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. However, PayPal is still processing your payment at this time. Once PayPal confirms that they have completed processing your payment we will contact you again to confirm the details.\n\n If you have any questions please do not hesitate to contact us.\n\n ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Completed payment confirmation
function paymentCompletedThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Process the order
function processOrder($id, $amount)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
 
	//this will be used to send back the overall return status of this function
	//assume that initially the function will fail
	$result=NULL;
 
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	
	//save the returned value
	$result1 = executeQuery($query);
 
	//check for errors
	if( NULL === $result1 )
	{
		echo $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
	}
	else//query succeeded
	{
		// Retrieve the query data and compute
		$row = mysql_fetch_array($result1);
		$paid = $row['paid_prj'] + $amount;
		$due = $row['due_prj'] - $amount;
		$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
		// Update the database
		$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
 
		//save the returned value. If this succeeds, the return value will the
		//the total number of updated records
		$result = executeQuery($query2);
		
		//check for errors
		if(NULL===$result)
		{
			echo $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
		}
	}
return $result;
}
 
// Step 0. Record the transaction
ob_start();
echo date("D M j G:i:s T Y") . "\n";
print_r($_SERVER);
print_r($_POST);
$body = ob_get_clean();
file_put_contents("logs/IPN.txt", $body, FILE_APPEND);
 
// Step 1. Verify IPN with PayPal
$result = verifyIPN($_POST);
if ($result == 0)
{
	$subject = "FAKE IPN RECEIVED";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
else if ($result != 1)
{
	$subject = "Unable to validate IPN";
	$body = "If this payment notification is valid it will need to be manually processed\n $result\n $body";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 1.5. Check payment status
switch ($_POST['payment_status'])
{
	case "Completed":
		paymentCompletedThankYou($_POST['payer_email']);
		break;
	case "Pending":
		paymentPendingThankYou($_POST['payer_email']);
		break;
	default:
		$body = "Hi, an IPN was received that was neither a complete payment nor a pending payment. Please contact us to confirm this transaction against our records.";
		$body .= $post;
		$subject = "IPN Received";
		$address = "admin@emarketbuilders.com";
		$headers = 
			"From: admin@emarketbuilders.com\r\n" . 
			"Reply-To: donotreply@emarketbuilders.com\r\n" . 
			"X-Mailer: PHP/" . phpversion();
		mail($address, $subject, $body, $headers);
		exit;
}
 
// Step 2. Confirm product information
$result = confirmProduct($_POST['item_number'], $_POST['item_name'], $_POST['mc_gross']);
if ($result == false)
{
	$subject = "Product Name/ID/Price Error";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" .
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 3. Process the order
processOrder($_POST['item_number'], $_POST['mc_gross']);
exit;
 
?>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:

Select allOpen in new window

 

by: EMB01Posted on 2008-09-20 at 05:23:17ID: 22529399

I'll try these changes again but as you recall, I already tried these changes and there was some sort of "IPN delivery failed. HTTP error code 500: Internal Server Error" error and the IPN could not be sent to the URL. You said "probably there's a semicolon missing some where." If you have a sandbox account you can try it yourself at developer.paypal.com.

 

by: hieloPosted on 2008-09-20 at 10:59:41ID: 22530884

>>You said "probably there's a semicolon missing some where."
Yes, but it does NOT necessarily a semicolon. I was hinting a possible syntax error. After reading through all the code (yet again, perhaps my 15th time), I see line 85 of the last code I posted is missing a parenthesis:
if( preg_match('/^SELECT\s+/i',$query )

It should be:
if( preg_match('/^SELECT\s+/i',$query) )

>>If you have a sandbox
No I don't

 

by: EMB01Posted on 2008-09-20 at 11:43:35ID: 22531052

There is no more error from PayPal; but the DB still isn't being updated. I'm not sure what do to about this as the connection seems to be made (refer Step 2) and the script seems to be functional (other than the fact that is doesn't work in this case). What would you recommend?

 

by: hieloPosted on 2008-09-20 at 12:30:51ID: 22531216

start by putting echo statements that will tell you the returned values of the various functions.

<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL);
 
// SQL error reporting
function sql_error()
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	if(empty($MYSQL_ERROR))
	{
		$MYSQL_ERRNO = mysql_errno();
		$MYSQL_ERROR = mysql_error();
	}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
 
// Connection information
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
	global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
	if (!$link_id)
	{
		$MYSQL_ERRNO = 0;
		$MYSQL_ERROR = "Connection failed to the host.";
	return 0;
	}
	else if (empty($dbname) && !mysql_select_db($default_dbname))
	{
		$MYSQL_ERRNO = mysql_errno();
		$MYSQL_ERROR = mysql_error();
		return 0;
	} 
	else
		return $link_id;
}
 
/*
// MySQL query operations
function insertQuery($query)
{
	$link_id = db_connect();
	$messages = mysql_query($query, $link_id) or die(mysql_error());
	return $messages;
}
*/
// MySQL query operations
function executeQuery($query)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	$link_id = db_connect();
	
	//check for connection problems
	if(!$link_id)
	{	//simply return null since db_connect() already set the
		//$MYSQL_ERRNO and $MYSQL_ERROR;
		return NULL;
	}
 
	$query =trim($query);
	$messages = mysql_query($query, $link_id);
 
	//check if query failed and update global variables accordingly
	//return NULL so you know query failed. Then at the place where you are callin
	//executeQuery you need to decide whether you want to script to "die" or not
	if( !$messages )
	{
		$MYSQL_ERRNO =mysql_errno();
		$MYSQL_ERROR = 'Problems encountered while executing <br/>'. $query . '<br/>Error:' .  mysql_error();
	return NULL;
	}
 
	//if you made it this far, there were not problems executing the script
	//so now check if a SELECT query was requested and if so, return the recordset
	if( preg_match('/^SELECT\s+/i',$query) )
	{
		return $messages;
	}
 
	//Get the number of affected rows by the last INSERT, UPDATE, REPLACE or 
	//DELETE query associated with link_identifier
	//Example: if you executed an update statement, this will tell you how many
	//records were actually updated, which could be 1 or 100 or even zero!
	if( preg_match('/^(INSERT|UPDATE|DELETE|REPLACE)\s+/',$query) )
	{
		return mysql_affected_rows($link_id); 
	}
 
//for any other command, we just know the command succeeded
return TRUE;
}
 
 
// Row count function
function rowCount($query)
{
	$link_id = db_connect();
	$result = mysql_query($query);
	$rowCount = mysql_num_rows($result);
	return $rowCount;
}
 
// Validating the IPN with PayPal
function verifyIPN($data)
{
	$postdata = "";
	$response = array();
	
	foreach($data as $var=>$val)
	{
		$postdata .= $var . "=" . urlencode($val) . "&";
	}
	$postdata .="cmd=_notify-validate";
	$fp=@fsockopen("ssl://www.sandbox.paypal.com" ,"443" , $errnum, $errstr, 30);
	if(!$fp)
	{
		return "$errnum: $errstr";
	} else
	{
		fputs($fp, "POST /cgi-bin/webscr HTTP/1.0\r\n");
		fputs($fp, "Host: www.sandbox.paypal.com\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ".strlen($postdata)."\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $postdata . "\r\n\r\n");
		while(!feof($fp))
		{
			$response[]=@fgets($fp, 1024);
		}
		fclose($fp);
	}
	$response = implode("\n", $response);
	if(eregi("VERIFIED", $response))
	{
		return true;
	}
return false;
}
 
// Confirming product information
function confirmProduct($id, $name, $amount)
{
	if (!(ctype_digit("$id") && is_numeric("$amount")))
	{
		return false;
	}
	else
	{
		$name = "Payment for " . mysql_escape_string("$name");
	}
	$query = "SELECT id_prj FROM projects WHERE `id_prj` = '$id' AND `name_prj` = '$name' LIMIT 1";
	if (rowCount($query) == 1)
	{
		return true;
	}
return false;
}
 
// Pending payment confirmation
function paymentPendingThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. However, PayPal is still processing your payment at this time. Once PayPal confirms that they have completed processing your payment we will contact you again to confirm the details.\n\n If you have any questions please do not hesitate to contact us.\n\n ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Completed payment confirmation
function paymentCompletedThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Process the order
function processOrder($id, $amount)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
 
	//this will be used to send back the overall return status of this function
	//assume that initially the function will fail
	$result=NULL;
 
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	
	//save the returned value
	$result1 = executeQuery($query);
 
	//check for errors
	if( NULL === $result1 )
	{
		echo $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
	}
	else//query succeeded
	{
		// Retrieve the query data and compute
		$row = mysql_fetch_array($result1);
		$paid = $row['paid_prj'] + $amount;
		$due = $row['due_prj'] - $amount;
		$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
		// Update the database
		$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
 
		//save the returned value. If this succeeds, the return value will the
		//the total number of updated records
		$result = executeQuery($query2);
		
		//check for errors
		if(NULL===$result)
		{
			echo $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
		}
	}
return $result;
}
 
// Step 0. Record the transaction
ob_start();
echo date("D M j G:i:s T Y") . "\n";
print_r($_SERVER);
print_r($_POST);
$body = ob_get_clean();
file_put_contents("logs/IPN.txt", $body, FILE_APPEND);
 
// Step 1. Verify IPN with PayPal
$result = verifyIPN($_POST);
echo "verifyIPN() returned: " . $result;
if ($result == 0)
{
	$subject = "FAKE IPN RECEIVED";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
else if ($result != 1)
{
	$subject = "Unable to validate IPN";
	$body = "If this payment notification is valid it will need to be manually processed\n $result\n $body";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 1.5. Check payment status
echo "payment status".$_POST['payment_status'];
switch ($_POST['payment_status'])
{
	case "Completed":
		paymentCompletedThankYou($_POST['payer_email']);
		break;
	case "Pending":
		paymentPendingThankYou($_POST['payer_email']);
		break;
	default:
		$body = "Hi, an IPN was received that was neither a complete payment nor a pending payment. Please contact us to confirm this transaction against our records.";
		$body .= $post;
		$subject = "IPN Received";
		$address = "admin@emarketbuilders.com";
		$headers = 
			"From: admin@emarketbuilders.com\r\n" . 
			"Reply-To: donotreply@emarketbuilders.com\r\n" . 
			"X-Mailer: PHP/" . phpversion();
		mail($address, $subject, $body, $headers);
		exit;
}
 
// Step 2. Confirm product information
$result = confirmProduct($_POST['item_number'], $_POST['item_name'], $_POST['mc_gross']);
echo "confirmProducts() return" . $result;
if ($result == false)
{
	$subject = "Product Name/ID/Price Error";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" .
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 3. Process the order
$result = processOrder($_POST['item_number'], $_POST['mc_gross']);
 
echo "processOrder() returned:" . $result;
exit;
 
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:

Select allOpen in new window

 

by: EMB01Posted on 2008-09-20 at 13:54:12ID: 22531575

Since the this page is never directly accessed, would it be possible to email the results or log them into the log file? I ask this because, again, the page is never accessed directly so the echo command wouldn't be of much help. Unless, I could bypass the IPN validation section and only test Steps 2 and 3 using controlled variables? Sorry, I know this must complicate things... Thanks for your help.

 

by: hieloPosted on 2008-09-21 at 08:43:39ID: 22535033

>>would it be possible to email the results
yes. Use this script. Update the $yourEmail variable at the end of this script. Hopefully I don't have any typos.

<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL);
 
$DEBUG_MODE=TRUE;
$DEBUG_MSG="";
// SQL error reporting
function sql_error()
{
	global $MYSQL_ERRNO, $MYSQL_ERROR;
	if(empty($MYSQL_ERROR))
	{
		$MYSQL_ERRNO = mysql_errno();
		$MYSQL_ERROR = mysql_error();
	}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
 
// Connection information
$dbhost = '';
$dbusername = '';
$dbuserpassword = '';
$default_dbname = '';
$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';
$default_sort_order = 'ASC';
$defaualt_order_by = 'uid';
$records_per_page = '5';
function db_connect()
{
	global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
	global $MYSQL_ERRNO, $MYSQL_ERROR,$DEBUG_MODE,$DEBUG_MSG;
	if($DEBUG_MODE) $DEBUG_MSG .= "-> dbConnect()\n";
	$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
	if (!$link_id)
	{
		$MYSQL_ERRNO = 0;
		$MYSQL_ERROR = "Connection failed to the host." . mysql_error();
		if($DEBUG_MODE) $DEBUG_MSG .= "-----> mysql_connect() Line: ".__LINE__."; Error:{$MYSQL_ERROR}\n";
	return 0;
	}
	else if (empty($dbname) && !mysql_select_db($default_dbname))
	{
		if($DEBUG_MODE) $DEBUG_MSG .= "-----> mysql_connect() Line: ".__LINE__."; Status:OK\n";
		$MYSQL_ERRNO = mysql_errno();
		$MYSQL_ERROR = mysql_error();
		if($DEBUG_MODE) $DEBUG_MSG .= "-----> mysql_select_db() Line: ".__LINE__."; Error: {$MYSQL_ERROR}\n";
		return 0;
	}
	if($DEBUG_MODE) $DEBUG_MSG .= "-----> db_connect() Line: ".__LINE__."; Status: OK\n";
return $link_id;
}
 
// MySQL query operations
function executeQuery($query)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR,$DEBUG_MODE,$DEBUG_MSG;
	$link_id = db_connect();
	
	//check for connection problems
	if(!$link_id)
	{	//simply return null since db_connect() already set the
		//$MYSQL_ERRNO and $MYSQL_ERROR;
		return NULL;
	}
	$query =trim($query);
	if($DEBUG_MODE) $DEBUG_MSG .= "-> executeQuery('{$query}') Line: ".__LINE__.";\n";
	$messages = mysql_query($query, $link_id);
 
	//check if query failed and update global variables accordingly
	//return NULL so you know query failed. Then at the place where you are callin
	//executeQuery you need to decide whether you want to script to "die" or not
	if( !$messages )
	{
		$MYSQL_ERRNO =mysql_errno();
		$MYSQL_ERROR = 'Problems encountered while executing <br/>'. $query . '<br/>Error:' .  mysql_error();
		if($DEBUG_MODE) $DEBUG_MSG .= "-----> executeQuery('...') Line: ".__LINE__."; Error: {$MYSQL_ERROR}\n";
	return NULL;
	}
 
	if($DEBUG_MODE) $DEBUG_MSG .= "-----> executeQuery('...') Line: ".__LINE__."; Status: OK\n";
 
	//if you made it this far, there were not problems executing the script
	//so now check if a SELECT query was requested and if so, return the recordset
	if( preg_match('/^SELECT\s+/i',$query) )
	{
		return $messages;
	}
 
	//Get the number of affected rows by the last INSERT, UPDATE, REPLACE or 
	//DELETE query associated with link_identifier
	//Example: if you executed an update statement, this will tell you how many
	//records were actually updated, which could be 1 or 100 or even zero!
	if( preg_match('/^(INSERT|UPDATE|DELETE|REPLACE)\s+/',$query) )
	{
		return mysql_affected_rows($link_id); 
	}
 
//for any other command, we just know the command succeeded
return TRUE;
}
 
 
// Row count function
function rowCount($query)
{
	global $DEBUG_MODE,$DEBUG_MSG;
	if($DEBUG_MODE) $DEBUG_MSG .= "-> rowCount('{$query}') Line: ".__LINE__."\n";
	$link_id = db_connect();
	$result = mysql_query($query);
	if( !result )
	{
		if($DEBUG_MODE) $DEBUG_MSG .= "-----> rowCount('...') Line: ".__LINE__."; Error:".mysql_error()."\n";
	}
	$rowCount = mysql_num_rows($result);
	return $rowCount;
}
 
// Validating the IPN with PayPal
function verifyIPN($data)
{
	global $DEBUG_MODE,$DEBUG_MSG;
	$postdata = "";
	$response = array();
	
	foreach($data as $var=>$val)
	{
		$postdata .= $var . "=" . urlencode($val) . "&";
	}
	$postdata .="cmd=_notify-validate";
	if($DEBUG_MODE) $DEBUG_MSG .= "-> verifyIPN('{$postdata}') Line: ".__LINE__."\n";
 
	$fp=@fsockopen("ssl://www.sandbox.paypal.com" ,"443" , $errnum, $errstr, 30);
	if(!$fp)
	{
		if($DEBUG_MODE) $DEBUG_MSG .= "-----------> fsockopen('ssl://www.sandbox.paypal.com' ,'443') Line: ".__LINE__."; Error: $errnum; $errstr\n";
		return "$errnum: $errstr";
	} 
	else
	{
		if($DEBUG_MODE) $DEBUG_MSG .= "-----------> fsockopen('ssl://www.sandbox.paypal.com' ,'443') Line: ".__LINE__."; Status: OK\n";
		fputs($fp, "POST /cgi-bin/webscr HTTP/1.0\r\n");
		fputs($fp, "Host: www.sandbox.paypal.com\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ".strlen($postdata)."\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $postdata . "\r\n\r\n");
		while(!feof($fp))
		{
			$response[]=@fgets($fp, 1024);
		}
		fclose($fp);
	}
	$response = implode("\n", $response);
	if(eregi("VERIFIED", $response))
	{
		if($DEBUG_MODE) $DEBUG_MSG .= "-> verifyIPN('{$postdata}') Line: ".__LINE__."; Status: VERIFIED\n";
		return true;
	}
	if($DEBUG_MODE) $DEBUG_MSG .= "-> verifyIPN('{$postdata}') Line: ".__LINE__."; Status: NOT VERIFIED\n";
return false;
}
 
// Confirming product information
function confirmProduct($id, $name, $amount)
{
	if (!(ctype_digit("$id") && is_numeric("$amount")))
	{
		return false;
	}
	else
	{
		$name = "Payment for " . mysql_escape_string("$name");
	}
	$query = "SELECT id_prj FROM projects WHERE `id_prj` = '$id' AND `name_prj` = '$name' LIMIT 1";
	if (rowCount($query) == 1)
	{
		return true;
	}
return false;
}
 
// Pending payment confirmation
function paymentPendingThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. However, PayPal is still processing your payment at this time. Once PayPal confirms that they have completed processing your payment we will contact you again to confirm the details.\n\n If you have any questions please do not hesitate to contact us.\n\n ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Completed payment confirmation
function paymentCompletedThankYou($address)
{
	$subject = "Order Received";
	$body = "Thanks for your payment to EMB!\n This automated message confirms that we have received notifications from PayPal regarding your payment. ";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: admin@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Process the order
function processOrder($id, $amount)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR,$DEBUG_MODE,$DEBUG_MSG;
 
	//this will be used to send back the overall return status of this function
	//assume that initially the function will fail
	$result=NULL;
 
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	
	//save the returned value
	$result1 = executeQuery($query);
 
	//check for errors
	if( NULL === $result1 )
	{
		if($DEBUG_MODE) $DEBUG_MSG .= "result after executing processOrder({$id},{$amount}) Line".__LINE__.": ".$MYSQL_ERROR . " - " . $MYSQL_ERRNO;
	}
	else//query succeeded
	{
		// Retrieve the query data and compute
		$row = mysql_fetch_array($result1);
		$paid = $row['paid_prj'] + $amount;
		$due = $row['due_prj'] - $amount;
		$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
		// Update the database
		$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
 
		//save the returned value. If this succeeds, the return value will the
		//the total number of updated records
		$result = executeQuery($query2);
		
		//check for errors
		if(NULL===$result)
		{
			if($DEBUG_MODE) "result after execQuery('{$query2}') Line:" . __LINE__ ."; ".$DEBUG_MSG .= $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
		}
	}
return $result;
}
 
// Step 0. Record the transaction
ob_start();
echo date("D M j G:i:s T Y") . "\n";
print_r($_SERVER);
print_r($_POST);
$body = ob_get_clean();
file_put_contents("logs/IPN.txt", $body, FILE_APPEND);
 
// Step 1. Verify IPN with PayPal
$result = verifyIPN($_POST);
if($DEBUG_MODE) $DEBUG_MSG .= "verifyIPN() returned: " . $result;
if ($result == 0)
{
	$subject = "FAKE IPN RECEIVED";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
else if ($result != 1)
{
	$subject = "Unable to validate IPN";
	$body = "If this payment notification is valid it will need to be manually processed\n $result\n $body";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" . 
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 1.5. Check payment status
if($DEBUG_MODE) $DEBUG_MSG .= "payment_status: ".$_POST['payment_status'];
switch ($_POST['payment_status'])
{
	case "Completed":
		paymentCompletedThankYou($_POST['payer_email']);
		break;
	case "Pending":
		paymentPendingThankYou($_POST['payer_email']);
		break;
	default:
		$body = "Hi, an IPN was received that was neither a complete payment nor a pending payment. Please contact us to confirm this transaction against our records.";
		$body .= $post;
		$subject = "IPN Received";
		$address = "admin@emarketbuilders.com";
		$headers = 
			"From: admin@emarketbuilders.com\r\n" . 
			"Reply-To: donotreply@emarketbuilders.com\r\n" . 
			"X-Mailer: PHP/" . phpversion();
		mail($address, $subject, $body, $headers);
		exit;
}
 
// Step 2. Confirm product information
$result = confirmProduct($_POST['item_number'], $_POST['item_name'], $_POST['mc_gross']);
if($DEBUG_MODE) $DEBUG_MSG .= "confirmProducts() returned " . $result;
if ($result == false)
{
	$subject = "Product Name/ID/Price Error";
	$address = "admin@emarketbuilders.com";
	$headers = 
		"From: admin@emarketbuilders.com\r\n" . 
		"Reply-To: donotreply@emarketbuilders.com\r\n" .
		"X-Mailer: PHP/" . phpversion();
	mail($address, $subject, $body, $headers);
	exit;
}
 
// Step 3. Process the order
$result = processOrder($_POST['item_number'], $_POST['mc_gross']);
 
echo "processOrder() returned:" . $result;
 
if($DEBUG_MODE){
	$yourEmail='you@yourcompany.com';
	mail($yourEmail, 'Program Trace', $DEBUG_MSG, 'From: admin@emarketbuilders.com\r\nTo:'.$yourEmail);
}
exit; 
?>
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:

Select allOpen in new window

 

by: EMB01Posted on 2008-09-22 at 06:28:38ID: 22539730

Thanks, there didn't seem to be any syntax errors. I updated the variable, the line looks like:
$yourEmail='admin@emarketbuilders.com';

No email was sent; however. I do get the Order Received email, but not the Program Trace.

 

by: hieloPosted on 2008-09-22 at 07:19:02ID: 22540201

>>I do get the Order Received email
OK, well step 1.5 has a switch with:
      case "Completed":
            paymentCompletedThankYou($_POST['payer_email']);
            break;
      case "Pending":
            paymentPendingThankYou($_POST['payer_email']);

both of which have the same subject line "Order Received". Also both of those functions:
paymentCompletedThankYou() AND paymentPendingThankYou() have exit at the end. So, regardless of which one is sending the email, the problem is that when "exit" is executed, the program quits completely. So it never makes it to step 2. I don't know why you put exit on those functions but if you remove "exit", it will continue onto the next step.

 

by: EMB01Posted on 2008-09-22 at 09:20:11ID: 22541397

Wow! That was the problem basically the whole time... Just one more thing - the database gets updated, except the data isn't concatenated. My guess (if it's of any relevance) is that the $row variables are never defined. Here's the function (attached):

function processOrder($id, $amount)
{
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	insertQuery($query);
 
	// Retrieve the query data and compute
	$row = mysql_fetch_array($result1);
	$paid = $row['paid_prj'] + $amount;
	$due = $row['due_prj'] - $amount;
	$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
	// Update the database
	$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
	insertQuery($query2);
	exit;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:

Select allOpen in new window

 

by: EMB01Posted on 2008-09-22 at 09:33:47ID: 22541510

Wait, let me update that with a revised code.

 

by: EMB01Posted on 2008-09-22 at 09:38:36ID: 22541565

Okay that does it! Here's the code as you revised (attached):

function processOrder($id, $amount)
{
	global $MYSQL_ERRNO, $MYSQL_ERROR,$DEBUG_MODE,$DEBUG_MSG;
 
	//this will be used to send back the overall return status of this function
	//assume that initially the function will fail
	$result=NULL;
 
	// Make the inputs safe from SQL injection hacking
	$id = mysql_real_escape_string($id);
	$amount = mysql_real_escape_string($amount);
 
	// Query for the data
	$query = "SELECT * FROM projects WHERE `id_prj` = $id";
	
	//save the returned value
	$result1 = executeQuery($query);
 
	//check for errors
	if( NULL === $result1 )
	{
		if($DEBUG_MODE) $DEBUG_MSG .= "result after executing processOrder({$id},{$amount}) Line".__LINE__.": ".$MYSQL_ERROR . " - " . $MYSQL_ERRNO;
	}
	else//query succeeded
	{
		// Retrieve the query data and compute
		$row = mysql_fetch_array($result1);
		$paid = $row['paid_prj'] + $amount;
		$due = $row['due_prj'] - $amount;
		$notes = $row['notes_prj'] . "A payment of $" . number_format($amount, 2) . " was made on " . date('l, F j, Y') . " at " . date('g:i A T') . ". ";
 
		// Update the database
		$query2 = "UPDATE projects SET `paid_prj` = '$paid', `due_prj` = '$due', `notes_prj` = '$notes' WHERE `id_prj` = $id LIMIT 1";
 
		//save the returned value. If this succeeds, the return value will the
		//the total number of updated records
		$result = executeQuery($query2);
		
		//check for errors
		if(NULL===$result)
		{
			if($DEBUG_MODE) "result after execQuery('{$query2}') Line:" . __LINE__ ."; ".$DEBUG_MSG .= $MYSQL_ERROR . "<br/>" . $MYSQL_ERRNO;
		}
	}
return $result;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:

Select allOpen in new window

 

by: EMB01Posted on 2008-09-22 at 09:39:28ID: 31490794

Great job, thanks for sticking with me through this long, drawn-out question. Have a great day!

 

by: hieloPosted on 2008-09-22 at 09:54:48ID: 22541741

Glad it all worked out. Take care.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...