插入查询以增量方式在 PHP 和 MySQL 中复制记录。

huangapple go评论67阅读模式
英文:

Insertion query duplicating records in incremental manner in php and mysql

问题

这是您要翻译的内容:

"I have a simple commenting system that I'm using AJAX to comment and fetch comments. But I have started experiencing duplicated comments for each comment. Here is how it's going on: the first comment is not duplicated, the second comment gets doubled (gets duplicated), the third gets tripled...and so on in that trend. How do I fix that?"

//这是评论的PHP文件
<?php
include '../DB-config/db-config.php';

if (!empty($_POST['comment_text']) && !empty($_POST['user_email']) && !empty($_POST['user_id']) && !empty($_POST['page_url'])) {

    $comment = $connection->real_escape_string($_POST['comment_text']);
    $user_email = $connection->real_escape_string($_POST['user_email']);
    $user_id = $connection->real_escape_string($_POST['user_id']);
    $page_url = $connection->real_escape_string($_POST['page_url']);
    
    
    $INSERT = "INSERT INTO comments (user_id, user_email, page_url, parent_id, comment) VALUES ('" . $user_id . "','"
     . $user_email . "','" . $page_url . "', '0', '" . $comment . "')";
    
    $sql = mysqli_query($connection, $INSERT);

}
?>

//AJAX

$('#comment-post').on('click', function (e) {
    $(document).on('submit', '#comment-writing form', function (event) {
        event.preventDefault();
        if ($('#comment-textarea').val() == '') {
            $('#comment-notice').html('Write a comment...').css('color', 'red');
            $('#comment-notice').show();
        }
        else {
            $('#comment-notice').hide();
            var user_email = $('#user_email').val();
            var user_id = $('#user_id').val();
            var page_url = window.location.search;
            var comment_text = $('#comment-textarea').val();
            $.ajax({
                url: "../Ajax/comment.php",
                method: 'POST',
                data: { user_email: user_email, user_id: user_id, page_url: page_url, comment_text: comment_text },
                success: function (response) {
                    $('#comment-textarea').empty();
                    $('.emojionearea.emojionearea-inline > .emojionearea-editor').empty();
                }
            });
        }
    })
});
英文:

I have a simple commenting system that I'm using AJAX to comment and fetch comments. But I have started experiencing duplicated comments for each comment. Here is how it's going on: the first comment is not duplicated, the second comment gets doubled(gets duplicated), the third gets tripled...and so on in that trend. How do I fix that?

`//THIS IS THE COMMENTING PHP FILE
&lt;?php
include &#39;../DB-config/db-config.php&#39;;

if (!empty($_POST[&#39;comment_text&#39;]) &amp;&amp; !empty($_POST[&#39;user_email&#39;]) &amp;&amp; !empty($_POST[&#39;user_id&#39;]) &amp;&amp; !empty($_POST[&#39;page_url&#39;])) {

    $comment = $connection-&gt;real_escape_string($_POST[&#39;comment_text&#39;]);
    $user_email = $connection-&gt;real_escape_string($_POST[&#39;user_email&#39;]);
    $user_id = $connection-&gt;real_escape_string($_POST[&#39;user_id&#39;]);
    $page_url = $connection-&gt;real_escape_string($_POST[&#39;page_url&#39;]);
    
    
    $INSERT = &quot;INSERT INTO comments (user_id, user_email, page_url, parent_id, comment) VALUES (&#39;&quot; . $user_id . &quot;&#39;, &#39;&quot; . $user_email . &quot;&#39;,&#39;&quot; . $page_url . &quot;&#39;, &#39;0&#39;, &#39;&quot; . $comment . &quot;&#39;)&quot;;
    
    $sql = mysqli_query($connection, $INSERT);

}
?&gt;`

//AJAX

`$(&#39;#comment-post&#39;).on(&#39;click&#39;, function (e) {
                $(document).on(&#39;submit&#39;, &#39;#comment-writting form&#39;, function (event) {
                    event.preventDefault();
                    if ($(&#39;#comment-textarea&#39;).val() == &#39;&#39;) {
                        $(&#39;#comment-notice&#39;).html(&#39;Write a comment...&#39;).css(&#39;color&#39;, &#39;red&#39;);
                        $(&#39;#comment-notice&#39;).show();
                    }
                    else {
                        $(&#39;#comment-notice&#39;).hide();
                        var user_email = $(&#39;#user_email&#39;).val();
                        var user_id = $(&#39;#user_id&#39;).val();
                        var page_url = window.location.search;
                        var comment_text = $(&#39;#comment-textarea&#39;).val();
                        $.ajax({
                            url: &quot;../Ajax/comment.php&quot;,
                            method: &#39;POST&#39;,
                            data: { user_email: user_email, user_id: user_id, page_url: page_url, comment_text: comment_text },
                            success: function (response) {
                                $(&#39;#comment-textarea&#39;).empty();
                                $(&#39;.emojionearea.emojionearea-inline &gt; .emojionearea-editor&#39;).empty();
                            }
                        });
                    }
                })
            });`

答案1

得分: 0

我已经通过在事件执行完其任务后使用 element.unbind(eventType) 来解决了相同问题。

例如,在我的情况下:$('#comment-post').unbind('click'); 在Ajax响应之后。

英文:

For anyone who may be having the same problem, I have fixed my problem by using element.unbind(eventType) after the event has performed its task

for example in my case: $('#comment-post').unbind('click'); after Ajax response.

huangapple
  • 本文由 发表于 2023年2月10日 04:36:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定