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

时间:2011-08-17 18:49:09

标签: php mysql utf-8 textarea wysiwyg

我正在使用名为“whizzywig”的wysiwyg,以便使用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 个答案:

答案 0 :(得分:1)

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

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

你无需从数据库中获取数据。

相关问题