Jump to content

[SOLVED] Timeclock Database Help


techiefreak05

Recommended Posts

I was give the task of creating a simple employee Timeclock script to make for a client of mine.

I have a database setup with a table called "employees" that hold all the employees information, and I have a "timePunch" table to store the time punch information of each employee. I am confused on the best way to store the time punch information.

 

What is the best way to store, employee id, and current date/time into the database and be later retrieved from the database.

Link to comment
Share on other sites

Store the timepunch as a DATETIME column. Store NOW() into it when the employee call the time punch function in your script.

 

[pre]

employee        timepunch                  activity

--------        -----------                ----------

id    ---+      id                    +--- id

name      |      timepunch (datetime)  |    act_name

etc      +---<  empID                  |

                activityID      >-----+

Link to comment
Share on other sites

I threw in Activity as an optional extra. I didn't know if you just wanted to record in/out or whether you wanted a time booking system to record time spent on various client or project activities throughout each day.

 

If all you need is IN/OUT then forget the activity table and the activityID in the timepunch table would simply be IN or OUT.

Link to comment
Share on other sites

If there are no records today for the user then it's an IN.

 

If the last was IN then next should be OUT and vice versa. But you may have to allow for situations where the user forgot, or was unable, to clock in or out.

 

For example, what if an employee goes direct to a client's office at 9am and arrives back at base at 12 noon. He now wants to clock out for lunch, but even though he started at 9am he hasn't clocked in yet? If you do it automatically because there is no clocking today then you clock him in 3 hours late.

Link to comment
Share on other sites

I get it.

But is there any obviou reason why this code doesnt work? I dont get any errors at all, but it doesnt insert any rows.

 

<?php
ob_start();
session_start();

include 'db.php';

if($_POST["doPunch"]){
   if(!$_POST['emp_id'] || !$_POST['emp_pw']){
      header("location: index.php?er=1");
      exit;
   }
   $_POST['emp_id'] = trim($_POST['emp_id']);

   $md5pass = md5($_POST['emp_pw']);
   $result = confirmUser($_POST['emp_id'], $md5pass);

   if($result == 1){
      header("location: index.php?er=2");
      exit;
   }
   else if($result == 2){
      header("location: index.php?er=3");
      exit;
   }


$checkLastPunch=mysql("SELECT * from timePunch WHERE empID = '$_POST[emp_id]' ORDER BY ID DESC LIMIT 1") or die(mysql_error());
while($r=mysql_fetch_assoc($checkLastPunc)){

if($r["activityID"] == "In"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'Out'
);") or die(mysql_error());
}else if($r["activityID"] == "Out"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());
}else{
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());

}

}
}

?>

Link to comment
Share on other sites

I did some modifications, but it's still not doing anything.

 

<?php
ob_start();
session_start();

include 'db.php';

if($_POST["doPunch"]){
   if(!$_POST['emp_id'] || !$_POST['emp_pw']){
      header("location: index.php?er=1");
      exit;
   }
   $_POST['emp_id'] = trim($_POST['emp_id']);

   $md5pass = md5($_POST['emp_pw']);
   $result = confirmUser($_POST['emp_id'], $md5pass);

   if($result == 1){
      header("location: index.php?er=2");
      exit;
   }
   else if($result == 2){
      header("location: index.php?er=3");
      exit;
   }


$cc=mysql("SELECT * from timePunch WHERE empID = '$_POST[emp_id]' ORDER BY `ID` DESC") or die(mysql_error());

if(mysql_num_rows($cc)<1){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES ('$_POST[emp_id]', 'Out');") or die(mysql_error());
}else{

$r=mysql_fetch_assoc($cc);

if($r["activityID"] == "In"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'Out'
);") or die(mysql_error());
}else if($r["activityID"] == "Out"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());
}else{
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());
}


}
}

?>

 

Link to comment
Share on other sites

Are you using phpmyadmin or have access to the mysql DB? I would try executing your queries on the database directly using an arbitrary Employee ID to make sure your query is working.

 

I would change your code to do the following:

 

Select only the columns you need (activityID) and timepunch where employee id = $_POST['empid] AND timepunch = date(curdate())

 

then check amount of mysql_num_rows():

if 0, then not punched in today. Punch them in

if 1+, check to make sure time is before current time and if they are punched in/out, then punch them in again (like returning from lunch) or punch out

 

You could also add certain restrictions for for each employee. For example, if someone tries to punch in/out before/after certain times perform certain actions. This is just a suggestion and you would obviously have to consult your client.

 

Sorry for providing any code. I would suggest a few other things:

Change activityID to a tinyint column and use 0/1 for In/Out (To save space and for optimization if the db REALLY grows)

and make sure you use indexes properly since this table could become large VERY fast depending on how many employees the company has.

 

Hope this helps.

Link to comment
Share on other sites

I did some modifications, but it's still not doing anything.

 

<?php
ob_start();
session_start();

include 'db.php';

if($_POST["doPunch"]){
   if(!$_POST['emp_id'] || !$_POST['emp_pw']){
      header("location: index.php?er=1");
      exit;
   }
   $_POST['emp_id'] = trim($_POST['emp_id']);

   $md5pass = md5($_POST['emp_pw']);
   $result = confirmUser($_POST['emp_id'], $md5pass);

   if($result == 1){
      header("location: index.php?er=2");
      exit;
   }
   else if($result == 2){
      header("location: index.php?er=3");
      exit;
   }


$cc=mysql("SELECT * from timePunch WHERE empID = '$_POST[emp_id]' ORDER BY `ID` DESC") or die(mysql_error());

if(mysql_num_rows($cc)<1){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES ('$_POST[emp_id]', 'Out');") or die(mysql_error());
}else{

$r=mysql_fetch_assoc($cc);

if($r["activityID"] == "In"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'Out'
);") or die(mysql_error());
}else if($r["activityID"] == "Out"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());
}else{
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());
}


}
}

?>

 

 

Change your query and use a where condition that checks the current date. I have a feeling your query is simply not returning any results. Try executing it in phpmyadmin or from the mysql console.

 

Also, make sure you are echoing out as much as you can. Echo your query statement and try adding:

 

<? echo "<pre>";

print_r($_POST);

echo "</pre>";

?>

 

At the bottom of your page to print out your POST array to make sure it is doing what you want/expect.

Link to comment
Share on other sites

This should work... i tried the first query in phpmyadmin, and it returned 0. because the table is empty.. ok no problems there.. but in my code i have if rows=0, then do this query...but it doesnt work. let me see what i can come up with.

 

<?php
$cc=mysql("SELECT * from timePunch WHERE empID = '$_POST[emp_id]' ORDER BY `ID` DESC") or die

(mysql_error());

if(mysql_num_rows($cc)<1){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES ('$_POST[emp_id]', 'Out');") or die(mysql_error());
}else{

$r=mysql_fetch_assoc($cc);

if($r["activityID"] == "In"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'Out'
);") or die(mysql_error());
}else if($r["activityID"] == "Out"){
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());
}else{
mysql_query("INSERT INTO `timePunch` (`empID`,  `activityID`)
VALUES (
'$_POST[emp_id]', 'In'
);") or die(mysql_error());
}
?>

Link to comment
Share on other sites

Change the first line, I don't believe you are actually executing the query (but I could be wrong):

 

Change it to mysql_query()

 

$cc=mysql_query("SELECT * from timePunch WHERE empID = '$_POST[emp_id]' ORDER BY `ID` DESC") or die (mysql_error());

 

I would add some test data into your table to help in troubleshooting. The rest of your code looks sound, just put some data in to test.

 

Also, I would suggest adding a data condition to your where clause. If your table balloons to thousands of rows you are putting added strain on the database with your rather vague query.

 

If you have never played around with the EXPLAIN syntax, search google for a tutorial and try to make a habit out of using this to make your queries as efficient as possible.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.