I want to save a query only one time in the database, this is my code:
$querystat = mysql_real_escape_string($_GET['q']);
$开发者_JAVA百科datetime = time();
if( ($querystat != $_SESSION['prev_search']) OR ( ($datetime - $_SESSION['datetime']) > 60) ) {
$insertquery = "INSERT INTO `query` ( `searchquery` , `datetime`) VALUES ( '$querystat' , '$datetime') ON DUPLICATE KEY UPDATE searchquery='$querystat';";
mysql_query($insertquery, $db);
}
maybe something with == 0
?
What happens is by ON DUPLICATE KEY UPDATE
you are checking if there was a duplicate on key 1, id
. What you are trying to have is unique values for field searchquery
. You will need to create a UNIQUE index on field searchquery
. To do that, run ALTER TABLE query ADD UNIQUE ( searchquery )
.
However, what ON DUPLICATE KEY UPDATE
part of your query does is probably not what you want. To indicate that this query was repeated at a later time, you probably would want to do ON DUPLICATE KEY UPDATE datetime='$datetime'
Also, are you sure you need field id
at all?
Is field searchquery UNIQUE or PRIMARY_KEY?
精彩评论