当前位置: 动力学知识库 > 问答 > 编程问答 >

database - PHP check in and out system

问题描述:

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!

网友答案:

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']);

网友答案:
UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE id = yourId;
网友答案:

replace

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

with

$userid = 1 // get user ID
$query = "UPDATE tijden SET tijdin = '$tijd' WHERE id = $userid";
分享给朋友:
您可能感兴趣的文章:
随机阅读: