[英]mysql/php increase number or insert not working
我有這個代碼:
$q = $_GET['q'];
$results= mysql_query("SELECT number FROM words WHERE keyword='$q'") or
die(mysql_error());;
if($results){
mysql_query("UPDATE words SET number = number + 1 WHERE keyword='$q'") or die(mysql_error());
}
else{
mysql_query("INSERT INTO words VALUES ('$q', '1', '$d')") or die(mysql_error());
}
我有表格的words
:
+--------+--------+-- -----+
|keyword |number |date |
+--------------------------+
|one |1 |01-01-01|
+--------------------------+
當$q
為one
,數字增加1但是當$q
其他單詞沒有任何反應時。 我希望當$q
不在關鍵字列表中時,要注冊,如果$q
在關鍵字列中,則數字會增加1.“如果單詞存在”有效,但“如果不存在”則不然工作......請幫忙! 謝謝!
假設keyword
是表的主鍵,您可以在一個查詢中執行:
INSERT INTO words (keyword, number, date)
VALUES ('$q', 1, '$d')
ON DUPLICATE KEY UPDATE number = number + 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.