Results 1 to 12 of 12

Thread: Need help with php please

  1. #1
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Need help with php please

    Hi all, if someone could help me fix an issue i'm having on a php website i would be very appreciative! Basically, one part of the php sends an email reminder for a meeting. It works correctly although it sends 2 copies of the emails to each recipient, which is not what i would like. The code for the bit that sends the reminder is below, hopefully it's enough, it's a group coursework and wasn't written by me, but none of us have been able to fix it yet.

    Code:
    if($remind==1)
    {
    list($year, $month, $day) = split('-', $odate);
    $odue=$day."/".$month."/".$year;
    
    #Sends e-mail reminder
    $mailmeeting="SELECT s.StudentName, s.Email, me.Date, me.Time, me.Place, g.GroupName, m.ManagerEmail
    FROM `Meeting` me, `Student` s, `Group` g, `GroupMember` gm, `ManagerGroup` mg, `Manager` m
    WHERE m.ManagerID = '$managerid'
    AND mg.ManagerID = m.ManagerID
    AND me.ManagerID = mg.ManagerID
    AND g.GroupID = $groupid
    AND gm.GroupID = g.GroupID
    AND mg.GroupID = mg.GroupID
    AND me.GroupID = gm.GroupID
    AND me.Time='$otime'
    AND me.Date='$odate'
    AND me.Place='$oplace'
    AND s.StudentID = gm.StudentID";
    
    $mailmeetings = mysql_query($mailmeeting,$connection);
    
    $notify=1;
    
    while($row=mysql_fetch_array($mailmeetings))
    {
    	$to = "$row[1]";
    	$subject = "COA281 - Reminder for Meeting for $odue at $row[3] in $row[4].";
    	$message = "This is to remind you that your group $row[5] has a meeting scheduled for $odue at $otime in $oplace.";
    	$from = "$row[6]";
    	$headers = "From: $from";
    	mail($to,$subject,$message,$headers);
    	if($notify==1)
    	{
    		$to = "$row[6]";
    		$subject = "COA281 - Reminder for Meeting for $odue at $row[3] in $row[4]. ";
    		$message = "This is to remind you that you have scheduled a meeting for group $row[5] at the above date, time and place.";
    		$from = "$row[6]";
    		$headers = "From: $from";
    		mail($to,$subject,$message,$headers);
    		$notify=$notify+1;
    	}
    }
    
    echo"<p>Reminder sent.<p>";
    }
    I'm not sure this piece of code is the culprit because it is very similar to other pieces that work, i was wondering if maybe the page was loading twice, sending the emails twice, but i can't see how this would happen. I tried putting counters in the if statement to see how many times things were happening and the values the showed indicated that the if statement was performing correctly and that the correct number of records are being retrieved from the database etc, so maybe the page is loading twice. If anyone can help, again i would be very appreciative, this is driving me crazy and i need to get on with other work. Oh i've only been doing php/mysql for a few weeks so sorry if there's any glaring mistakes!

    Thanks

    Andy

  2. #2
    www.dougmcdonald.co.uk
    Join Date
    May 2007
    Location
    Bath
    Posts
    523
    Thanks
    5
    Thanked
    20 times in 20 posts
    • DougMcDonald's system
      • Motherboard:
      • Asus P5B Deluxe
      • CPU:
      • Inter Core 2 Duo E6600
      • Memory:
      • 2 x 2GB - Geil Black Dragon PC6400
      • Storage:
      • 2 x 400GB Samsung Spinpoints (Running in Matrix array) 100GB @ RAID0 + 300GB @ RAID1
      • Graphics card(s):
      • BFG nVidia 8800GTS 320MB OC2
      • PSU:
      • Corsair HX520W modular
      • Case:
      • Lian-Li PC7 II Plus
      • Monitor(s):
      • LG 17" Flat Thingy
      • Internet:
      • Crappy BT 1MB Unreliable wank :s

    Re: Need help with php please

    Looking at this quickly, the key pieces to look at would be the php 'mail()' function calls of which there are two.

    Looking at the code, one will be executed by default the one just before the line if($notify==1)

    The other will be executed if the condition if($notify==1) returns true...which it always will because you've set it to 1 just after performing the SQL query.

    As a result two mails would be sent with what would look to be very similar content.

    Try changing either one of the '$subject' lines to something totally different (so you can differentiate between the two mails) and see if you are getting both mails being sent, or if you are getting two copies of the first (or second)

    Unless I've misunderstood something, this could be a problem

  3. #3
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Re: Need help with php please

    the first email goes to multiple recipients in a group and the second - in the "if" statement goes to the manager, or at least it should at the moment it's sending two copies of both messages.

  4. #4
    www.dougmcdonald.co.uk
    Join Date
    May 2007
    Location
    Bath
    Posts
    523
    Thanks
    5
    Thanked
    20 times in 20 posts
    • DougMcDonald's system
      • Motherboard:
      • Asus P5B Deluxe
      • CPU:
      • Inter Core 2 Duo E6600
      • Memory:
      • 2 x 2GB - Geil Black Dragon PC6400
      • Storage:
      • 2 x 400GB Samsung Spinpoints (Running in Matrix array) 100GB @ RAID0 + 300GB @ RAID1
      • Graphics card(s):
      • BFG nVidia 8800GTS 320MB OC2
      • PSU:
      • Corsair HX520W modular
      • Case:
      • Lian-Li PC7 II Plus
      • Monitor(s):
      • LG 17" Flat Thingy
      • Internet:
      • Crappy BT 1MB Unreliable wank :s

    Re: Need help with php please

    Ah ok, yes I see, missed the different $to variables.

    If there are two emails getting sent, it suggests that this line:

    PHP Code:
    while($row=mysql_fetch_array($mailmeetings)) 
    is being evaluated to true on two occasions.

    Can you paste the output from the SQL query here?

  5. #5
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Re: Need help with php please

    sorry, i've not done that before, how would i get the sql output?

  6. #6
    www.dougmcdonald.co.uk
    Join Date
    May 2007
    Location
    Bath
    Posts
    523
    Thanks
    5
    Thanked
    20 times in 20 posts
    • DougMcDonald's system
      • Motherboard:
      • Asus P5B Deluxe
      • CPU:
      • Inter Core 2 Duo E6600
      • Memory:
      • 2 x 2GB - Geil Black Dragon PC6400
      • Storage:
      • 2 x 400GB Samsung Spinpoints (Running in Matrix array) 100GB @ RAID0 + 300GB @ RAID1
      • Graphics card(s):
      • BFG nVidia 8800GTS 320MB OC2
      • PSU:
      • Corsair HX520W modular
      • Case:
      • Lian-Li PC7 II Plus
      • Monitor(s):
      • LG 17" Flat Thingy
      • Internet:
      • Crappy BT 1MB Unreliable wank :s

    Re: Need help with php please

    A couple of ways really.

    If you have something like SQL Server management studio you can just run your query in that. If using mySQL or something like that, then you'd need to echo your sql query variable, in this case:

    PHP Code:
    print($mailmeeting); 
    And this would write the query you are using to the page you are viewing, then you can copy and paste this into something like the phpMyAdmin console where you can select a database and then use the 'Query' tab to perform a query.

    It will return the results (rows etc) below, which you can paste into this post.

    What DB is it? and what access do you have to it?

  7. #7
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Re: Need help with php please

    We're using phpMyAdmin on a university server using mysql version 5.0.24. I just did what you said using the phpmyadmin console.

    The query was -

    Code:
    SELECT s.StudentName, s.Email, me.Date, me.Time, me.Place, g.GroupName, m.ManagerEmail FROM `Meeting` me, `Student` s, `Group` g, `GroupMember` gm, `ManagerGroup` mg, `Manager` m WHERE m.ManagerID = '1' AND mg.ManagerID = m.ManagerID AND me.ManagerID = mg.ManagerID AND g.GroupID = 64 AND gm.GroupID = g.GroupID AND mg.GroupID = gm.GroupID AND me.GroupID = gm.GroupID AND me.Time='10:00:00' AND me.Date='2007-12-10' AND me.Place='n018' AND s.StudentID = gm.StudentID
    which gave the three rows that i expected it to give, and all the data was as i expected, so maybe it's the whole if statement that is being executed twice, but i can't see why this would be, because there is no code that would reload the page that i can see

  8. #8
    www.dougmcdonald.co.uk
    Join Date
    May 2007
    Location
    Bath
    Posts
    523
    Thanks
    5
    Thanked
    20 times in 20 posts
    • DougMcDonald's system
      • Motherboard:
      • Asus P5B Deluxe
      • CPU:
      • Inter Core 2 Duo E6600
      • Memory:
      • 2 x 2GB - Geil Black Dragon PC6400
      • Storage:
      • 2 x 400GB Samsung Spinpoints (Running in Matrix array) 100GB @ RAID0 + 300GB @ RAID1
      • Graphics card(s):
      • BFG nVidia 8800GTS 320MB OC2
      • PSU:
      • Corsair HX520W modular
      • Case:
      • Lian-Li PC7 II Plus
      • Monitor(s):
      • LG 17" Flat Thingy
      • Internet:
      • Crappy BT 1MB Unreliable wank :s

    Re: Need help with php please

    It returns three rows?

    In terms of the project, would these represent three different meetings?

    If so, have you checked to ensure that it's not just co-incidence that the $to emails are the same in two out of the three rows returned?

    The three rows would indicate a maximum of 6 emails sent looking at the code, but it may be a level higher that's causing the problem.

    Would you be able to either paste the three rows returned, or elaborate on how this code is called?

  9. Received thanks from:

    kasavien (10-12-2007)

  10. #9
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Re: Need help with php please

    Each of the 3 rows returned contains the details of a student that the email is supposed to be sent to. So three emails should be sent to those students, and a fourth sent to their manager. At the moment there are 6 emails being sent to students and 2 emails sent to the manager, so it seems like its sending the first four then somehow sending the same four again. Unfortunately i can't really post the query result because of what it contains.

  11. #10
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,283
    Thanks
    293
    Thanked
    841 times in 476 posts

    Re: Need help with php please

    Here's a tip:

    Get the script to print out all the queries it's running (an echo of the $var with the query text in is fine) then whack them into phpmyadmin and run them yourself... see what results come up - tweak the query and work out what's going wrong.

    You'll at least then see if you're getting more rows back than you should (and perhaps you need to use SELECT DISTINCT somewhere, or an extra clause) or if the semantics of some of your PHP isn't quite right.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  12. Received thanks from:

    kasavien (11-12-2007)

  13. #11
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: Need help with php please

    PHP + MySQL The natural home of the injection attack.

    Please tell me your validating the parameters properly, because that snippet (and magic variables) means that its easy to trash your database/extract info i shouldn't.
    throw new ArgumentException (String, String, Exception)

  14. Received thanks from:

    kasavien (11-12-2007)

  15. #12
    Senior Member kasavien's Avatar
    Join Date
    Aug 2005
    Location
    St. Albans
    Posts
    1,829
    Thanks
    145
    Thanked
    104 times in 49 posts

    Re: Need help with php please

    I've got the problem sorted now, it was a problem with some buttons on another page, that when clicked went to the page that sent the emails, they were somehow loading the page twice.

    TheAnimus - we haven't done parameter validation because this website is only going to be used by 3 maybe 4 people, and we've not had the time to add in things like that. So the site is quite rough and ready. If i do any more php/mysql it is something i will look into though, or to be honest any future websites i do will probably use asp.net if they need to be dynamic because it looks quite interesting.

    Thanks though for the tips on using phpmyadmin to check sql queries, it's something i hadn't come across before.

    Thanks again everyone for your help

    Andy

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PHP and SQL Server on IIS
    By DougMcDonald in forum Software
    Replies: 8
    Last Post: 27-07-2007, 11:41 AM
  2. MySQL extension for PHP 5.2.3 not working
    By Jerrythafast in forum Help! Quick Relief From Tech Headaches
    Replies: 18
    Last Post: 13-06-2007, 08:03 PM
  3. php noob
    By j.o.s.h.1408 in forum Software
    Replies: 15
    Last Post: 23-05-2007, 10:37 AM
  4. PHP and file uploads timing out too soon
    By McClane in forum Software
    Replies: 12
    Last Post: 02-12-2006, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •