php - How to insert or update if a condition is true, but not at every row? -


when search word in searching box, if word not in database want add him giving suggestions, , if word in database want update number of searches of word can make top of popular words.

it's google. search box works don't know how insert word 1 time not every time rows fetched.

$cuvant=$_get['cuvant']; //the word introduce in searching box $sql="select * cautare";// cautare table keep words $resursa=mysql_query($sql); //the resource     while($row = mysql_fetch_array($resursa)) {         $nr=$row['nr_cautari'];//number of serching of each word         if ($row['cuvant']!=$cuvant && $cuvant!=''){//if word not in database want insert him              $nr=1;             $sql2="insert cautare values('".$cuvant."',".$nr.")";             $resursa2=mysql_query($sql2);         }else if($row['cuvant']==$cuvant && $cuvant!=''){ //if word in database want uptdate number_of_searches field             $nr=$nr+1;             $sql3="update cautare set nr_cautari=".$nr."where cuvant='".$cuvant."'";//update number_of_searches field             $resursa3=mysql_query($sql3);             }     }  

add unique key on word field, , then:

insert table (...)   values (...)   on duplicate key update field=field+1 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -