Reputation:
So here is my problem, I want to add to my database table an expiration date to a specific row trough HTML and PHP but I dont know what is wrong. For some reason it dont delete the specific row when the time runs out.
And is possible to add something to the database for example, on the php introduce the time to expire in a column? I'm just starting with this PHP MySQLi interactions so the code will be probabily bad!
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);
?>
Upvotes: 0
Views: 931
Reputation:
Your current query is:
$sql = "INSERT INTO sm_admins
(identity, name, flags, immunity, '', '', now(),
DATE_ADD(NOW(), INTERVAL time='time' MINUTE))
VALUES ('$identity', '$name', '$flags', '$immunity')";
it needs to be changed so that the database knows what you want. Since I don't have all the fieldnames for you sm_admin table I'll use f1,f2, f3 etc so you'll need to replace to the appropriate fieldname
$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))";
Edit:
Your original sql statement wouldn't work. The above correction will work but you'll need to change f1..f5 to your relevant fieldnames.
As for the automatic deletion of records you could use a cronjob or a mySQL event. Documentation can be found at https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html. Cronjob will depend upon your server. Here is a skeleton mysql event schedule - read documentation for details.
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 ;
Upvotes: 1
Reputation: 8786
...delete the specific row when the time runs out.
You can't do this with MySQL. You'll have to come up with a solution using PHP. In this case cron jobs may come in handy.
How to create cron job using PHP?
A work around would be to add a column with DATETIME
to your table. Set the default value to NOW()
. So when you insert a record to the table inserted date and time will be saved in that column.
Then when you are retrieving data add a condition to the search query to filter information by expiration date and time
... WHERE `dateTime` > '2018-07-22 00:05:48'
Upvotes: 0