我已经完成了修复之前写过的投票功能(不是我)的任务,我无法弄清楚它为什么不起作用。写入的函数位于名为users.php的文件中。我在这里向用户意识到按钮的代码实际上是在寻找一个javascript函数。如何编辑下面的代码来调用我的php函数?我真的需要帮助,以便在我试图举办吉他比赛时为我的朋友解决这个问题。
以下是调用该函数的按钮的代码,该函数应该将投票保存到数据库中
<div style="margin:0 3px;background-image: url('<?php echo base_url(); ?>images/vote.png');background-repeat:no-repeat;float:left;">
<a id="vote_<?php echo $photo['id']; ?>" href="hello" onclick='vote("<?php echo $captions; ?>", "<?php echo $photo['video_id']; ?>", <?php echo $photo['id']; ?>, <?php echo $user_id[0]->id; ?>)' class="vote">
<?php echo ($this->users->has_voted_source($photo['id'], $user_id[0]->id,$iframe)) ? "Voted" : "Vote"; ?></a></div>
这是我认为应该将投票保存到数据库中的函数。
public function vote($pic_id, $user_id,$source) {
//$sqlQuery = "SELECT * FROM votes WHERE pic_id = ".$pic_id." AND user_id = ".$user_id;//." AND vote_time > ".time();
$userObject['pic_id'] = $pic_id;
$userObject['user_id'] = $user_id;
$userObject['source'] = $source;
$userObject['vote_time'] = date('Y-m-d H:i:s');
$sqlQuery = "INSERT INTO votes ('pic_id','user_id','vote_time','source') VALUES (".$pic_id.",".$user_id.",".$source.",now())";
$this->db->insert('votes', $userObject);
$newID = $this->db->insert_id();
if ($newID) {
$update = "UPDATE photos set votes = votes+1 WHERE `id` = $pic_id";
$query = $this->db->query($update);
return true;
} else {
return false;
}
}