简体   繁体   English

更新表并在用户注销时捕获注销时间

[英]Update table and capture logout time on user logout

I was tasked to make a simple volunteer checkin/out page. 我的任务是制作一个简单的志愿者检入/检出页面。 Volunteer picks name from dropdown clicks submit and info is submitted to mysql db, the process is the same for checkout, you pick your name from dropdown and submit and it should update table with logout time. 志愿者从下拉列表中选择名称,然后单击提交,然后将信息提交到mysql db,结帐的过程是相同的,您从下拉列表中选择名称,然后提交,它应该以注销时间更新表。 I am having issues with the query to do this, any help or direction will be greatly appreciated. 我在执行此查询时遇到问题,将不胜感激任何帮助或指导。 Thank You. 谢谢。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>

<body>

<form method="post" action="">

    <label >Please Select Username</label>

    <select name="userid">

    <?php
    require('db.php'); 

    $sql = mysqli_query($con, "SELECT * 
                               FROM users 
                               INNER JOIN check_in_out ON users.id = check_in_out.user_id ");
    while ($row = $sql->fetch_assoc()){
        echo "<option value=". $row['id'] .">" . 
                  $row['last_name'] . ","  . $row['first_name'] . 
             "</option>";
    }
    ?>

    </select>

    <button type="submit" name="submit" >Checkout</button>

    <?php
    require('db.php'); 
    if(isset($_POST["submit"]))
    {

        $user_id = $_POST['userid'];
        $check_out_date = date("Y-m-d H:i:s");
        $update_checkout=mysqli_query("UPDATE check_in_out 
                                       SET check_out_date= $check_out_date, 
                                           user_id=$user_id "); 
        session_destroy();
        header("Location: login.php");
    }
    ?>
</form> 
</body>
</html>

You can try this to update : 您可以尝试此更新:

$user_id = $_POST['userid']; 
$update_checkout=mysqli_query("UPDATE check_in_out 
                               SET check_out_date= NOW() 
                               WHERE user_id=$user_id"); 

1/ I remove the date() part and use mysql NOW() : see documentation 1 /我删除了date()部分并使用mysql NOW() :请参阅文档

2/ You need to use WHERE user_id=$user_id so you know which row you will update 2 /您需要使用WHERE user_id=$user_id以便知道要更新的行

3/ You should use some prepare statement instead of adding direct $_POST in your code : check the documentation and see why here 3 /您应该使用一些prepare语句,而不要在代码中直接添加$_POST :查看文档,在此处查看原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM