views:

75

answers:

1

Hi, what is the best method to insert a search query into MySql

and check for double words? (for showing up the last searches and a collection of searches)

maybe something like this:

< ?php
/*------------------------------
Read and save the search query
-------------------------------*/
$querystat = mysql_real_escape_string($_GET['q']);
$insertquery = "INSERT INTO `query` ( `query`) VALUES ( '$querystat');";
mysql_query($insertquery, $db);
}
?>

but how to check for double words?

A: 

If you don't like a field to contain duplicated entries, you have to define it as UNIQUE.

Then you would issue your connands just the way you suggested:

$querystat = mysql_real_escape_string($_GET['q']);
$insertquery = "INSERT INTO `query` ( `query`) VALUES ( '$querystat');";
$res = mysql_query($insertquery, $db);

if (!$res) echo 'Insert faild. Most likely query already exists';
JochenJung