public function update()
 {
     if (empty($_POST)) {
         alert('请选择投票项!', 1);
     }
     inject_check($_POST['id']);
     if (Cookie::is_set('vote' . $_POST['id'])) {
         alert('您已投过票了!', 1);
     }
     //读取数据库
     $vote = M('vote');
     //if(C('TOKEN_ON') && !$vote->autoCheckToken($_POST)){$this->error(L('_TOKEN_ERROR_'));}//防止乱提交表单
     $vo = $vote->where('id=' . intval($_POST['id']))->field('vote,overtime,starttime,stype')->find();
     $strs = explode(PHP_EOL, trim($vo['vote']));
     //业务处理
     if (!$vo) {
         alert('投票不存在!', 3);
     }
     if ($vo['overtime'] != '' && cptime(date('Y-m-d H:i:s'), $vo['overtime'])) {
         alert('投票已结束!', U('votes/' . $_POST['id']));
     }
     if (!cptime(date('Y-m-d H:i:s'), $vo['starttime'])) {
         alert('投票没有开始!', U('votes/' . $_POST['id']));
     }
     $data['vote'] = $vo['vote'];
     if ($vo['stype'] == 0) {
         $_POST['vote'] = array($_POST['vote']);
     }
     foreach ($_POST['vote'] as $v) {
         $v = str_replace(PHP_EOL, "", $v);
         if (in_array($v, $strs)) {
             $s = explode("=", $v);
             if (count($s) == 2 && is_numeric($s[1])) {
                 $data['vote'] = str_replace($v, $s[0] . "=" . (intval($s[1]) + 1), $data['vote']);
             }
         }
     }
     if ($vote->where('id=' . intval($_POST['id']))->save($data)) {
         Cookie::set('vote' . $_POST['id'], '1', 365 * 60 * 60 * 24);
         alert('投票成功!', U('votes/' . $_POST['id']));
     }
     alert("操作失败!", U('votes/' . $_POST['id']));
 }
示例#2
0
 public function update()
 {
     if (!isset($_GET['id'])) {
         alert('非法操作!', 3);
     }
     if (empty($_POST)) {
         alert('请选择投票项!', 1);
     }
     //读取数据库
     $vote = M('vote');
     $vo = $vote->where('id=' . $_GET['id'])->field('vote,overtime,starttime')->find();
     //业务处理
     if (!$vo) {
         alert('投票不存在!', 3);
     }
     if (cptime(date('Y-m-d H:i:s'), $vo['overtime'])) {
         alert('投票已结束!', U('votes/' . $_GET['id']));
     }
     if (!cptime(date('Y-m-d H:i:s'), $vo['starttime'])) {
         alert('投票没有开始!', U('votes/' . $_GET['id']));
     }
     if (Cookie::is_set('wkvote' . $_GET['id'])) {
         alert('您已投过票了!', 1);
     }
     $data['vote'] = $vo['vote'];
     foreach ($_POST as $k => $v) {
         $pattern = "/{$k}=[0-9]{1,}/";
         $v += 1;
         $replacement = $k . '=' . $v;
         $data['vote'] = preg_replace($pattern, $replacement, $data['vote']);
     }
     if ($vote->where('id=' . $_GET['id'])->save($data)) {
         Cookie::set('wkvote' . $_GET['id'], '1', 365 * 60 * 60 * 24);
         alert('投票成功!', U('votes/' . $_GET['id']));
     }
 }