I have a PHP script search script that logs every query made in a MySQL database starting with 1 in the value column. Currently, if the same terms are searched more than once, 1 is added to the number in the value column. However, after two searches for the same word, the query gets added to the database again. Why could this be?
My PHP code is:
<?php
$database=mysql_connect("localhost","username","password");
mysql_select_db("database",$database);
$query=$_GET['q'];
logQuery($query);
function logQuery($query){
$query="insert into queries (query) values ('$query') on duplicate key update value=value+1";
mysql_query($query);
}
?>
Sounds like either
queryis not your primary key, or that $query is different from the DB contents. My money is on the key; could it be a composite key? i.e.idANDquery?In this situation/example, using
mysql_escape_string(trim($query))is a MUST to avoid SQL injection.