我有这个小脚本来更新报告计数器
/*********************************
* Report an ad as inappropriate
* This happens when a user click
* the "Report ad" link on the ad
* view page.
*
* The ad can then be reviewed
* and disabled.
*
* @param int | The ad id
*********************************/
function report_ad($aid) {
$row = $this->db->dbh->query('UPDATE '. $this->config->db_prefix .'_adverts SET been_reported = 1, num_reports = num_reports + 1 WHERE aid = '.$aid.'');
$row->execute();
}
和这个 jQuery 来处理链接点击
$("#report-ad").click(function(){
var conf = confirm("Do you want to report this ad as inappropriate?");
var aid = {$smarty.get.aid}
if(conf == true) {
$.ajax({
url: 'reportad.php',
type: 'post',
data: {literal}{aid: aid}{/literal},
success: function(data) {
alert("The ad has been reported as inappropriate");
},
error: function(data) {
alert("An error occured");
}
});
}
return false;
});
reportad.php 仅包含以下内容:
$adverts = new Adverts();
$adverts->report_ad($_POST["aid"]);
由于某种原因,它用 2 更新 num_reports,所以如果它是 1,它将变为 3,然后是 5,依此类推。我看不出哪里有问题。