0

I've been working on a website that has a membership system, users can buy plans and then it is added to their account. I'm having a problem with the de-activation of the members once it has expired.

$time_current = time();
$db_mem_check = mysqli_query($con, "SELECT * FROM members");
while ($row = mysqli_fetch_array($db_mem_check, MYSQLI_ASSOC)) {
    $id = $row['id'];
    $time_exp = (int)$row['mem_expiration_time'];
    if ($time_exp = 0) {

    } else {
        $db_update = mysqli_query($con, "UPDATE members SET title='No Membership',mem_rank='none',mem_expiration='unknown',mem_expiration_time='0' WHERE mem_expiration_time < '$time_current' and id = '$id'");
        if(!$db_update) {
            $danger[] = "MySqli Error: ".mysqli_error($con);
        }
    }

}

So the problem is that whenever a user refreshed the page, it changes everyone's title and mem_rank to No Membership and none, but i want it to ignore changing the database if it is 0.

Sorry if I worded this awkwardly, it is kinda hard to explain. Thanks in advance everyone!

FearGannicus
  • 205
  • 5
  • 17
  • 1
    `if ($time_exp = 0) {` this *assigns* a value, you probably want `==`, which compares? As it is now, that statement will **always** return true, because it successfully sets the variable `$time_exp` to the value 0. So the `else`-block will *never* be executed. – Qirel Jan 24 '16 at 02:07
  • Ohh god, how could I be soo stupid. Im pretty sure you are correct let me go and check – FearGannicus Jan 24 '16 at 02:08
  • 1
    The minor mistakes are often the easiest to make, @Fearless ;-) – Qirel Jan 24 '16 at 02:08
  • 1
    @Qirel - Im pretty sure that was the error, could you make an answer so I can set it as the answer. booyyy i feel stupid – FearGannicus Jan 24 '16 at 02:10

2 Answers2

1

You don't really need two queries for this and you're using = which assigns a value instead of == which checks a value. Try this instead of your current code:

mysqli_query($con, "UPDATE members SET title='No Membership',mem_rank='none',mem_expiration='unknown',mem_expiration_time='0' WHERE mem_expiration_time < UNIX_TIMESTAMP() AND mem_expiration_time != 0");

Far less resource usage too. One line instead of 15.

Matt
  • 2,851
  • 1
  • 13
  • 27
1

As discussed in the comments, the solution to your issue is the fact that you are assigning, instead of comparing

The line

if ($time_exp = 0) {

assigns a value instead of comparing it, and thus it should be

if ($time_exp == 0) {

to compare (the double equality-marks). See Reference - What does this symbol mean in PHP?

In addition, you could simplify your query, as shown in the other answer given by Matt. Usually, if you can do something in SQL, do it in SQL. Makes for cleaner code.

Community
  • 1
  • 1
Qirel
  • 25,449
  • 7
  • 45
  • 62