davesmith437 Posted November 2, 2010 Share Posted November 2, 2010 Hi, Apologies if this is extremely trivial, I'm new to PHP. I'm trying to convert a timesheet application which I've had as an Access database into a Mysql/php driven website to get it out of the constraints of a VPN and in to the open where it will be much easier to work with. I've got the data entry working fine but have run into problems trying to get the SQL right to add up the hours each person spend actually working. Each user logs in and sets a week number to record their work - these are both stored as session variables and appear on the forms automatically. My problem starts on the summary page. Each user needs to be able to see an overview of the work they done and how many hours they spent working / travelling. I've managed to get the page to calculate and display the time spend but I can't get it to display the data just for the logged in user and week number they are reviewing. My code is as follows: <?php $sql = "SELECT HoursSpentLas, TravelTimeLas FROM tblProjects WHERE AdviserName = $MM_Username AND WeekNumber = $MM_Weeknumber"; $query = mysql_query($sql); while($row = mysql_fetch_assoc($query)){ echo $query['HoursSpentLas']; } $TimeWorkedLAs = "SELECT HoursSpentLas, SUM(HoursSpentLas) FROM tblProjects GROUP BY WeekNumber"; $result = mysql_query($TimeWorkedLAs) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "Total = ". $row['SUM(HoursSpentLas)']; } ?> Currently, its adding up fine but displaying the results for everyone. Any suggestions gratefully received! Many thanks Dave Quote Link to comment https://forums.phpfreaks.com/topic/217555-sum-with-where-clause-using-session-variables/ Share on other sites More sharing options...
sasa Posted November 8, 2010 Share Posted November 8, 2010 change $sql = "SELECT HoursSpentLas, TravelTimeLas FROM tblProjects WHERE AdviserName = $MM_Username AND WeekNumber = $MM_Weeknumber"; to $sql = "SELECT HoursSpentLas, TravelTimeLas FROM tblProjects WHERE AdviserName = '$MM_Username' AND WeekNumber = $MM_Weeknumber"; add qoutes around string data Quote Link to comment https://forums.phpfreaks.com/topic/217555-sum-with-where-clause-using-session-variables/#findComment-1131662 Share on other sites More sharing options...
davesmith437 Posted November 9, 2010 Author Share Posted November 9, 2010 Thanks Sasa, it now works Quote Link to comment https://forums.phpfreaks.com/topic/217555-sum-with-where-clause-using-session-variables/#findComment-1132171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.