简体   繁体   English

插入mysql时丢失发布数据

[英]Losing post data when inserting into mysql

This problem seems very strange to me because it is intermittant. 这个问题对我来说似乎很奇怪,因为它是间歇性的。 I am going to be detailed so please bear with me. 我将详细介绍,请耐心等待。

I have a textarea which is made into a wysiwyg editor with http://redactorjs.com 我有一个textarea,可以通过http://redactorjs.com制作为所见即所得的编辑器

I am using the click-to-edit feature (http://redactorjs.com/docs/examples/click-to-edit/) and saving the data with an ajax post. 我正在使用点击编辑功能(http://redactorjs.com/docs/examples/click-to-edit/),并使用ajax发布保存数据。

The mysql database field is called 'Info' and is set to type 'blob'. mysql数据库字段称为“信息”,并设置为“ blob”类型。

Now, if i enter the following text into the textarea and press save, it will probably store it fine. 现在,如果我在文本区域中输入以下文本并按保存,则可能会很好地存储它。

"Hello my name is greg and i am a very good actor. I am also a cooking enthusiast who once came 2nd in a cooking competition." “你好,我叫格雷格,我是一个很好的演员。我也是一个烹饪爱好者,曾经在烹饪比赛中排名第二。”

Then if i click edit and add to it 然后,如果我单击“编辑”并添加到它

"Hello my name is greg and i am a very good actor. I am also a cooking enthusiast who once came 2nd in a cooking competition. I also like to swim on a thursday morning!" “你好,我叫格雷格,我是一个非常好的演员。我也是烹饪爱好者,曾经在烹饪比赛中获得第二名。我也喜欢在周四早上游泳!”

I sometimes see that only something like the below will be saved into the db. 有时我只会看到类似以下内容的内容会保存到数据库中。

"Hello my name is greg and i am a very good actor. I am also a cooking enthusiast who once came" “你好,我叫格雷格,我是一个很好的演员。我也是曾经来的烹饪爱好者”

Then if if edit it again and add the same text back in and save again it saves successfully!?!? 然后,如果再次进行编辑并再次添加相同的文本并再次保存,则保存成功!!!?

Let me show you some code.. 让我给你看一些代码..

view.php view.php

echo the info data 回显信息数据

<script>
function ClickToEditInfo()
{ 
    var info =  ['formatting', '|', 'bold', 'italic', 'deleted', '|',
                    'unorderedlist', 'orderedlist', 'outdent', 'indent', '|',
                    'table', 'link', '|',
                    'fontcolor', 'backcolor', '|', 
                    'alignleft', 'aligncenter', 'alignright', 'justify', '|',
                    'horizontalrule']
    $('#info').redactor({ 
            focus: true,
            buttons: info,
            wym: true,
            fixed: true
    }); 
}

function ClickToSaveInfo()
{
    var html = $('#info').getCode();
    var item = <?php echo $item_id; ?>;

    $.ajax({
            type: 'POST',
            url: "ajax/clickToEditInfo.php",
            data: "item_id="+item+"&html="+html,
            error: function(xhr, status, error) {
                console.log(error);
            },
            beforeSend: function() {
                $("#ajax").show();
                $("#ajax").html('<span class="label label-info">Saving info.. please wait</span>');
            },
            success: function(html){
                console.log(html);
                $("#ajax").html('<span class="label label-success">Saved!</span>');
                $("#ajax").fadeOut(1000, function () {
                    $(this).hide();
                });
            }
    });

    $('#info').destroyEditor(); 
}
<?php 
    endif; // end check if user owns item
}
?>
</script>

        <div id="info">
            <?php echo $item->getMainField('info', $item_id); ?>
        </div>  

clickToEditInfo.php clickToEditInfo.php

<?php 
include_once('../classes/item.class.php');
if (!isset($_SESSION)) session_start();

$item_id = $_POST["item_id"];
$html = $_POST["html"];
$user_id = $_SESSION['jigowatt']['user_id'];

if( $item->checkUserOwns($user_id, $item_id) ) : //check user owns item

    $item->clickToEditInfo($user_id, $item_id, $html);

else : // user does not own it
    return false;
endif; // end check if user owns item
?>

and

item.class.php > clickToEditInfo item.class.php> clickToEditInfo

public function clickToEditInfo($user_id, $item_id, $html) {
    $stmt = parent::query("UPDATE `item_main` SET `info` = '$html' WHERE `item_id` = $item_id AND `user_id` = $user_id");
}

Can you think of a reason why the data posted in the DB gets intermittently truncated? 您能想到数据库中的数据被间歇性地截断的原因吗?

UPDATE: 更新:

I have found a way to reliably reproduce it. 我找到了一种可靠地复制它的方法。 like i said its a wysiwyg editor. 就像我说的是所见即所得的编辑器。

"Hello my name is greg and i am a very good actor. I am also a cooking enthusiast who once came 2nd in a cooking competition." “你好,我叫格雷格,我是一个很好的演员。我也是一个烹饪爱好者,曾经在烹饪比赛中排名第二。”

if i highlight competition and click the hyperlink button and link it to google for example. 如果我突出显示竞争,然后单击超链接按钮并将其链接到Google,例如。 Press Save and the following is inserted. 按保存,然后插入以下内容。

"Hello my name is greg and i am a very good actor. I am also a cooking enthusiast who once came 2nd in a cooking" “你好,我叫格雷格,我是一个很好的演员。我也是一个烹饪爱好者,曾经在烹饪界排名第二。”

i have edited 我已经编辑

<?php 
include_once('../classes/item.class.php');
if (!isset($_SESSION)) session_start();

$item_id = $_POST["item_id"];
$html = $_POST["html"];
$user_id = $_SESSION['jigowatt']['user_id'];

if( $item->checkUserOwns($user_id, $item_id) ) : //check user owns item

    $item->clickToEditInfo($user_id, $item_id, $html);

    **echo $html;**

else : // user does not own it
    return false;
endif; // end check if user owns item
?>

and the console.log(html) is showing 并且console.log(html)正在显示

<p>Hello my name is greg and i am a very good actor. I am also a cooking enthusiast who once came 2nd in a cooking

so its not posting all the data, something to do with the html tags? 所以它没有发布所有数据,这与html标签有关?

UPDATE2 更新2

I have done console log on var html = $('#info').getCode(); 我已经在var html = $('#info')。getCode();上完成了控制台登录 and confirmed that 并确认

<p>Hello my name is greg and i am a very good actor.</p><p>I am also a cooking enthusiast who once came 2nd place on Come dine with me :)&nbsp;<a href="goggle" target="">link</a>&nbsp;</p>

is posted, yet only 已发布,但仅

<p>Hello my name is greg and i am a very good actor.</p><p>I am also a cooking enthusiast who once came 2nd place on Come dine with me :) 

is received in clickToEditInfo.php 在clickToEditInfo.php中收到

how do i fix this? 我该如何解决?

通过POST发送之前转义帖子数据

var html = escape($('#info').getCode()); 

Think i may have fixed it with escape() on the html before i post it. 认为我在发布之前可能已经使用html上的escape()修复了它。

http://www.w3schools.com/jsref/jsref_escape.asp http://www.w3schools.com/jsref/jsref_escape.asp

var html = $('#info').getCode();
    var item = <?php echo $item_id; ?>;
    var html = escape(html);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM