简体   繁体   English

使用PHP变量的MYSQL表中的时间戳之间的比较不起作用-PHP MYSQL

[英]Comparison between a timestamp in MYSQL table with a PHP variable NOT Working - PHP MYSQL

I'm trying to filter out repeated values entering into a MySQL table, by comparing the input PHP variable with the timestamp of an entry already present in the table and only if they don't match, the input PHP variable is entered into the table. 我试图通过将输入的PHP变量与表中已经存在的条目的时间戳进行比较来过滤出进入MySQL表的重复值,并且只有当它们不匹配时,才将输入的PHP变量输入到表中。

$user1_date = mysql_real_escape_string($user1_date); // the date variable
$user1_temp1 = mysql_real_escape_string($user1_temp1);
$user1_temp2 = mysql_real_escape_string($user1_temp2);
$user1_temp3 = mysql_real_escape_string($user1_temp3); 
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));  //Typecasting PHP variable into timestamp       

$sql_check = "SELECT * FROM user_details WHERE temp_date ='$user1_date'";
$result_check = mysql_query($sql_check);
$num_rows_check = mysql_num_rows($result_check);

if ($num_rows_check == 0) // To check if there is no entry in the table with the same date and time as input PHP variable
{
$sql_insert = "INSERT INTO data_hour (user_id, temp1, temp_date, temp2, temp3)
VALUES (1,'$user1_temp1', '$user1_date', '$user1_temp2', '$user1_temp3')";
$result_insert = mysql_query($sql_insert);
} 

temp_date is a column in the table of type timestamp. temp_date是时间戳类型表中的一列。 Even when the $user1_date is the same as the temp_date (timestamp) column for one of the entries in the table, it considers it as not equal and is inserting it into the table and hence I'm getting repeated values. 即使$user1_date与表中条目之一的temp_date (timestamp)列相同,它也认为它不相等并将其插入表中,因此我得到了重复的值。 I'm guessing the WHERE temp_date = '$user1_date' is not working properly. 我猜测WHERE temp_date = '$user1_date'无法正常工作。 Some troubleshooting that I have done included 我已经完成的一些故障排除

  1. Changing '$user1_date' to just $user1_date in the WHERE statement WHERE语句$user1_date '$user1_date'更改为仅$user1_date
  2. Changing the WHERE clause as follows WHERE temp_date = (date)'$user1_date' 如下更改WHERE子句WHERE temp_date = (date)'$user1_date'

It will be great if somebody can help me out with this! 如果有人可以帮助我,那就太好了!

A nice easy solution would be giving temp_date a UNIQUE INDEX in your Mysql Table, as that would not allow the same value to be inserted twice. 一个不错的简单解决方案是在mysql表中给temp_date一个UNIQUE INDEX,因为这样一来,相同的值就不能插入两次。 This would also make your operations more efficient, as you wouldn't have to do SELECT * every time you want to insert something. 这也将使您的操作更有效率,因为您不必每次都要插入SELECT *

However, for what you're doing, I think I see your problem; 但是,对于您的工作,我想我看到了您的问题。 there are some quirks in your logic so I'll try to dispel them here. 您的逻辑中有一些怪癖,所以我将在这里消除它们。 (Hopefully?) this will make your program cleaner and you'll be able to pinpoint the error, if not eliminate it altogether. (希望吗?)这将使您的程序更整洁,并且即使不能完全消除错误,也可以查明错误。


Examining this piece of code: 检查这段代码:

// $user1_date doesn't have a value here! //
$user1_date = mysql_real_escape_string($user1_date);
...
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));

Error 1 - You escape the string before ever setting a value. 错误1-您在设置值之前先对字符串进行转义。

What you are doing is that you are using mysql_real_escape_string() before $user1_date is ever defined. 您正在做的是在定义$user1_date之前使用mysql_real_escape_string()

Correction: 更正:

// Getting better, but not done. //
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));
...
$user1_date = mysql_real_escape_string($user1_date);

Error 2 - You do not give the date() function appropriate parameters 错误2-您没有给date()函数适当的参数

The date() function in PHP expects a timestamp, which is just an int . PHP中的date()函数需要一个时间戳,它只是一个int You can easily get the time with time(), so that should rectify your problem 您可以使用time()轻松获得时间,因此应该可以解决您的问题

Correction: 更正:

// You use strtotime($user1_date), but you should use time() //
$user1_date = date("Y-m-d H:i:s", time());
...
$user1_date = mysql_real_escape_string($user1_date);

These are small mistakes, but they can be deadly. 这些都是小错误,但可能致命。 Like I said, you should assign temp_date to a UNIQUE INDEX in your MySQL table, but make sure to correct these errors listed as well. 就像我说的那样,您应该将temp_date分配给MySQL表中的UNIQUE INDEX,但请确保也纠正列出的这些错误。

Let me know how it goes! 让我知道事情的后续!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM