php - mysql/php 增加数量或插入不工作

标签 php mysql datatable count

我有这个代码:

$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|
+--------------------------+

$qone 时,数字增加 1 但是当 $q 其他词没有任何反应。我希望当 $q 不在关键字列表中时,进行注册,如果 $q 在关键字列中,则数字增加 1。“如果单词存在” 有效,但“如果不有效” 则无效.... 请帮忙!谢谢!

最佳答案

假设 keyword 是表的主键,您可以在一个查询中完成:

INSERT INTO words (keyword, number, date)
VALUES ('$q', 1, '$d')
ON DUPLICATE KEY UPDATE number = number + 1

关于php - mysql/php 增加数量或插入不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18451898/

相关文章:

php - 在 codeigniter 中使用 javascript 进行表单验证

php - 什么是 PHP 中的魔术引号运行时?

php - mysql内置函数去除标签

php - 如何防止重复插入表

c# - ExpandoObject 到 DataTable

javascript - 如何使用服务器端处理对数据表进行搜索?

c# - 在 DataTable 中分组 Column sum

php if 语句总是返回 false

php - mysql版本更新中Moodle配置错误

php - 在 RegEx 中,如何找到包含不超过 3 个唯一字符的行?