flareclare100
Anime News Reporter
LEVEL 1
200 XP
This tutorial will show you how to set up expiration on mysql row in PHP/MySQLi. In this tutorial, I've set up to update the expired row but in case that you wanted to delete the row, I've added the delete code in the comment. So feel free to switch it. This tutorial also has two mysqli methods that I've included in the comments as well so you can switch between them.
Creating our Database
I've created a sample database that we are going to use in this tutorial.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "expiration".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
Creating our Connection
Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
Creating our Sample Table
Lastly, we create our sample table. This table will show the expiration of our row. We name this as "index.php".
Download
Creating our Database
I've created a sample database that we are going to use in this tutorial.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "expiration".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
- CREATE
TABLE
`user`
(
- `userid`
INT
(
11
)
NOT
NULL
AUTO_INCREMENT
,
- `username`
VARCHAR
(
30
)
NOT
NULL
,
- `password`
VARCHAR
(
30
)
NOT
NULL
,
- `login_date`
datetime NOT
NULL
,
- `status`
VARCHAR
(
15
)
NOT
NULL
,
- PRIMARY
KEY
(
`userid`
)
- )
ENGINE=
InnoDB DEFAULT
CHARSET=
latin1;

Creating our Connection
Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
- <?php
- //MySQLi Procedural
- //$conn = mysqli_connect("localhost","root","","expiration");
- //if (!$conn) {
- // die("Connection failed: " . mysqli_connect_error());
- //}
- //MySQLi Object-oriented
- $conn
=
new
mysqli(
"localhost"
,
"root"
,
""
,
"expiration"
)
;
- if
(
$conn
->
connect_error
)
{
- die
(
"Connection failed: "
.
$conn
->
connect_error
)
;
- }
- ?>
Creating our Sample Table
Lastly, we create our sample table. This table will show the expiration of our row. We name this as "index.php".
- <DOCTYPE html>
- <html>
- <head>
- <title>Set up Expiration on MySQL Row in PHP</title>
- </head>
- <body>
- <h2>Sample Login Table</h2>
- <table border="1">
- <thead>
- <th>UserID</th>
- <th>Username</th>
- <th>Password</th>
- <th>Login Date</th>
- <th>Expiry</th>
- <th>Status</th>
- </thead>
- <tbody>
- <?php
- include
(
'conn.php'
)
;
- //$query=mysqli_query($conn,"select * from `user`");
- //while($row=mysqli_fetch_array($query)){
- /* ?>
- <tr>
- <td><?php echo $row['userid']; ?></td>
- <td><?php echo $row['username']; ?></td>
- <td><?php echo $row['password']; ?></td>
- <td><?php echo $row['login_date']; ?></td>
- <td>
- <?php
- //set up your timezone using date_default_timezone_set
- $today=date('Y-m-d H:i:s');
- //we set up our row to expire 3 days after login_date
- $expire=date('Y-m-d H:i:s', strtotime($row['login_date']. '+3 days'));
- if ($today>=$expire){
- //if you wanted to delete row if expired you can do so by substituting the code in the comment below
- //mysqli_query($conn,"delete `user` where userid='".$row['userid']."'");
- mysqli_query($conn,"update `user` set status='Expire' where userid='".$row['userid']."'");
- echo $expire;
- }
- else{
- echo $expire;
- }
- ?>
- </td>
- <td><?php echo $row['status']; ?></td>
- </tr>
- <?php */
- //}
- $query
=
$conn
->
query
(
"select * from `user`"
)
;
- while
(
$row
=
$query
->
fetch_array
(
)
)
{
- ?>
- <tr>
- <td><?php
echo
$row
[
'userid'
]
;
?>
</td>
- <td><?php
echo
$row
[
'username'
]
;
?>
</td>
- <td><?php
echo
$row
[
'password'
]
;
?>
</td>
- <td><?php
echo
$row
[
'login_date'
]
;
?>
</td>
- <td>
- <?php
- //set up your timezone using date_default_timezone_set
- $today
=
date
(
'Y-m-d H:i:s'
)
;
- //we set up our row to expire 3 days after login_date
- $expire
=
date
(
'Y-m-d H:i:s'
,
strtotime
(
$row
[
'login_date'
]
.
'+3 days'
)
)
;
- if
(
$today
>=
$expire
)
{
- //if you wanted to delete row if expired you can do so by substituting the code in the comment below
- //$conn->query("delete `user` where userid='".$row['userid']."'");
- $conn
->
query
(
"update `user` set status='Expire' where userid='"
.
$row
[
'userid'
]
.
"'"
)
;
- echo
$expire
;
- }
- else
{
- echo
$expire
;
- }
- ?>
- </td>
- <td><?php
echo
$row
[
'status'
]
;
?>
</td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
Download
You must upgrade your account or reply in the thread to view hidden text.