PHP 与mySQL:查询表以查看日期是否+ IP组合已存在

发布于 2024-12-12 02:26:45 字数 810 浏览 0 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-12-19 02:26:45

您的结构是正确的,要使用两个条件进行查询,只需添加一个 AND 即可:

$query = "SELECT COUNT(ip) AS count FROM contest WHERE ip = '$ip' AND DATE(date) = CURDATE()";

这将计算 contest 表中具有该 IP 且已被查询的所有行。于该特定日期提交。请注意,我将“日期”作为您的字段名,因为我不知道您的表结构,因此请根据您创建的结构进行调整。

编辑
我刚刚注意到您正在使用 NOW() 将日期时间存储在数据库中,因此我更改了查询以使用 DATE() 函数。这会提取值的日期或日期时间表达式的日期部分,如下所示 此处

希望这有帮助!

Your structure is correct, to make a query with two conditions, simply add a AND to it:

$query = "SELECT COUNT(ip) AS count FROM contest WHERE ip = '$ip' AND DATE(date) = CURDATE()";

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 the DATE() function. This extracts the date part of the date or datetime expression of the value as can be seen here.

Hope this helps!

临走之时 2024-12-19 02:26:45

您将想要执行类似以下操作:

$count = mysql_num_rows(mysql_query("SELECT * FROM contest WHERE IP ='".$IP."'"));

if($count > 0) {
$error = '<div class="error_message">Sorry, username already taken.</div>';
}

简单地说,您只是查询数据库以查看 IP 是否存在,请注意,这可能应该包含在 if(isset($_POST['add_user'] 中) )) { } 检测表单是否确实是通过此表单 ID 单独提交的。

You are going to want to do something similar to:

$count = mysql_num_rows(mysql_query("SELECT * FROM contest WHERE IP ='".$IP."'"));

if($count > 0) {
$error = '<div class="error_message">Sorry, username already taken.</div>';
}

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.

贩梦商人 2024-12-19 02:26:45

您可以使用 strtotime('midnight') 获取今天开始的时间戳。当您将其包含在查询中时,您可以确保 mysql 只返回今天的条目:

$query = "SELECT COUNT(ip) AS count FROM contest WHERE ip = '$ip' AND DATE > " . strtotime('midnight');

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:

$query = "SELECT COUNT(ip) AS count FROM contest WHERE ip = '$ip' AND DATE > " . strtotime('midnight');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文