hi,
i want to store date time whan i click radio button(name login time)in mysql. if i click logout radio button it should store logout time. ples help me... php and mysql code ...

Dani AI

Generated

The reason both columns end up with the same timestamp is that your script inserts both values at once and also overwrites/reads the wrong POST variables. As suggested, only write the column that matches the chosen action, and make sure the radio buttons share one name so the server can tell which was selected (for example use a single group name like action with values login / logout). , your code also sets $login/$logout to 'unchecked' after reading POST and reads $_POST['log'] while the form uses different names — that prevents detecting the chosen option.

Recommended logic (summary)

  • Use one radio group name (e.g. action) so only one value is posted.
  • On login: create a session row (or update the employee's last_login) with the login timestamp.
  • On logout: update the matching session row (the latest row for that empid where logout_time IS NULL) and set logout_time.
  • Use prepared statements (PDO/mysqli), validate empid, and avoid deprecated mysql_* functions.

Example server-side pattern (PDO, minimal):

$action = $_POST['action'] ?? '';
$empid  = (int)($_POST['empid'] ?? 0);

if ($action === 'login') {
  $pdo->prepare('INSERT INTO sessions (empid, login_time) VALUES (:empid, NOW())')
      ->execute([':empid' => $empid]);
} elseif ($action === 'logout') {
  $pdo->prepare(
    'UPDATE sessions
     SET logout_time = NOW()
     WHERE empid = :empid AND logout_time IS NULL
     ORDER BY login_time DESC
     LIMIT 1'
  )->execute([':empid' => $empid]);
}

Other notes: make logout_time nullable, consider a sessions table instead of forcing two columns on emp, handle the case where logout is clicked with no open session (check affected rows), and ensure consistent timezone handling (store UTC or set server TZ). These changes will ensure login and logout times are recorded correctly and paired reliably.

Recommended Answers

All 3 Replies

You can use NOW() in your MySQL query which will store the current date and time.

thanks for reply. i used NOW() in query but i getting same datetame in login and logout column.
this is radio button code:

<input type="radio" name="login"  >LogIn
<input type="radio" name="logout">LogOut

and

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$data_base="log";
mysql_select_db($data_base, $con);
if($_SERVER['REQUEST_METHOD']=="POST"){
$name = $_POST['name'];
$empid = $_POST['empid'];
$shift = $_POST['shift'];
$login = $_POST['login'];
$logout = $_POST['logout'];

$login = 'unchecked';
$logout = 'unchecked';

if (isset($_POST['Submit'])) {

$selected_radio = $_POST['log'];

if ($selected_radio == 'login') {

$login = 'checked';

}
else if ($selected_radio == 'logout') {

$logout = 'checked';

}}

$sql="INSERT INTO emp (name,empid,shift,login,logout)VALUES ('$name','$empid','$shift',now(),now())";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}

echo "Your Information Was Successfully Posted";
include('thankyou.php');
mysql_close($con);
?>

ple help me.

You should only update the login column when logging in, and the logout column when logging out, not both at the same time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.