数据库拒绝通过AJAX调用进行更新

时间:2013-03-26 12:21:43

标签: php javascript mysql jquery

我在解决编程问题时尽量避免寻求帮助,但是现在我很难过,并且可以从好人那里得到一些帮助。

情景:

我正在开发的网站上有一个页面允许某些用户在Google地图上选择位置作为访问位置所有更新都是通过ajax到mySQL数据库完成的,所有这些都可以正常工作。 当用户完成选择后,按下一个按钮来锁定当前年份的选择过程,这只需通过对名为Selection_Complete的访问者表上的标志位进行AJAX数据库更新即可完成。

问题:

直到几天前,一切似乎都很好但是我注意到当按下完成选择按钮时选择没有被锁定,这很奇怪,因为它已经工作,然后一些环顾四周显示数据库标志已经停止更新

以下是相关代码:

JavaScript关闭功能:

function closeSelection() {

var conf = confirm('Are you sure you want to finish selection ?');

if (conf === true){

    setCompletionStatus(userID,1,function(data){

        if (data === -1) {
            displayMessage ('Unable to close selection: an error occurred in update');
            console.error('setCompletionStatus() returned -1 a database error occoured during query');
            return;
        } else if (data === -2) {
            displayMessage ('Unable to close selection: an error occurred in update');
            console.error('setCompletionStatus() returned -2 a database error occurred during query');
            return;
        } else if (data === 0) {
            displayMessage ('Unable to close selection: an error occurred in update');
            console.error('Close selection returned 0 update enabled, should have been 1 to close');
            return;
        } else if (data === 1) {
            selectionComplete = data; // a global variable holding the current users selection status
            lockSelectBtns();
            displayMessage('Selection now closed for ' + dateInfo.getFullYear());
            $('#placementMessages').html('Selection closed for ' + dateInfo.getFullYear()).css({
                'color':'red' 
            }).show();
            return;
        } else {
              displayMessage ('Unable to lock selection: an error occurred in update');
              console.error('Unknown code: ' + data + ' returned by setCompletionStatus()' );
        } 
    },displayMessage);   
   }
 }

javaScript Ajax处理程序:

    function setCompletionStatus (userID,isComplete,callback,failCallback) {
    if ((! jQuery.type(userID)=== 'String') && (! jQuery.type(userID)=== 'number')) {
        throw new Error ('The userID parameter of function  setNumChoices() (AJAX) was of type '+ jQuery.type(userID) +
            '\n either a String or an Integer was expected.');
    }
    $.ajax({
        data: {
            mode: 'setIsComplete',
            userID: userID,
            isComplete:isComplete
        }
    }).fail(function(jqHXR,textStatus,errorThrown) {
        console.error('An error occurred while calling setCompletionStatus() \n response text was: ' + jqHXR.responseText + 
            + ' \n status was: ' +  textStatus + '\n' + 'Header contents was: ' + errorThrown);

    }).done(function(data){
        if ( (typeof data === 'undefined')||(typeof data === 'null')) {
           failCallback ('Unable to set completion status');
            console.error('An error occurred while calling setCompletionStatus.done() data was null or undefined');
        }
        else  {
                callback(data);
        }   
      });
  }

PHP db查询代码:

public function setSelectionStatus($userID, $selectionComplete, $asJSON = TRUE) {try {
        $queryString = 'UPDATE Visitor 
                        SET Selection_Complete = :selectionComplete 
                        WHERE User_ID = :userID';

        $statementHandle = $this->_dbHandle->prepare($queryString);
        $statementHandle->bindValue(':selectionComplete', $selectionComplete);
        $statementHandle->bindValue(':userID', $userID);
         $success = $statementHandle->execute();
        $numRecords = $statementHandle->rowCount();

        if ( $success === false) {
            if ($asJSON) {
                echo json_encode(-1);
            }
            return -1;
        }
        else if ($numRecords === 0) {
            if ($asJSON) {
                echo json_encode(-2);
            }
            return -2;
        } else {
            if ($asJSON) {
                echo json_encode((int) $selectionComplete);
            }
            return (int) $selectionComplete;
        }
    } catch (PDOException $e) {
        echo 'Sorry,an error occurred while attempting update the current users selection status,administrators please check error logs for more details. <br/>' . PHP_EOL;

        error_log('An error occurred while attempting to run the query function' . __FUNCTION__ . ' of class ' . __CLASS__ . ' on server (' . $_SERVER['SERVER_NAME']
                . ')' . PHP_EOL . 'Details:' . PHP_EOL .
                'Message: ' . $e->getMessage() . PHP_EOL .
                'In file: ' . $e->getFile() . PHP_EOL .
                'On line: ' . $e->getLine() . PHP_EOL .
                'Stack trace:' . PHP_EOL . $e->getTraceAsString() . PHP_EOL, 0);
    }
}

AJAX函数在设置数据库字段时返回正确的值没有错误记录在日志中PDO或PDO语句都没有返回任何错误我也检查了通过firebug来回发送的数据这是和但数据库拒绝更新该字段的空白。 我很确定它与AJAX调用cos有关,如果我直接运行PHP它每次都能完美运行。 对不起它有点长,可以有人帮忙吗?我已经看了将近一天了,我很茫然。 提前致谢。

0 个答案:

没有答案