2016-02-06 22 views
0

由于某些原因,当帖子包含非常长的文本(即超过200个字左右)时,长篇帖子未插入到数据库中。但是,这个问题非常含糊,因为这些长文本中的一些被插入到数据库中(例如插入大量文本文件而没有任何问题)。首先,我认为它与标点符号有关,但这不是问题。此外,文本中断等不是问题的原因。此外,我在数据库中检查了注释部分的数据结构(它被分配了如下所示的textarea),并且在那里我看到它已经被赋予了长文本作为一种类型。所以字符限制也不是问题。因此,奇怪的是,它只发生在“一些”长文本中。大约200字的短文本没有问题,并且完美插入。jQuery ajax post并不总是与长文本一起工作

我的代码如下。首先,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); 
        } 
      }); 
     } 
    }); 

}); 

这是附加comment.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元模型进行采纳,一个人使用控制器来监视他或她的 运动使机器人可以对这些运动

的一个例子回应不插入文本:

Mijn iboga ervaring BIJ iboga农场 泽尔mooie连接rustige locatie连接EEN heerlijke sfeer,verzorgd GOED。 Geen haastihehe 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 jezondheid toestand en eet gewoontes。 Alles is aanwezig wat men nodig heeft。 Eten,drinken,douche gelegenheid,slaap plek等 Mooie ceremonie voorafgaand。 Erst krijg je een test test dom 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 je ge en en je word uitstekend verzorgd,je zal niks tekort komen gedurende de iboga reis。 Als men het toilet nodig heeft wel even even om assassie 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德sessie梅尔克济人DAT呃veel dwars liggende emotie的连接gewoontes verdwijnen UIT JE systeem,EN

+1

单引号将打破这一点,你巧合是开放的SQL注入。 – chris85

回答

1

例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

欲了解更多的主题参见:
How can I prevent SQL injection in PHP?
https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

+0

Thnx克里斯你的帮助。解决方案之后,我仍然需要在客户端JQuery中添加一件事。我需要添加encodeURIComponent()来处理&符号。现在一切工作正常! – user2237168

0

你需要使用准备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 = "[email protected]"; 
$comment = "TEST"; 
$stmt->execute(); 
+0

Thnx关于安全性的评论!但是,您是否知道是什么导致了这个问题?插入小文本(即最多200个单词都可以)。问题是长文本中没有插入“一些”长文本 – user2237168

+0

,有没有撇号? – PHPDave

+0

@ user2237168“长文本”在某些时候可能有引号。提供具体帮助的问题的可重现示例。 – chris85