PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am on the final touches on my project and there's this one big dilemma.
I have to subtract the value of time_in (the value is saved to a database as a DATETIME upon clicking the LOGIN button) from time_out (the value is saved to the same database as DATETIME also upon clicking the LOGOUT button). Well, here's the code:
$a=mysql_query("SELECT * FROM Backup WHERE status = 0");
while ($stat=mysql_fetch_array($a));
{
if ($stat['status'] = 0) // Status being zero means that the user opted to logout, therefore clicking the LOGOUT button
{
$date1=$stat['time_in'];
$date2=$stat['time_out'];
$dat2=strtotime($date2);
$dat3=strtotime($date1);
$interval=$dat2-$dat3;
$sql="UPDATE Persons SET elapse = $interval WHERE time_out = $date2";
$sql2="DELETE * FROM Backup";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
if (!mysql_query($sql2,$con))
{
die('Error: ' . mysql_error());
}
}
}
<?php
/* Be specific when you fetch results from a database! */
$a = mysql_query("SELECT `time_in`, `time_out` FROM `Backup` WHERE `status` = '0';");
/* Why is there a semi-colon here? */
while ($stat = mysql_fetch_array($a));
{
/* This is a useless check. Look at the query, we only selected rows where status = 0 */
//if ($stat['status'] = 0) { // Status being zero means that the user opted to logout, therefore clicking the LOGOUT button
/*
// This is confusing!
$date1 = $stat['time_in'];
$date2 = $stat['time_out'];
$dat2 = strtotime($date2);
$dat3 = strtotime($date1);
$interval = $dat2 - $dat3;
*/
$interval = strtotime($stat['time_out']) - strtotime($stat['time_in']);
/* You should escape all data before running it through a query */
/* For an operation like this, you should be referencing a unique id in the table. */
$sql = "UPDATE `Persons` SET `elapse` = '$interval' WHERE `time_out` = '$date2';";
$sql2 = "DELETE * FROM `Backup`;";
if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
if (!mysql_query($sql2,$con))
die('Error: ' . mysql_error());
//}
}
?>
If this were my project, I'd probably leverage mysql datetime functions:
Thing is, it does not compute. When I echo the value of the timediff, it does not show. Even when I look at my database, the value is never computed. Help, anyone? I'm excited seeing this the only thing that is not working in the project.