2016-02-06 17 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