php(比较查询中的两个日期时间)

时间:2016-10-18 13:12:11

标签: php mysql

用户可以写他的电子邮件并订阅新闻,但我想阻止一个IP地址每天发送超过5个电子邮件地址。我在数据库中插入日期,IP和电子邮件,但我不知道如何在查询中比较两个日期时间。有人可以帮我写一个查询吗?

if(isset($_POST["send"])){

    $email = $_POST['email'];
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        $query = 'INSERT INTO subscriber(data, ip, email)
                  SELECT CURRENT_TIMESTAMP, "'.$_SERVER['REMOTE_ADDR'].'", "'.$email.'" 
                  FROM dual
                  WHERE NOT EXISTS (
                     SELECT * FROM subscriber
                     WHERE email="'.$email.'"
                  )';
        $result =  mysqli_query($dbc, $query) or die(mysqli_error($dbc));
        $msg="Email sent successfully";
    }
    else {
        $msg="Error";
    }
}

1 个答案:

答案 0 :(得分:0)

我想我很想念。可能是我错了,但我认为你需要这个

    INSERT INTO iwaki_alphabet (data, ip, email) (SELECT CURRENT_TIMESTAMP, "'.$_SERVER['REMOTE_ADDR'].'", "'.$email.'" FROM dual where NOT EXISTS ( Select count(email) as c  FROM (
SELECT email FROM iwaki_alphabet  WHERE ip="'.$_SERVER['REMOTE_ADDR'].'" and DATE(`data`) = CURDATE() group by email) as a having c>5))

只需检查并确认您需要上述内容或其他内容。