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

标签 javascript php jquery mysql

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

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

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

这是将评论保存到数据库中的函数,它在 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(以数据库为单位)的评论,但如果再次发表评论,则显示的评论将是那些 ID 不正确的评论,只有那些。

可能是什么问题?

提前致谢!

最佳答案

To get the where the error is comming up. Please check with breakpoints for the JS file with keeping breakpoint for the submitComment function check if the return of the function is updated with correct value in JS or PHP

关于javascript - php 类/函数没有得到正确的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350666/

相关文章:

javascript - Bootstrap - 在刷新之间维护 <div> 折叠状态

javascript - 重新排序列表元素 - jQuery?

javascript - TOMCAT报告此错误: INFO: Character decoding failed

php - PhoneGap/Cordova 将 GeoLocation 从 localStorage 变量推送到 MySQL

php - 防止保存时由于 WiFi 连接不良而导致数据库损坏

javascript - 从跨域文件中禁用 Javascript

c# - C# 的内联服务器代码在移动到 JavaScript .js 文件时无法工作

php - Symfony2 + Doctrine 管理大尺寸数据生成错误 : Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 5131594 bytes)

javascript - 通过 jQuery 附加的 HTML 字符串

javascript - jQuery 功能荧光笔插件