在mysql数据库中保存whizzywig文本的问题

Posted

技术标签:

【中文标题】在mysql数据库中保存whizzywig文本的问题【英文标题】:problems saving whizzywig text in mysql database 【发布时间】:2011-10-29 04:45:19 【问题描述】:

我正在使用名为“whizzywig”的所见即所得,以便使用 php 将内容保存在 mysql 数据库中。问题是,如果我有时使用粗体或斜体等格式,文本无法正确保存。

文本被保存,但只有一段文本。通常在多次更改格式后,更改后的文本不会保存。

我认为是编纂或类似的问题。

我使用以下函数:

要获取文本:utf8_decode($row['text'])

要保存文本:utf8_encode('text')

我使用 ajax 将文本保存在数据库中。根据“whizzywig”的说明,我必须在获取日期之前使用函数syncTextarea()。我也是。

我认为这可能是编码的问题,也可能是“whizzywig”的问题。

这是我显示数据库文本的代码:

<?
$result = mysql_query("SELECT * FROM noticias order by fecha desc limit $RegistrosAEmpezar, $RegistrosAMostrar");
echo mysql_error($con);
?>
<table>
<?
if(mysql_num_rows($result)==0)

    ?> <tr><td colspan="4">No hay noticias en la base de datos</td></tr> <?

else

    while($row = mysql_fetch_array($result))
      
          ?>
          <tr><th>Fecha:</th><td><?= $row['fecha'] ?></td><th>Título</th><td><?= $row['titulo'] ?></td></tr>
          <tr><th>Texto:</th><td colspan="3"><?= corta_texto(utf8_decode($row['texto'])) ?></td></tr>
          <tr><td colspan="4"><a href="javascript:eliminar_noticia(<?= $row['id_noticia'] ?>)">Eliminar</a> | <a href="javascript:popUp('sec_admin/not_upd.php?keepThis=true&TB_iframe=true&height=600&width=760&id_noticia=<?= $row['id_noticia'] ?>')">Actualizar</a> | <a href="javascript:popUp('sec_admin/not_det.php?keepThis=true&TB_iframe=true&height=600&width=760&id_noticia=<?= $row['id_noticia'] ?>')">Mostrar</a>

          </td></tr>
          <?
        

?>

这是我用文字填写的表格:

<form id="noticias_ins">
<fieldset>
<table>
    <tr><td>
    Título:
    </td><td>
    <input type="text" name="titulo" size="90" />
    </td></tr>
    <tr><td colspan="2">
    Contenido:
    </td></tr>
    <tr><td colspan="2">
    <textarea style="width:100%; height:300px" name="texto"></textarea>
    </td></tr>
    <tr><td colspan="2">
    <input type="button"  value="guardar" onclick="guarda_noticia()" />
    </td></tr>
    </table>
</fieldset>
</form>

这是为了保存文本而调用的 ajax 方法:

//Guardar noticia
function guarda_noticia()

//Obtengo form y campos
var form=document.getElementById("noticias_ins");

var titulo=form.elements["titulo"].value;
syncTextarea();
var texto=form.elements["texto"].value;

if( titulo.length==0 || texto.length==0)

    document.getElementById("info").innerhtml="Error, el título y el téxto no pueden estar vacios";
document.getElementById("info").style.display='block';
return;


xmlhttp_not.onreadystatechange=function()

  if (xmlhttp_not.readyState==4 && xmlhttp_not.status==200)

    document.getElementById("info").innerHTML=xmlhttp_not.responseText;
document.getElementById("info").style.display='block';
verNoticias(1);
listado_mostrado_not=true;


//Envío al php para la inserción
syncTextarea();
xmlhttp_not.open("GET","sec_admin/not_ins.php?titulo="+titulo+"&texto="+texto,true);
xmlhttp_not.send();

这是我用来保存数据的查询:

<?
//Listado de noticias
include("../conexion.php");

$titulo=$_GET['titulo'];
$texto=utf8_encode($_GET['texto']);
$fecha=date("Y-m-d H:i:s");

$result = mysql_query("INSERT INTO noticias (fecha, titulo, texto)
VALUES ('".$fecha."', '".$titulo."', '".$texto."')");

if($result)
echo "Se ha insertado correctamente la noticia";
else
echo "Ha habido errores en la inserción de la noticia";

mysql_close($con);

?>

先谢谢了。

【问题讨论】:

【参考方案1】:

使用它来保存来自 whizziwig 的数据:

$txt = htmlspecialchars($txt, ENT_QUOTES);
$txt = mysql_real_escape_string($txt);

而且您无需从数据库中取回数据。

【讨论】:

以上是关于在mysql数据库中保存whizzywig文本的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 PHP 在 MySQL 数据库中将纯文本 CSV 字段保存为 HTML?

如何在 Android 上将数据保存到文本文件?

使用JSON php脚本在Android应用程序的MySQL服务器中显示保存在varchar(utf8_unicode_ci)中的语言文本

将nicedit textarea内容保存到mysql数据库中

Python 2.7 MySQLdb将从数据库读取的最后一行保存到文本文件中

在mysql数据库中,一张表最多能保存多少条数据