0

I'm having trouble with getting the time and date on a checkbox change.

I have a MySQL database named "panel" which is filled with entries of users, every entry has a "status" field in the database, which is either "1" or "0". I'm using this status to see who's online and style the checkboxes as buttons, 1 = green, 0 = gray.

Now I want to get the current system time whenever this status changes and write it into a text file.

For example: When the status changes from 0 to 1, write the following text into a document: [Current time] + [Name of the user from MySQL database] + "logged in"

When the status changes from 1 to 0, write: [Current time] + [Name of the user from mySQL database] + "logged out"

Heres my code:

#this is where I change the status field in MySQL

<?php
$id = $_POST["id"];
$update = mysql_query("update panel 
    SET status = CASE
        WHEN status = '1' THEN '0'
        WHEN status = '0' THEN '1'          
    END 
WHERE id = $id")
?>

#this is where I execute the function

<td>
<script type="text/javascript" src="scripte/jquery-2.1.4.min.js"></script> 
<script>
function save_checkbox(id)
{
$.post( 'save_check.php' , { checked : $(this).attr("checked"), id: id });
}
</script>
<div class="switch anws">
<input type="checkbox" name="anw_status" value="1" onchange="save_checkbox(<?php echo "$row->id"; ?>);" <?php if ($row->status==1) echo "checked";?>>
<label class="label"><p><?php echo "$row->gender $row->person";?></p></label>
</div>
</td>
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
Pyt-YL
  • 9
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Aug 21 '17 at 08:01
  • 2
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Aug 21 '17 at 08:01
  • What exactly is the problem with getting the date and the time? I don't see anything related to that. – jeroen Aug 21 '17 at 08:07

1 Answers1

0

I would recomend making a seperate table in your database for this, instead of writing it into a text file.

However, this is how you can write to a txt file: lets assume you have created a text file called log.txt in the same folder as save_check.php then when save_check.php is called you can do this:

first add a sql query to get the new status and the user name and set them to the variables $state and $name then

$my_file = 'log.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file:  '.$my_file);

if ($state == 0 {
  $data = "\n" . date('m/d/Y h:i:s a', time()) . " " . $name . " logged out";
} else {
  $data = "\n" . date('m/d/Y h:i:s a', time()) . " " . $name . " logged in";
}

fwrite($handle, $data);
fclose($handle);
  • \n is to ensure that it is on a new line.
  • date('m/d/Y h:i:s a', time()) will get the current time and format it
  • fwrite will append to the file
BrendanMullins
  • 587
  • 3
  • 18