Comparison of PHP and MYSQL dates

Asked

Viewed 65 times

0

I’m having problems with a code, as I’m new in the area I’m developing just to learn. What I want to do: Perform a monthly access control of the user, that is, if it is not paid the monthly fee it does not access. My problem is to check if the user’s expiration date is less than or equal to CURRENT DATE. If the expiration date is equal to the current date, the value of the "paid" column will be "0", i.e., it did not pay. Otherwise, "1". I’m doing it this way:

function isExpired()
{
    global $conn;
    global $empresa;
    $sql2 = mysqli_query($conn, "SELECT * FROM clientes WHERE nome_fantasia = '$empresa' AND DATE(expire_date) = DATE(NOW())");
    if (mysqli_fetch_row($sql2)==1) {
        //expirou
        $confere = "UPDATE FROM clientes SET `pago` = 0 WHERE nome_fantasia = '$empresa'";
        $editar = mysqli_query($conn, $confere); //Realiza a consulta
        if ($editar == '') {
            echo 'nao deu';
        } else {
            echo 'alterou';
        }
    }
}
isExpired();

I do not know if it was clear what I want, I apologize. Anything I try to explain more. PS: to learn how to play with functions, ANY HINT or CRITICISM is SUPER WELCOME. The code does not return me nor the

if ($editar == '') {
            echo 'nao deu';
        } else {
            echo 'alterou';
        }

obg.

2 answers

1


In your UPDATE, you don’t need FROM, you can do it this way:

  $confere = "UPDATE clientes SET `pago` = 0 WHERE nome_fantasia = '$empresa'";

The return of mysqli_query can be false in case of error, or true in case of success of update. Here are more explanations:

https://www.php.net/manual/en/mysqli.query.php

-1

First thing, your query, dates do not have to be compared only if they are equal but tb smaller, so you have to replace = with <=
controls whether or not data is changed in the Database And to check if the query was executed try with that code


if ($editar){
            echo 'nao deu';
        } else {
            echo 'alterou';
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.