Deanznet Posted September 10, 2008 Share Posted September 10, 2008 Hey i have a search form on my site that searches a table in Mysql. i created another tabled called query_log with 2 columns query and results. How can i make it so when someone searches it inserts their keyword into query and then inserts how many times it was search for into results. so if someone searches php it inserts php then 1 into the query and results. if someone else searches php it inserts 2 into results. Anyone? Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/ Share on other sites More sharing options...
BlueSkyIS Posted September 10, 2008 Share Posted September 10, 2008 i would first see if the search term has been searched for before. if so, get the record id and update that record setting count = count + 1. if not, insert the search term with a count of 1. Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638272 Share on other sites More sharing options...
Mchl Posted September 10, 2008 Share Posted September 10, 2008 INSERT INTO query_log (query, results) VALUES ('$query',1) ON DUPLICATE KEY UPDATE SET results = results + 1 That's assuming there's a UNIQUE index on query field. Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638299 Share on other sites More sharing options...
BlueSkyIS Posted September 10, 2008 Share Posted September 10, 2008 sweet! i didn't know that "ON DUPLICATE KEY UPDATE SET" existed! thanks for that! Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638313 Share on other sites More sharing options...
Deanznet Posted September 10, 2008 Author Share Posted September 10, 2008 Thanks! But im still having a problem. This works. $querylog = "INSERT INTO query_log (query,results) VALUES ('$search',1)"; mysql_query($querylog) or die('Error, insert query failed'); But if i do $querylog = "INSERT INTO query_log (query,results) VALUES ('$search',1) ON DUPLICATE KEY UPDATE SET results = results + 1)"; mysql_query($querylog) or die('Error, insert query failed'); I made my query column primary index unquie was that right? It wont work, i get the insert query failed message. Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638602 Share on other sites More sharing options...
Deanznet Posted September 11, 2008 Author Share Posted September 11, 2008 Bump. Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638778 Share on other sites More sharing options...
Mchl Posted September 11, 2008 Share Posted September 11, 2008 $querylog = "INSERT INTO query_log (query,results) VALUES ('$search',1) ON DUPLICATE KEY UPDATE SET results = results + 1)"; mysql_query($querylog) or die('Error, insert query failed: '.mysql_error()); Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638920 Share on other sites More sharing options...
Deanznet Posted September 11, 2008 Author Share Posted September 11, 2008 Thanks i got it now! Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638936 Share on other sites More sharing options...
Mchl Posted September 11, 2008 Share Posted September 11, 2008 What was it? Link to comment https://forums.phpfreaks.com/topic/123595-solved-loging-search-terms-in-mysql/#findComment-638988 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.