jQuery ajax post并不总是适用于长文本


jQuery ajax post does not always work with long texts

由于某种原因,当帖子包含很长的文本(即超过 200 个单词左右(时,长帖子不会插入到数据库中。但是,这个问题非常模糊,因为其中一些长文本入到数据库中(例如,插入一个大的 lorum ipsum 文本没有任何问题(。首先,我认为这与标点符号有关,但这不是问题所在。此外,文本中断等不是问题的原因。此外,我检查了数据库中注释部分的数据结构(分配了一个文本区域,如下所示(,在那里我看到它被赋予了一个长文本作为类型。所以字符限制也不是问题。因此,奇怪的是它只发生在"一些"长文本中。大约 200 字的短文本没有问题,并且插入得很完美。

我的代码如下。首先是 HTML 部分:

    <div class="new-com-bt">
    <span>Schrijf hier uw bericht ....</span>
</div>
<div class="new-com-cnt">
    <input type="text" id="name-com" name="name-com" value="" placeholder="Uw naam" />
    <input type="text" id="mail-com" name="mail-com" value="" placeholder="Uw e-mail adres" />
    <input type="text" id="code-com" name="code-com" value="" placeholder="Viercijferige code..." />
    <textarea class="the-new-com"></textarea>
    <span class="rating2">Beoordeel ons:&nbsp;
        <input type="radio" class="rating-input"
            id="rating-input-2-1" name="example" value="5">
        <label for="rating-input-2-1" class="rating-star"></label>
        <input type="radio" class="rating-input"
            id="rating-input-2-2" name="example" value="4">
        <label for="rating-input-2-2" class="rating-star"></label>
        <input type="radio" class="rating-input"
            id="rating-input-2-3" name="example" value="3">
        <label for="rating-input-2-3" class="rating-star"></label>
        <input type="radio" class="rating-input"
            id="rating-input-2-4" name="example" value="2">
        <label for="rating-input-2-4" class="rating-star"></label>
        <input type="radio" class="rating-input"
            id="rating-input-2-5" name="example" value="1">
        <label for="rating-input-2-5" class="rating-star"></label>
    </span>
    <div class="bt-add-com">Plaats bericht</div>
    <div class="bt-cancel-com">Annuleer</div>
</div>

现在是 JQuery 部分:

<script type="text/javascript">
  $(function(){ 
    //alert(event.timeStamp);
              $('.new-com-bt').click(function(event){    
              $(this).hide();
              $('.new-com-cnt').show();
               $('#name-com').focus();
    });
    /* when start writing the comment activate the "add" button */
    $('.the-new-com').bind('input propertychange', function() {
       $(".bt-add-com").css({opacity:0.6});
       var checklength = $(this).val().length;
       if(checklength){ $(".bt-add-com").css({opacity:1}); }
    });
    /* on clic  on the cancel button */
    $('.bt-cancel-com').click(function(){
        $('.the-new-com').val('');
        $('.new-com-cnt').fadeOut('fast', function(){
            $('.new-com-bt').fadeIn('fast');
        });
    });
    // on post comment click 
    $('.bt-add-com').click(function(){
        var theCom = $('.the-new-com');
        var theName = $('#name-com');
        var theMail = $('#mail-com');
        var theCode = $('#code-com');
        var theRating = $('input[name=example]:checked');
        if( !theCom.val()){ 
            alert('U moet een bericht schrijven'); 
        }else if(theCode.val() != '7624'){
            alert('Vul de viercijferige code in die u heeft gekregen tijdens de ceremonie')
        }else{
            $.ajax({
                type: "POST",
                url: "ajax/add-comment.php",
                data: 'act=add-com&id_post='+<?php echo $id_post; ?>+'&name='+theName.val()+'&email='+theMail.val()+'&comment='+theCom.val()+'&rating='+theRating.val(),
                success: function(html){
                    theCom.val('');
                    theMail.val('');
                    theName.val('');
                    theRating.val('');
                        setTimeout(function(){
                            //fade back
                            $('.new-com-cnt').html("Dank u wel voor uw bericht. Deze zal zo spoedig mogelijk op de site verschijnen!");
                        }, 0);
                    }  
            });
        }
    });
});

这是添加注释.php脚本:

<?php
extract($_POST);
if($_POST['act'] == 'add-com'):
$name = htmlentities($name);
$email = htmlentities($email);
$comment = htmlentities($comment);
$rating = htmlentities($rating);
include('../config.php'); 
// Get gravatar Image 
// https://fr.gravatar.com/site/implement/images/php/
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . $default . "&s=" . $size;
if(strlen($name) <= '1'){ $name = 'Guest';}
//insert the comment in the database
mysql_query("INSERT INTO comments (name, email, comment, id_post, rating, display)VALUES( '$name', '$email', '$comment', '$id_post', '$rating', 'nee')");
if(!mysql_errno()){
?>
<div class="cmt-cnt">
    <img src="<?php echo $grav_url; ?>" alt="" />
    <div class="thecom">
        <h5><?php echo $name; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo date('d-m-Y H:i'); ?></span><span class="com-dt-rating"><span class="rating">
                <input type="radio" class="rating-input"
                    id="rating-input-1-1" value="5" disabled="disabled" <?php echo ($rating=='5')?'checked':'' ?> />
                <label for="rating-input-1-1" class="rating-star"></label>
                <input type="radio" class="rating-input"
                    id="rating-input-1-2" value="4" disabled="disabled" <?php echo ($rating=='4')?'checked':'' ?> />
                <label for="rating-input-1-2" class="rating-star"></label>
                <input type="radio" class="rating-input"
                    id="rating-input-1-3" value="3" disabled="disabled" <?php echo ($rating=='3')?'checked':'' ?> />
                <label for="rating-input-1-3" class="rating-star"></label>
                <input type="radio" class="rating-input"
                    id="rating-input-1-4" value="2" disabled="disabled" <?php echo ($rating=='2')?'checked':'' ?> />
                <label for="rating-input-1-4" class="rating-star"></label>
                <input type="radio" class="rating-input"
                    id="rating-input-1-5" value="1" disabled="disabled" <?php echo ($rating=='1')?'checked':'' ?> />
                <label for="rating-input-1-5" class="rating-star"></label>
            </span></span>
        <br/>
        <p>
            <?php echo $comment; ?>
        </p>
    </div>
</div><!-- end "cmt-cnt" -->
<?php } ?>

插入的长文本示例:

论文中描述的用于建模和形式化需求的技术称为FLAGS。标志 除了清晰的目标之外,还提供了对模糊目标进行建模和形式化的可能性。这些与众不同 模糊目标是它们不能具体实现,而清晰的目标可以。相反, 这些目标的可实现性是模糊的,因此它们不能满足或不满足,但它们可以 在一定程度上得到满足。以驾驶汽车为目标,节省燃料。目前尚不清楚 这一目标是否具体实现。帽子是,油耗只能很小,但这并不重要 说明目标是否明确实现。相反,它只是在一定程度上满足, 即当汽车仅使用少量燃料时。同样的思维方式可以应用于 物理治疗通过模拟这些目标来评估运动的速度和正确性 由一个人实现,只有当FLAGS目标模型元素 形式化。

需要

遵循几个步骤才能应用 FLAGS。 这些步骤可以在附录 A 中找到 表 1 并将在本章中通过描述一个示例来进一步阐述,其中应用程序 被描述为学习机器人如何像人类一样移动。由于示例涉及机器人 应用程序,从而描述了另一种情况,来自Pasquale等人的FLAGS元模型(2013( 扩展到本章中描述的示例。他的扩展FLAGS元模型可以是 附录A中的图2中查阅了。此外,对于示例,假设 需求已经确定。

FLAGS元模型由几个类组成,这些类描述了FLAGS目标的组成 抽象级别的模型。它解释了清晰的目标(明确的目标(或模糊的目标(可实现 特定范围(可以建模。此外,目标可以相互影响(受关系影响( 并且可以分解为子目标(按关系分解(。实现这些目标是 对人和机器人都很重要。此外,领域假设(条件(和 在FLAGS元模型中采用操作,一个人使用控制器来监控他或她 运动,以便机器人能够对这些运动做出反应

未插入的文本示例:

Mijn iboga ervaring bij iboga farm Zeer mooie en rustige locatie en een heerlijke sfeer, goed verzorgd. Geen haastigheid maar alles in een relaxte flow. Ik wist niet wat ik moest verwachten maar de jongens stellen je goed gerust, goede uitleg en er word goed rekening gehouden met je gezondheid toestand en eet gewoontes. Alles is aanwezig wat men nodig heeft.Eten,drinken,douche gelegenheid,slaap plek 等 Mooie ceremonie voorafgaand. Eerst krijg je een test dosis om vast te kunnen stellen of men allergisch is voor de substantie. Iets wat veel andere niet doen. Als het eenmaal werkt word de sessie begonnen terwijl je boven op een comfortabel bed ligt, Met een spiritueel muziekje op de achtergrond. Toen het eenmaal goed werkte ervaarde ik een soort van ijl achtige droomstaat trip waarbij er visioenen tevoorschijn komen. Ik persoonlijk heb vorige levens en voorouders gezien en gevoeld. Ondertussen word er zeer goed op je gelet en je word uitstekend verzorgd, je zal niks tekort komen gedurende de iboga reis. Als men het toilet nodig heeft wel even om assistentie vragen want het lopen gaat wat moeilijk. Tijdens de reis zal het voor ieder andere anders zijn want het zijn allemaal persoonlijke kwesties die verwerkt worden. Ondertussen de sessie merk je al dat er veel dwars liggende emotie's en gewoontes verdwijnen uit je systeem, en

示例 1 没有单引号。示例 2 具有单引号,因此您的数据库将引发 1064 错误。使用mysql_errno()时,如果有错误,您应该对错误执行一些操作。所以在你的关闭时做

} else {
    echo mysql_errno() . ": " . mysql_error();
}

该错误发生在emotie's,因为'关闭了 SQL 字符串,然后剩余的文本 SQL 不知道如何处理。这也是SQL注入的工作方式。恶意使用该',然后传入 SQL 命令。使用 mysql_ 函数,您必须使用转义函数来防止这种情况。不过,您真的应该更新到mysqli_PDO。然后,可以使用驱动程序处理报价的参数化查询。

因此,要分配变量,它们应该是:

$name = mysql_real_escape_string(htmlentities($name);
$email = mysql_real_escape_string(htmlentities($email));
$comment = mysql_real_escape_string(htmlentities($comment));
$rating = mysql_real_escape_string(htmlentities($rating));

另请注意手册页面上的警告,这不是最好的方法,但这是mysql_驱动程序的最佳方法。 http://php.net/manual/en/function.mysql-error.php

有关该主题的更多信息,请参阅:
如何防止 PHP 中的 SQL 注入?
https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

您需要使用准备好的 SQL 语句,这样您就不会遇到 sql 注入问题,并且用户输入中的撇号 ' 不会杀死您的插入。

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

你的代码应该类似于这个

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$databasename = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $databasename);
// Was there a connection error?
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO comments (name, email, comment) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $comment);
// set parameters and execute
$name = "John";
$email = "john@example.com";
$comment = "TEST";
$stmt->execute();