字符串值在MySQL数据库中累积空格

时间:2014-10-07 07:57:39

标签: javascript php html mysql

我有一个网页,管理员用户可以在其中编辑页面上的文字。但是当他们将文本插入到mysql数据库中时,它有时会在acual内容之前添加越来越多的空格 如果您将cursur放在页面上的第一个单词之前,并且垃圾邮件退格一段时间,则数据库中的空白会消失。但随着时间的推移,您继续编辑页面越多,就会再次添加越来越多的空白。

我在拍摄时遇到了很多麻烦,但我无法弄清楚是什么原因导致添加空白。它并不总是发生,因此很难排除故障。

这是我的代码:

由于我的代码很长,我试图将大部分内容翻译成英文 如果您想要翻译已经翻译过的内容,原始语言是荷兰语。

over_ons.php - 显示数据库中的编辑按钮和页面内容。

//Active page:
$pagina = 'over_ons'; ?>
<input type='hidden' id='pagina' value='<?php echo $pagina; ?>'> <!--Show active page to javascript--><?php
//Active user:
if(isset($_SESSION['correct_ingelogd']) and $_SESSION['functie']=='admin'){
$editor = $_SESSION['gebruikersnaam']; ?>
<input type='hidden' id='editor' value='<?php echo $editor; ?>'> <!--Show active user to javascript--><?php  
} ?>

<!--Editable DIV: -->
<div class='big_wrapper'><?php
        //Get eddited page content from the database
        $query=mysql_query("SELECT inhoud FROM paginas WHERE naam_pagina='" .$pagina. "'");
        while($inhoud_test=mysql_fetch_array($query)){
            $inhoud=$inhoud_test[0];
        }

    //Show Content
    ?><div id='editedText'><?php echo $inhoud; ?></p></div>

<!--Show edit button-->
<?php
if(isset($_SESSION['correct_ingelogd']) and $_SESSION['functie']=='admin')
{?>
    <div id='sidenote'>
        <input type='button' value='Bewerken' id='sent_data' class='button' />
        <div id="feedback" />
    </div>
<?php }

javascript.js - 将页面内容发送到php文件sent_data.php:

//If the system is in edit mode and the user tries to leave the page,
//let the user know it is not so smart to leave yet.
$(window).bind('beforeunload', function(){
var value = $('#sent_data').attr('value'); //change the name of the edit button

if(value == 'Verstuur bewerkingen'){
    return 'Are you sure you want to leave the page? All unsaved edits will be lost!';
}
});

//Make content editable and send page content
$('#sent_data').click(function(){
    var value = $('#sent_data').attr('value'); //change the name of the edit button

    if(value == 'Bewerken'){
        $('#sent_data').attr('value', 'Verstuur bewerkingen');  //change the name of the edit button
        var $div=$('#editedText'), isEditable=$div.is('.editable'); //Make div editable
        $div.prop('contenteditable',!isEditable).toggleClass('editable')
        $('#feedback').html('<p class="opvallend">The content from<BR>this page is now<BR>editable.</p>');
    }else if(value == 'Verstuur bewerkingen'){
                var pagina = $('#pagina').val();
                var editor = $('#editor').val();
                var div_inhoud = $("#editedText").html();
                $.ajax({
                type: 'POST',
                url:  'sent_data.php',
                data: 'tekst=' +div_inhoud+ '&pagina=' +pagina+ '&editor=' +editor,
                success: function(data){
                Change the div back tot not editable, and change the button's name
                    $('#sent_data').attr('value', 'Bewerken');  //change the name of the edit button
                    var $div=$('#editedText'), isEditable=$div.is('.editable'); //Make div not editable
                    $div.prop('contenteditable',!isEditable).toggleClass('editable')

                //Tell the user if the edditing was succesfully
                    $('#feedback').html(data);
                    setTimeout(function(){
                        var value = $('#sent_data').attr('value'); //look up the name of the edit button
                        if(value == 'Bewerken'){ //Only if the button's name is 'bewerken', take away the help text
                        $('#feedback').text('');
                        }
                        }, 5000);
                    }
                    }).fail(function() {
                    //If there was an error, let the user know
                        $('#feedback').html('<p class="opvallend">There was an error.<BR>Your changes have<BR>not been saved.<BR>Please try again.</p>');
                    });
    }
});


最后,
sent_data.php - 从javascript,js获取页面内容并插入数据库:

<?php
session_start();
include_once('./main.php');
include($main .'connectie.php');

//Look up which page has to be edited
$pagina=$_POST['pagina'];
//Get the name of the person who eddited the page
$editor=$_POST['editor'];
//Get content:
$tekst=$_POST['tekst'];
$tekst = mysql_real_escape_string($tekst);
$tekst = trim($tekst);

$query="UPDATE paginas SET naam_editer='" .$editor. "', inhoud='" .$tekst. "' WHERE naam_pagina='" .$pagina. "'";

}
    if(mysql_query($query)){
        echo "<p class='opvallend'>Successfully saves changes.</p>";
    }else{
        echo "<p class='opvallend'>Saving of changes failed.<BR>
        Please try again.</p>";
    }
?>

额外信息:
PHP版本:5.5.15
jQuery版本:1.11.1
在浏览器中测试:Chrome
数据库:phpMyAdmin 5.5.39
内容以VARCHAR类型插入,空格为10000个字符

先谢谢你的帮助!

感谢很多人的大力帮助,尤其是@avnishkgaur,现在它的工作非常好。这是我调整的内容(也在上面的代码中更改了它,以便现在正在编写代码)。

1。 Removed all the white spaces I had in my code between <div id='editable'> and <?php
2。 Added $tekst = trim($tekst); to my PHP file to remove white spaces (didn't work)
3。 Placed the editable text into another div as the code for getting the data from the database (was in the same div before)
4。 Renamed the ID from the editable div to editedText. Also changed the name in the javascript file. This solution made it work perfectly (it was 'editable' before).

这是一种意想不到的解决方案,所以我认为这也可以帮助其他人。

4 个答案:

答案 0 :(得分:1)

至于为什么添加额外的空格,我认为这是因为你直接将数据库中的文本插入到div中(其中包含html代码中的一些空格,在呈现页面时会删除)。

一个有效的解决方案是在

标签中插入您的内容,可能是这样的:

<div class='big_wrapper'>
<p id='editable'></p>
</div>

另一个解决方案是在插入db之前修剪文本。您可以在javascript post阶段或在插入mysql db之前执行此操作。

在jQuery中,(你正在使用)你可以实现这个:

data: 'tekst=' +$.trim(div_inhoud)+ '&pagina=' +pagina+ '&editor=' +$.trim(editor),

或者在sent_data.php中,您可以在更新查询中使用TRIM mysql函数。

答案 1 :(得分:0)

首先,我强烈建议使用mysqli或PDO而不是PHP中的mysql函数,因为这些函数已被弃用。请在PHP.net上查看。

至于你的问题,我没有尝试重现这个问题。我建议你记录并检查一步一步发生的事情,例如记录div_inhoud var,这个阶段已包含的空格了吗?等等。

如果你赶时间,你也可以在你的$tekst sent_data.php var上使用PHP函数ltrim,它将修剪左侧的所有空格(或任何空格)你想要从字符串修剪的字符)

答案 2 :(得分:0)

尝试在send_data.php中添加trim()函数,这有助于您去除空格。

$tekst = trim($_POST['tekst']);

答案 3 :(得分:0)

我的猜测是<div id='editable'>?>之后的换行符以及 over_ons.php <?php的缩进添加了额外的空格。具体来说,还有一些额外的/n,以及文件中缩进的空格。

您可以在将空白保存到数据库之前修剪空白,或者在ajax调用之前,或两者都修剪空白。

在php中,trim()

<强> sent_data.php

$tekst = trim($tekst);

或javascript,str.trim()

<强> javascript.js

var div_inhoud = $("#editable").html().trim();

此外,您可能需要考虑使用PHP Data Objects,而不是将变量直接插入到SQL语句中。它实际上非常容易使用,在我看来,代码更容易阅读,更可重用。有一个fantastic tutorial by Tuts+可以让您轻松学习并开始使用。这将确保您不会意外地允许SQL注入问题进入您的应用程序。