php类/函数没有得到正确的id

时间:2014-10-13 23:54:47

标签: javascript php jquery mysql

我在php / jscript中有一个用于照片的评论系统,并且工作正常,直到我退出图像和评论所在的块预览或点击下一个/上一个。

问题是,当我在块中时我可以发表评论并且保存的(db)id是正确的,但如果我退出或单击下一个/上一个按钮,则保存的ID不同。我在谈论照片ID和属于该id的评论。

我刚注意到id是我第一次发布的帖子数量(没有关闭或更改图片)少1.例如,没有评论的照片:我发表5条评论而不关闭或更改当前照片,然后当我更改或关闭它并再次打开它并发表评论时,保存的id将是少于1的第一个数字。在这种情况下,我会在评论表中看到photo_id = 4而不是正确的照片ID,但前5个评论将有真实的照片ID。

这是将注释保存到db中的函数,它位于CMyComments类:

function acceptComment() {
    $iItemId = (int)$_POST['id']; // prepare necessary information
    $sIp = $this->getVisitorIP();
    $sName = $GLOBALS['MySQL']->escape(strip_tags($_POST['name']));
    $sText = $GLOBALS['MySQL']->escape(strip_tags($_POST['text']));

    if ($sName && $sText) {
        // check - if there is any recent post from you or not
        $iOldId = $GLOBALS['MySQL']->getOne("SELECT `c_item_id` FROM `s281_items_cmts` WHERE `c_item_id` = '{$iItemId}' AND `c_ip` = '{$sIp}' AND `c_when` >= UNIX_TIMESTAMP() - 1 LIMIT 1");
        if (! $iOldId) {
            // if everything is fine - allow to add comment
            $GLOBALS['MySQL']->res("INSERT INTO `s281_items_cmts` SET `c_item_id` = '{$iItemId}', `c_ip` = '{$sIp}', `c_when` = UNIX_TIMESTAMP(), `c_name` = '{$sName}', `c_text` = '{$sText}'");
            $GLOBALS['MySQL']->res("UPDATE `user_uploads` SET `comments_count` = `comments_count` + 1 WHERE `id` = '{$iItemId}'");

            // and print out all comments
            $sOut = '';
            $aComments = $GLOBALS['MySQL']->getAll("SELECT * FROM `s281_items_cmts` WHERE `c_item_id` = '{$iItemId}' ORDER BY `c_when` DESC");
            foreach ($aComments as $i => $aCmtsInfo) {
                $sWhen = date('F j, Y H:i', $aCmtsInfo['c_when']);
                $sOut .= <<<EOF
                            <div class="comment" id="{$aCmtsInfo['c_id']}">
                                <p>Comment from {$aCmtsInfo['c_name']} <span>({$sWhen})</span>:</p>
                                <p>{$aCmtsInfo['c_text']}</p>
                            </div>
EOF;
            }
            return $sOut;
        } else return 1;
    }        
}

这是javascript函数:

// submit comment
function submitComment(id) {
    var sName = $('#name').val();
    var sText = $('#text').val();

    if (sName && sText) {
        $.post('/views/site/reto.php', { action: 'accept_comment', name: sName, text: sText, id: id }, 
            function(data){ 
                if (data != '1') {
                    $('#comments_list').fadeOut(1000, function () {
                        //$('#name').val() = "";
                        //$('#text').val() = "";
                        $(this).html(data);
                        $(this).fadeIn(1000);
                    });
                } else {
                    $('#comments_warning2').fadeIn(1000, function () {
                        $(this).fadeOut(1000);
                    });
                }
            }
        );
    } else {
        $('#comments_warning1').fadeIn(1000, function () { 
            $(this).fadeOut(1000); 
        }); 
    }
};

这是照片脚本:

if ($_POST['action'] == 'get_info' && (int)$_POST['id'] > 0) {

    require_once('classes/CMySQL.php'); // include service classes to work with database and comments
    require_once('classes/CMyComments.php');

    // get photo info
    $iPid = (int)$_POST['id'];
    $aImageInfo = $GLOBALS['MySQL']->getRow("SELECT * FROM `user_uploads` WHERE `id` = '{$iPid}'");

    // prepare comments
    $sCommentsBlock = $GLOBALS['MyComments']->getComments($iPid);

    $aItems = $GLOBALS['MySQL']->getAll("SELECT * FROM `user_uploads` ORDER by `up_time` DESC"); // get photos info

    // Prev & Next navigation
    $sNext = $sPrev = '';
    $iPrev = (int)$GLOBALS['MySQL']->getOne("SELECT `id` FROM `user_uploads` WHERE `id` < '{$iPid}' ORDER BY `id` DESC LIMIT 1");
    $iNext = (int)$GLOBALS['MySQL']->getOne("SELECT `id` FROM `user_uploads` WHERE `id` > '{$iPid}' ORDER BY `id` ASC LIMIT 1");
    $sPrevBtn = ($iPrev) ? '<div class="preview_prev" onclick="getPhotoPreviewAjx(\''.$iPrev.'\')"><img src="/images/prev.png" alt="prev" /></div>' : '';
    $sNextBtn = ($iNext) ? '<div class="preview_next" onclick="getPhotoPreviewAjx(\''.$iNext.'\')"><img src="/images/next.png" alt="next" /></div>' : '';

    require_once('classes/Services_JSON.php');
    $oJson = new Services_JSON();
    header('Content-Type:text/javascript');
    echo $oJson->encode(array(
        'data1' => '<img class="fileUnitSpacer" src="/images/user_uploads/'. $aImageInfo['filename'] .'">' . $sPrevBtn . $sNextBtn,
        'data2' => $sCommentsBlock,
    ));
    exit;
}

当我再次打开照片时,它会显示具有正确ID的评论(在db中),但如果再次发表评论,则显示的评论将是那些ID不正确的评论,只有那些。

可能是什么问题?

提前致谢!

1 个答案:

答案 0 :(得分:0)

  

获取错误发生的位置。    请检查JS文件的断点,并为submitComment函数保留断点,检查函数的返回是否使用JS或PHP中的正确值更新