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
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
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 :P at the moment it's sending two copies of both messages.
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?
Re: Need help with php please
sorry, i've not done that before, how would i get the sql output?
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?
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
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?
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.
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.
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.
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