I want to save a query only one time in the database, this is my code:
$querystat = mysql_real_escape_string($_GET['q']);
$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 UPDATEyou are checking if there was a duplicate on key 1,id. What you are trying to have is unique values for fieldsearchquery. You will need to create a UNIQUE index on fieldsearchquery. To do that, runALTER TABLE query ADD UNIQUE ( searchquery ).However, what
ON DUPLICATE KEY UPDATEpart 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 doON DUPLICATE KEY UPDATE datetime='$datetime'Also, are you sure you need field
idat all?