您好我想在数据库中出现两次或更多时更新值。我有这个代码,但它没有用。
告诉我如何添加记录。
global $wpdb;
$prefix = $wpdb->prefix.searchmeter ;
$search_terms = $_GET['s'];
$query = "INSERT INTO `$prefix` (`terms`,`date`,`count`)
VALUES ('$search_terms',CURDATE(),1)
ON DUPLICATE KEY UPDATE `count` = `count`+1 ";
$success = $wpdb->query($query);
这是我如何显示它
<?php
$results = $wpdb->get_results(
"SELECT `terms`, `count`
FROM `{$wpdb->prefix}searchmeter`
WHERE DATE_SUB( CURDATE( ) , INTERVAL 30 DAY ) <= `date` AND count > 15
GROUP BY `terms`
ORDER BY count DESC, `terms` ASC");
echo "<ul class='wp-tag-cloud'>";
foreach ($results as $result) {
echo '<li><a href="?s='.$result->terms.'">'. $result->terms .'</a></li>'."\n";
}
echo "</ul>";
?>
我有这个数据
terms | data | count
list 15.03 1
当我有重复的密钥计数应该是2但是就像这样
terms | data | count
list 15.03 3
答案 0 :(得分:0)
试试
global $wpdb;
$prefix = $wpdb->prefix.searchmeter ;
$search_terms = $_GET['s'];
$query = "INSERT INTO `$prefix` (`terms`,`date`,`count`)
VALUES ('$search_terms',CURDATE(),1)
ON DUPLICATE KEY UPDATE `count` = `count`+1 ";
$success = $wpdb->query($query);