tags:

views:

67

answers:

2

hello, 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 ?

A: 

Is field searchquery UNIQUE or PRIMARY_KEY?

freeatnet
dont know, this was the query: CREATE TABLE `query` (`id` int(11) NOT NULL auto_increment,`searchquery` varchar(255) NOT NULL default '',`datetime` int(11) NOT NULL default '0',PRIMARY KEY (`id`)) ENGINE=MyISAM
elmaso
A: 

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?

freeatnet
no dont need it, but should I delete the double entries before running this query?
elmaso
Yes, you certainly must do so.
freeatnet
thank you, great!!
elmaso