1

I have a basic knowledge of PHP and I am trying to make a check in and out system by adding a check in time and check out time to a database.

Check in:

session_start();
$_SESSION ['inchecken'] = true;
$tijd = date("H:i:s");
$query = "INSERT INTO tijden(tijdin) VALUES('$tijd')";
$resultaat = mysql_query($query);    

Check out:

$tijd = date("H:i:s");
$query = "INSERT INTO tijden(tijduit) VALUES('$tijd')";
$resultaat = mysql_query($query);
unset($session['inchecken']);

The problem is that the check in time and check out time are both saved to a new id in my database (auto increment). Is there someone who can tell me how to add both the check in and check out time in the same database id? Thanks in advance!

  • Using an `UPDATE` statement and providing it the ID. – Jonnix Jun 11 '15 at 20:05
  • just run an [**update**](https://dev.mysql.com/doc/refman/5.0/en/update.html) using a `where` clause on check out. Unless you *really* want to use INSERT, that's another ballgame. As in "relational tables". – Funk Forty Niner Jun 11 '15 at 20:07
  • [`INSERT ... ON DUPLICATE KEY UPDATE`](https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) is another option. – Funk Forty Niner Jun 11 '15 at 20:11
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 11 '15 at 20:23
  • [Your script is at risk for SQL Injection.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jun 11 '15 at 20:23
  • Thanks guys, but it is just for practice so I do not have to worry about sql injections. And I have pre-defined everything. – stacksjouwer Jun 11 '15 at 21:26

3 Answers3

2

You're inserting two rows. That's the reason why you're going to get two entries in your table. You need to use the UPDATE construct for the second piece of code.

You need a way to know the id of the checkin time in order to update it.

$tijd = date("H:i:s");
$query = "UPDATE tijden SET tijduit = '".$tijd."' WHERE id =".$id;
$resultaat = mysql_query($query);
unset($session['inchecken']);

Also, your session variable isn't getting unset.

change your unset($session['inchecken']); statement to unset($_SESSION['inchecken']);

  • 1
    be better to fix the code and mention that the session variable unset isn't working rather than just mention so as to have only 'good' in the answer. (just need to replace the $session with $_SESSION) – Nick Dickinson-Wilde Jun 11 '15 at 20:14
  • *"Also, your session variable isn't getting unset."* - You need to outline "how" they should fix that, and in your answer, and not in comments. Some tend to do that. – Funk Forty Niner Jun 11 '15 at 20:18
1
UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE id = yourId;
0

replace

$query = "INSERT INTO tijden(tijdin) VALUES('$tijd')";

with

$userid = 1 // get user ID
$query = "UPDATE tijden SET tijdin = '$tijd' WHERE id = $userid";
Positivity
  • 5,406
  • 6
  • 41
  • 61