PHP Looped Function Will Not Send Multiple Emails

I am working on an osCommerce site which offers a few products and a variety of music classes. Each product and class is processed in the usual way, but an additional process is added which is supposed to send a separate email for each item or class purchased. The emails contain details about the product or the class. The function, doemail(), is attached. The problem is that when a user orders multiple items, only the email for the first item ordered is sent. The other items appear to be ignored.

The function first searches the database for the items contained in the order. It then enters a while loop. Inside the loop, a switch statement assigns values for various parameters associated with the item referenced in the row being examined. Next, a series of conditional routines, compose the message to be sent in the email. Finally, the email is supposed to be sent and the loop begins again with the next row.

As noted, the function works well for the first row, but fails to function after that. No error messages are generated on-screen.
customfunctions.php
LVL 1
gpinzinoAsked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
This is most likely because your second database query (the one inside the WHILE loop) is overwriting the $result variable, which contains the outer loop.

Try changing:
            $sql = "SELECT customers_email_address FROM orders WHERE orders_id='$order_id'";
            $result = mysql_query ( $sql, $$link ) or die ( "Oops, I can't look up the email address!");
            $to = mysql_result ( $result, 0 );

to:
            $sql3 = "SELECT customers_email_address FROM orders WHERE orders_id='$order_id'";
            $result3 = mysql_query ( $sql3, $$link ) or die ( "Oops, I can't look up the email address!");
            $to = mysql_result ( $result3, 0 );
0
 
Julian HansenConnect With a Mentor Commented:
Further to what gr8gonzo said.

I might be missing something here but why do you keep looking up the clients email address in the loop?

It makes more sense for you to get the email address outside the main loop - save what you need and then continue with your loop. Otherwise as gr8gonzo said you are going to be wiping the $result value and performing unnecessary lookups on what should be a loop invariant value.
0
 
gpinzinoAuthor Commented:
You guys are good. I did not write this code, and I was going up a blind alley trying to debug it. I have now made the SQL change and moved it out of the loop and it works great now. Thank you both for your help.
0
 
Julian HansenCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.