将数据插入到期日期为DATE的MySQL trought php中

时间:2018-07-22 01:20:42

标签: php html mysql database mysqli

所以这是我的问题,我想通过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);

?>

2 个答案:

答案 0 :(得分:1)

您当前的查询是:

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

它需要更改,以便数据库知道您想要什么。由于我没有您需要的所有字段名,因此我将使用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 ;

答案 1 :(得分:0)

  

...当时间用完时删除特定行。

MySQL无法做到这一点。您将不得不提出使用PHP的解决方案。在这种情况下,cron工作可能会派上用场。

How to create cron job using PHP?


一种解决方法是在表中添加一个带有DATETIME的列。将默认值设置为NOW()。因此,当您向表中插入记录时,插入的日期和时间将保存在该列中。

然后,当您检索数据时,向搜索查询中添加条件以按到期日期和时间过滤信息

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