PHP Looped Function Will Not Send Multiple Emails

Posted on 2012-08-29
Last Modified: 2012-08-29
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.
Question by:gpinzino
    LVL 34

    Accepted Solution

    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 );

                $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 );
    LVL 49

    Assisted Solution

    by:Julian Hansen
    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.
    LVL 1

    Author Closing Comment

    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.
    LVL 49

    Expert Comment

    by:Julian Hansen
    You are welcome.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now