doulei2100 2014-08-26 16:05
浏览 45

在Ajax调用时,变量不被视为对象

I have a poll and when user click on one of the options, it sends data through ajax:

$.ajax({
    type: 'POST',
    url: '/poll.php',
    data: {option: option, IDpoll: IDpoll},
    dataType: 'json',
    async: false,
    error: function(xhr, status, error) {
          alert(xhr.responseText);
        },
    success: function(data) {
        if(data.msg == "0") {
            $( "#pollArea" ).load( "/pollVote.php", { allow: true }, function() {
                  alert( "Ďakujeme za Váš hlas!" );
                });
        }
        else {
            alert(data.msg);
            alert("V tejto ankete ste už hlasovali.");}
        }
    });

This works fine. Now data are passed to the file poll.php:

if (isset($_POST['option']) && isset($_POST['IDpoll'])) {
    require 'includes/config.inc.php';
    $ip     = $_SERVER['REMOTE_ADDR'];
    $option = $pdo->quote($_POST['option']);
    $IDpoll = $pdo->quote($_POST['IDpoll']);
    $date   = date("d.m.Y H:i:s");
    $poll   = new Poll();
    $msg    = $poll->votePoll($IDpoll, $ip, $option, $date);
    $arr    = array(
        'msg' => $msg

    );
echo json_encode($arr);

This also works, the problem happened in class Poll - method VotePoll:

public function votePoll($IDpoll, $ip, $option, $date)
    {
        try {
        $query  = "SELECT * FROM `votes` WHERE `IDpoll` = '$IDpoll' AND `ip` = '$ip'";
        $result = $this->pdo->query($query);    
        if ($result->rowCount() == 0) {
           /* do stuff */
      }
      catch (PDOException $e) {
        return $e->getMessage();
      }
    }

And the error message from the ajax call is following: Call to a member function rowCount() on a non-object. I know what this message means, but I can't find out why the variable $result isn't considered as PDO object. Strange thing is, that when I try to call function votePoll manually, it works perfectly and when I use var_dump on result it is PDO object. So where is the mistake?

EDIT: I forgot to say I was just editing this function. Originally it worked with mysqli but I wanted to switch to pdo (so query and stuff like that are okay).

  • 写回答

1条回答 默认 最新

  • dongyirong3564 2014-08-26 16:35
    关注

    So, this problem was in these lines:

       $option = $pdo->quote($_POST['option']);
       $IDpoll = $pdo->quote($_POST['IDpoll']);
    

    PDO quote function add quotes to the string so option became 'option' etc. Then it was sent to query where additional quotes were added, so the result was ''option'' and that is error.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器