将数据插入到mysql trought php中并带有过期日期

vbkedwbf  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(270)

所以这就是我的问题,我想通过html和php向我的数据库表中添加一个特定行的过期日期,但我不知道怎么了。由于某些原因,它不会在时间用完时删除特定的行。
有没有可能向数据库中添加一些内容,例如,在php中,在一列中引入过期时间?我只是从这个php mysqli交互开始,所以代码可能很糟糕!
html格式:

<div class="modal fade" id="addModal" role="dialog">
   <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-header">
            <h4 class="modal-title">VIPS</h4>
         </div>
         <div class="modal-body">
            <div class="container">
               <form class="form-horizontal" action="insert.php" method="post" onsubmit="setTimeout('location.reload()', 10);">
                  <div class="form-group">
                     <label class="control-label col-sm-2" for="full name">SteamID</label>
                     <input class="form-control" type="text" id="identity" name="identity" placeholder="SteamID" ng-model="newUser.fullname"/>
                  </div>
                  <div class="form-group">
                     <label class="control-label col-sm-2" for="full name">Name</label>
                     <input class="form-control" type="text" placeholder="Name" id="Name" name="name" ng-model="newUser.fullname"/>
                  </div>
                  <div class="form-group">
                     <label class="control-label col-sm-2" for="full name">immunity</label>
                     <input class="form-control" type="text" id="immunity" name="immunity" placeholder="Imunidade" ng-model="newUser.fullname"/>
                  </div>
                  <div class="form-group">
                     <label class="control-label col-sm-2" for="full name">Time</label>
                     <select id="time" name="time" data-plugin-selectTwo class="form-control populate">
                        <optgroup label="Staff">
                           <option value="1">1 minute</option>
                           <option value="2">2 minute</option>
                        </optgroup>
                     </select>
                  <div class="form-group">
                     <label class="control-label col-sm-2" for="full name">Flags</label>
                     <select id="flags" name="flags" data-plugin-selectTwo class="form-control populate">
                        <optgroup label="Staff">
                           <option value="zo">Founder</option>
                           <option value="abcdefghjkp">Admin</option>
                           <option value="abcdfgjkq">Mod</option>
                        </optgroup>
                     </select>
                     <div class="modal-footer">
                        <button class="btn btn-primary" type="submit" value="ADICIONAR">Add</button>
                        <button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
                     </div>
                  </div>
               </form>
            </div>

PHP:

<?php

$conn = new mysqli("", "", "", "");

if ($conn->connect_error) {
    die("Connection failed");
} 

// Escape user inputs for security
$identity = mysqli_real_escape_string($conn, $_REQUEST['identity']);
$name = mysqli_real_escape_string($conn, $_REQUEST['name']);
$flags = mysqli_real_escape_string($conn, $_REQUEST['flags']);
$immunity = mysqli_real_escape_string($conn, $_REQUEST['immunity']);
$time=$_POST['time'];

$sql = "SELECT COUNT(*) FROM sm_admins WHERE identity = ('$identity')";
if($count = $conn->query($sql)){

    if($count == 1){
        $sql = "INSERT INTO sm_admins 
                        (identity, name, flags, immunity, '', '', now(), 
                         DATE_ADD(NOW(), INTERVAL time='time' MINUTE)) 
                VALUES ('$identity', '$name', '$flags', '$immunity')";
        if($conn->query($sql)){
            echo "Good Connection";
        }
    }else{
        echo "Identity already exist";
    }
}
// close connection
header('Location: panel.php');
mysqli_close($conn);

?>
vfwfrxfs

vfwfrxfs1#

…在时间用完时删除特定行。
你不能用mysql做这个。您必须使用php提出一个解决方案。在这种情况下,cron jobs可能会派上用场。
如何使用php创建cron作业?
一个解决方法是添加一列 DATETIME 到你的table上去。将默认值设置为 NOW() . 因此,在向表中插入记录时,插入的日期和时间将保存在该列中。
然后在检索数据时,向搜索查询中添加一个条件,以按过期日期和时间筛选信息

... WHERE `dateTime` > '2018-07-22 00:05:48'
rur96b6h

rur96b6h2#

您当前的查询是:

$sql = "INSERT INTO sm_admins 
                (identity, name, flags, immunity, '', '', now(), 
                 DATE_ADD(NOW(), INTERVAL time='time' MINUTE)) 
        VALUES ('$identity', '$name', '$flags', '$immunity')";

它需要更改,以便数据库知道您想要什么。因为我没有sm\u admin表的所有字段名,所以我将使用f1、f2、f3等,所以您需要替换为适当的字段名

$sql = "INSERT INTO sm_admins 
                    (identity, name, flags, immunity, f1, f2, f3, f4, f5 ) 
            VALUES ('$identity', '$name', '$flags', '$immunity', '', '', now(), 
                     DATE_ADD(NOW(), INTERVAL time='time' MINUTE))";

编辑:
原来的sql语句不起作用。上面的更正将起作用,但您需要将f1..f5更改为相关的字段名。
至于自动删除记录,可以使用cronjob或mysql事件。文档可在https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html. cronjob将取决于您的服务器。下面是一个mysql事件调度的框架-详细信息请阅读文档。

DELIMITER $$
-- SET GLOBAL event_scheduler = ON$$     -- required for event to execute but not create    

CREATE  /*[DEFINER = { user | CURRENT_USER }]*/ EVENT `_u3a_work`.`xx`

ON SCHEDULE
     /* uncomment the example below you want to use */

    -- scheduleexample 1: run once

       --  AT 'YYYY-MM-DD HH:MM.SS'/CURRENT_TIMESTAMP { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] }

    -- scheduleexample 2: run at intervals forever after creation

       -- EVERY 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...]

    -- scheduleexample 3: specified start time, end time and interval for execution
       /*EVERY 1  [HOUR|MONTH|WEEK|DAY|MINUTE|...]

       STARTS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1[HOUR|MONTH|WEEK|DAY|MINUTE|...] }

       ENDS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] } */

/*[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']*/

DO
    BEGIN
        (sql_statements)
    END$$

DELIMITER ;

相关问题