PHP 与mySQL:查询表以查看日期是否+ IP组合已存在
我有这个 PHP 联系表单,它不允许从现有 IP 提交。我正在尝试添加每天为每个 IP 提交一个条目的功能。
我的数据库表中有一个名为“日期”的列,它对每一行进行日期标记。我知道我需要创建具有 2 个条件的 if 语句...我只是不确定如何在提交新条目之前检查数据库。任何帮助表示赞赏。谢谢。
代码片段:
$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)",
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)",
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的结构是正确的,要使用两个条件进行查询,只需添加一个 AND 即可:
这将计算 contest 表中具有该 IP 且已被查询的所有行。于该特定日期提交。请注意,我将“日期”作为您的字段名,因为我不知道您的表结构,因此请根据您创建的结构进行调整。
编辑
我刚刚注意到您正在使用
NOW()
将日期时间存储在数据库中,因此我更改了查询以使用DATE()
函数。这会提取值的日期或日期时间表达式的日期部分,如下所示 此处。希望这有帮助!
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!
您将想要执行类似以下操作:
简单地说,您只是查询数据库以查看 IP 是否存在,请注意,这可能应该包含在
if(isset($_POST['add_user'] 中) )) { }
检测表单是否确实是通过此表单 ID 单独提交的。You are going to want to do something similar to:
Simply put, you are just querying the database to see if an IP exists, mind you this should probably be wrapped in a
if(isset($_POST['add_user'])) { }
to detect if the form was indeed submitted by this form ID alone.您可以使用 strtotime('midnight') 获取今天开始的时间戳。当您将其包含在查询中时,您可以确保 mysql 只返回今天的条目:
you can use strtotime('midnight') to get the timestamp of the start of today. When you include this in the query you make sure that mysql only returns entries from today: