开发者

MySQL database entry duplication

I have a PHP script search script 开发者_StackOverflowthat 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 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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜