In my page user can register with email and subscribe for news. I'm putting email, IP and timestamp into my database. I want to make sure that one IP can only get 5 emails per day and I don't want the same email addresses in my database.
I tried this but it doesn't work, I can post as many as I want.
if(isset($_POST["send"]))
{
$email = test_input($_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 count(email) as c FROM (
SELECT email FROM subscriber WHERE ip="'.$_SERVER['REMOTE_ADDR'].'" and DATE(`data`) = CURDATE() group by email) as a having c>5))';
$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));
$msg="E-mail sent successfully";
} else
{
$msg="Wrong e-mail address";
}
And not to write same emails i should use something like this after insert?
FROM dual WHERE NOT EXISTS ( SELECT * FROM subscriber WHERE email="'.$email.'" )';