0

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);
}

?>
2
  • Can you post your table schema? Commented Jun 20, 2011 at 23:03
  • What is the PRIMARY_KEY you base the duplicate key query on? Commented Jun 20, 2011 at 23:04

3 Answers 3

3

Sounds like either query is 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. id AND query?

In this situation/example, using mysql_escape_string(trim($query)) is a MUST to avoid SQL injection.

Sign up to request clarification or add additional context in comments.

Comments

0

If the query is getting added a second time, then it is obviously not a primary key, which prevents the 'on duplicate key' from working

Comments

0

Do you define query field as unique? maybe a whitespace or something, try using md5 as unique key

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.