I have this PHP contact form which disallows a submission from an existing IP. I am trying to add the feature to submit one entry per IP, per day.
I have a column in my db table called Date which is datestamping every row. I know I need to create my if statement with 2 conditions… I am just not sure how to check the DB before submitting a new entry. Any help is appreciated. Thanks.
CODE SNIPPET:
$ip = gethostbyname($_SERVER['REMOTE_ADDR']);
mysql_select_db($database, $connection);
$QUERY = "SELECT COUNT(IP) AS `count` FROM `contest` WHERE IP = '$ip'";
$RESULT = mysql_query($QUERY) or die(mysql_error());
// Read the firs row
$row = mysql_fetch_assoc($RESULT);
// Check how many rows MySQL counted
if($row['count'] > 0) {
header ('Location: valueexists.html');
}
else {
//save the data on the DB
mysql_select_db($database, $connection);
$insert_query = sprintf("INSERT INTO contest (Name, Email_Address, Phone, Company_Name, Company_Address, Date, ip) VALUES (%s, %s, %s, %s, %s, NOW(), %s)",
Your structure is correct, to make a query with two conditions, simply add a AND to it:
This will count all rows in the contest table with that IP and which have been submitted on that specific date. Note that I took “date” as your fieldname as I’m unaware of your table structure, so adapt it to the one you created.
Edit
I just noticed you are using
NOW()to store your datetime in your database, so I changed my query to make use of theDATE()function. This extracts the date part of the date or datetime expression of the value as can be seen here.Hope this helps!