1

在解决编程问题时,我尽量避免寻求帮助,但现在我很困惑,可以从你们这些好人那里得到一些意见。

场景:

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

问题:

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

这是相关的代码:

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,它每次都能完美运行。抱歉,有点长,有人可以帮忙吗?我已经看了将近一天了,我很茫然。提前致谢。

4

0 回答 0