如何将更新的CKEditor内容保存到数据库?
我在谷歌和这个网站上到处搜索这个问题的答案,但没有运气。
我正在为想要自己维护的未来客户开发定制 CMS。下面是将当前页面内容从数据库加载到文本区域的代码:
if(isset($_GET['id'])) {
$id = $_GET['id'];
$content = mysql_query("SELECT title, content FROM pages WHERE id=".$id);
$search = mysql_num_rows($content);
if($search > 0){
while($page = mysql_fetch_array($content)) { ?>
<h1>Editing <?php echo $page['title']; ?></h1>
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<textarea id="editor" name="content"><?php echo $page['content']; ?></textarea>
</form>
<?php }
}
} ?>
这是 save.php
中的代码:
<?php
if(isset($_POST['content'])){
$content = $_POST['content'];
$id = $_GET['id'];
echo $content;
mysql_query("UPDATE pages SET content=".$content." WHERE id=".$id);
}
?>
问题是 POST['content'] 不断获取原始内容,而不是原始内容编辑了用户刚刚提交的一份。
我该如何解决这个问题?
I've searched high and low both on Google and this site for an answer to this question with no luck.
I'm working on a custom CMS for future customers who want to do their own maintenance. Here's the code that loads the current page content into the textarea from the database:
if(isset($_GET['id'])) {
$id = $_GET['id'];
$content = mysql_query("SELECT title, content FROM pages WHERE id=".$id);
$search = mysql_num_rows($content);
if($search > 0){
while($page = mysql_fetch_array($content)) { ?>
<h1>Editing <?php echo $page['title']; ?></h1>
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<textarea id="editor" name="content"><?php echo $page['content']; ?></textarea>
</form>
<?php }
}
} ?>
Here's the code in save.php
:
<?php
if(isset($_POST['content'])){
$content = $_POST['content'];
$id = $_GET['id'];
echo $content;
mysql_query("UPDATE pages SET content=".$content." WHERE id=".$id);
}
?>
The problem is that the POST['content'] keeps getting the original content, not the edited one that the user just submitted.
How do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发布表单时,数据应自动传递。以下是开发人员指南中的“集成”页面,对此进行了解释:
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Integration
您确定查询正在成功运行并且内容字段正在更新吗?我问是因为您发布的代码说在 save.php 中您正在使用:
这似乎会导致问题,因为您在表单中使用的是 method="post",而不是 method="get"。
您还可以发布您用来用 CKEditor 替换 textarea 的代码吗?您使用的是 PHP 方法:
还是 JavaScript 方法:
好吧,
Joe
跟进评论中的问题
嗨 Purmou,
我没有注意到您在表单操作中包含了 id,对此感到抱歉。如果您确实想将其用作 $_POST 变量,则可以将其作为隐藏字段包含在内,如下所示:
在 CKEditor 安装的 _samples 文件夹中,有一些关于通过 PHP 加载编辑器的好文档:
http://YourSite.com/ckeditor/_samples/php/
http://YourSite.com/ckeditor/_samples/php/replace.php,有基本的设置:
与 JavaScript 方法类似,您可以在替换文本区域之前添加配置选项。 “advanced.php”文件中的示例:
要将 PHP 方法与您的特定代码一起使用,请执行以下操作:
我将 textarea id 从“editor”更改为“content”。我建议不要使用“editor”作为 id 或名称,因为它在 CKEditor 代码中用于引用 CKEditor 实例。
您可以在加载编辑器的页面、config.js 文件或您自己的自定义配置文件中进行配置设置。
我花了一些时间尝试在提交表单后捕获表单内容字段的值,但只能在 CKEditor 更新内容之前才能看到它。
祝你好运,
乔
The data should be passed automatically when the form is posted. Here is the "Integration" page page from the Developers Guide that explains this:
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Integration
Are you sure that the query is being run successfully and that the content field is being updated? I ask because the code you posted says that in save.php, you are using:
This would seem to cause a problem, because you are using method="post" in your form, not method="get".
Could you also post the code you are using to replace the textarea with CKEditor. Are you using the PHP method:
Or the JavaScript method:
Be Well,
Joe
Follow up to question in comments
Hi Purmou,
I didn't notice that you were including the id in the form action, sorry about that. If you did want to use it as a $_POST variable instead, you could include it as a hidden field like this:
There is some good documentaion about loading the editor via PHP in the _samples folder of the CKEditor install:
http://YourSite.com/ckeditor/_samples/php/
http://YourSite.com/ckeditor/_samples/php/replace.php, has the basic settings:
Similar to the JavaScript method, you can add config options before you replace the textarea. An example from the "advanced.php" file:
To use the PHP method with your specific code, do this:
I changed the textarea id from "editor" to "content". I would recommend against using "editor" for the id or name, because it's used in the CKEditor code to refer to the CKEditor instance.
You can do config settings in the page where you load the editor or in the config.js file or in your own custom config file.
I spent some time trying to catch the value of the form content field after the form is submitted, but was only able to see it before CKEditor had updated the contents.
Be Well,
Joe