• We just launched and are currently in beta. Join us as we build and grow the community.

How to Set up Expiration on MySQL Row in PHP/MySQLi

flareclare100

Anime News Reporter
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
96
Likes
47
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
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.

  1. CREATE

    TABLE

    `user`

    (
  2. `userid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `username`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. `password`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  5. `login_date`

    datetime NOT

    NULL

    ,
  6. `status`

    VARCHAR

    (

    15

    )

    NOT

    NULL

    ,
  7. PRIMARY

    KEY

    (

    `userid`

    )
  8. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6.png

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.

  1. <?php

  2. //MySQLi Procedural
  3. //$conn = mysqli_connect("localhost","root","","expiration");
  4. //if (!$conn) {
  5. // die("Connection failed: " . mysqli_connect_error());
  6. //}

  7. //MySQLi Object-oriented
  8. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "expiration"

    )

    ;
  9. if

    (

    $conn

    ->

    connect_error

    )

    {
  10. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  11. }

  12. ?>

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".

  1. <DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Set up Expiration on MySQL Row in PHP</title>
  5. </head>
  6. <body>
  7. <h2>Sample Login Table</h2>
  8. <table border="1">
  9. <thead>
  10. <th>UserID</th>
  11. <th>Username</th>
  12. <th>Password</th>
  13. <th>Login Date</th>
  14. <th>Expiry</th>
  15. <th>Status</th>
  16. </thead>
  17. <tbody>
  18. <?php
  19. include

    (

    'conn.php'

    )

    ;

  20. //$query=mysqli_query($conn,"select * from `user`");
  21. //while($row=mysqli_fetch_array($query)){
  22. /* ?>
  23. <tr>
  24. <td><?php echo $row['userid']; ?></td>
  25. <td><?php echo $row['username']; ?></td>
  26. <td><?php echo $row['password']; ?></td>
  27. <td><?php echo $row['login_date']; ?></td>
  28. <td>
  29. <?php
  30. //set up your timezone using date_default_timezone_set
  31. $today=date('Y-m-d H:i:s');
  32. //we set up our row to expire 3 days after login_date
  33. $expire=date('Y-m-d H:i:s', strtotime($row['login_date']. '+3 days'));

  34. if ($today>=$expire){
  35. //if you wanted to delete row if expired you can do so by substituting the code in the comment below
  36. //mysqli_query($conn,"delete `user` where userid='".$row['userid']."'");

  37. mysqli_query($conn,"update `user` set status='Expire' where userid='".$row['userid']."'");
  38. echo $expire;
  39. }
  40. else{
  41. echo $expire;
  42. }
  43. ?>
  44. </td>
  45. <td><?php echo $row['status']; ?></td>
  46. </tr>

  47. <?php */
  48. //}

  49. $query

    =

    $conn

    ->

    query

    (

    "select * from `user`"

    )

    ;
  50. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  51. ?>
  52. <tr>
  53. <td><?php

    echo

    $row

    [

    'userid'

    ]

    ;

    ?>

    </td>
  54. <td><?php

    echo

    $row

    [

    'username'

    ]

    ;

    ?>

    </td>
  55. <td><?php

    echo

    $row

    [

    'password'

    ]

    ;

    ?>

    </td>
  56. <td><?php

    echo

    $row

    [

    'login_date'

    ]

    ;

    ?>

    </td>
  57. <td>
  58. <?php
  59. //set up your timezone using date_default_timezone_set
  60. $today

    =

    date

    (

    'Y-m-d H:i:s'

    )

    ;
  61. //we set up our row to expire 3 days after login_date
  62. $expire

    =

    date

    (

    'Y-m-d H:i:s'

    ,

    strtotime

    (

    $row

    [

    'login_date'

    ]

    .

    '+3 days'

    )

    )

    ;

  63. if

    (

    $today

    >=

    $expire

    )

    {

  64. //if you wanted to delete row if expired you can do so by substituting the code in the comment below
  65. //$conn->query("delete `user` where userid='".$row['userid']."'");

  66. $conn

    ->

    query

    (

    "update `user` set status='Expire' where userid='"

    .

    $row

    [

    'userid'

    ]

    .

    "'"

    )

    ;
  67. echo

    $expire

    ;
  68. }
  69. else

    {
  70. echo

    $expire

    ;
  71. }
  72. ?>
  73. </td>
  74. <td><?php

    echo

    $row

    [

    'status'

    ]

    ;

    ?>

    </td>
  75. </tr>

  76. <?php
  77. }

  78. ?>
  79. </tbody>
  80. </table>
  81. </body>
  82. </html>


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top