Php 如果条件为真,但不是每行都为真,如何插入或更新?
当我在搜索框中搜索一个单词时,如果该单词不在我的数据库中,我想添加他以提供建议,如果该单词已经在数据库中,我只想更新该单词的搜索次数,以便能够在最流行的单词中排名靠前 就像谷歌。搜索框可以工作,但我不知道如何只插入一次单词,而不是每次提取行时Php 如果条件为真,但不是每行都为真,如何插入或更新?,php,mysql,Php,Mysql,当我在搜索框中搜索一个单词时,如果该单词不在我的数据库中,我想添加他以提供建议,如果该单词已经在数据库中,我只想更新该单词的搜索次数,以便能够在最流行的单词中排名靠前 就像谷歌。搜索框可以工作,但我不知道如何只插入一次单词,而不是每次提取行时 $cuvant=$_GET['cuvant']; //the word that I introduce in the searching box $sql="SELECT * from cautare";// cautare is the table w
$cuvant=$_GET['cuvant']; //the word that I introduce in the searching box
$sql="SELECT * from cautare";// cautare is the table where I keep the 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 the word is not in the database i want to insert him
$nr=1;
$sql2="INSERT INTO cautare values('".$cuvant."',".$nr.")";
$resursa2=mysql_query($sql2);
}else if($row['cuvant']==$cuvant && $cuvant!=''){ //if the word is in the database i just want to uptdate the number_of_searches field
$nr=$nr+1;
$sql3="UPDATE cautare set nr_cautari=".$nr."where cuvant='".$cuvant."'";//update the number_of_searches field
$resursa3=mysql_query($sql3);
}
}
在单词字段中添加一个
唯一的
键,然后:
INSERT INTO table (...)
VALUES (...)
ON DUPLICATE KEY UPDATE field=field+1
在单词字段中添加一个
唯一的
键,然后:
INSERT INTO table (...)
VALUES (...)
ON DUPLICATE KEY UPDATE field=field+1