php - 在 mysql 数据库中保存 whizzywig 文本时出现问题

标签 php mysql utf-8 textarea wysiwyg

我正在使用名为“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);

?>

谢谢。

最佳答案

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

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

而且您无需执行任何操作即可从数据库取回数据。

关于php - 在 mysql 数据库中保存 whizzywig 文本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7097865/

相关文章:

php - 列出数据库中最常见电子邮件后缀的 SQL 代码

php - 配置文件的最佳文件格式是什么?

php - 创建简单的 MySQL 更新触发器时出现问题

mysql - 在整个Mysql表(所有字段)中搜索特定字符串

mysql - 如何使用单个查询计算 SQL (MySQL) 表中行的出现概率?

utf-8 - 在 VBScript 中解码 URL 编码的 UTF-8 字符串

php - codeigniter 查询上的内部服务器错误 500

mysql - Yii2:如何从一种形式将不同的数据保存在两个表中?

objective-c - "NSString stringWithUTF8String:"过于敏感

php - mysql 因某些字符而阻塞,而 mssql 不会