【问题标题】:textarea not submitting into databasetextarea 未提交到数据库
【发布时间】:2012-12-15 22:17:02
【问题描述】:

我有一个表单正在尝试提交到我的 MySQL 数据库中,它提交的所有记录都可以接受我博客表中的“内容”字段。

我不确定这是否重要,但我正在使用来自 nicedit.com 的 textarea 框架

更新:我已经删除了 NicEdit 框架,并且它作为普通文本区域提交时效果很好,我不确定现在该去哪里我会尝试将我的支持转移到 NicEdit 并在我找到时发布回复这个问题的解决方案。

但是我遇到了另一个问题并且不想打开关于它的新帖子,我的日期插入为 0000-00-00 而不是当前日期。有什么解决办法吗?

这里是 php post 方法

<?php
include_once"dbconnection.php";
session_start();
$connect = mysql_connect($host, $username, $password) or die (@mysql_error());
$selectdb = mysql_select_db($database, $connect) or die (@mysql_error());
if(isset($_POST['postblog'])){
    $blogtitle = $_POST['blogtitle'];
    $blogcontent = $_POST['blogcontent'];
    $author = 'Admin';
    $query = mysql_query("INSERT INTO blog SET date='date()', title='$blogtitle', author='$author', published='1', content='$blogcontent'");
    if ($query == false){
        echo "false";
    } else {
        echo "true";
    }
}
?>

和html表单

<form action='postblog.php' method='post'>
    <input class="inputMed" type="text" name="blogtitle"> <br />
    <textarea style="width: 600px;" name="blogcontent"></textarea> <br />
    <input type="submit" name="postblog" id="postblog" value="Post Blog" />
</form>

提前致谢

【问题讨论】:

  • 我闻到了 SQL 注入的味道。 Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial.
  • 只是猜测,您的“博客内容”中有 MySQL 不喜欢的未转义字符。尝试在插入前清理您的内容。
  • 如果您在编写代码时不担心 MySQL 安全性,那么您就是在为失败做好准备。我建议您阅读准备好的语句,它们是防止 SQL 注入的救星。
  • @AaronRussell 几乎肯定是在 NicEdit 框架中。请参阅我在下面提供的答案。
  • 对于日期问题,去掉date()周围的引号。他们正在阻止它运行该功能。

标签: php mysql forms textarea nicedit


【解决方案1】:

您的编辑器可能正在提交不同的变量(或根本不提交)。一种判断方法是让您的 PHP 执行以下操作:

print_r($_REQUEST);

暂时将其放入您的脚本中,以查看提交的内容和名称。如果你看到它,那就太好了。如果您没有看到您的内容,您将知道问题与 PHP 无关,您将不得不查看 nicedit 方面的内容。

【讨论】:

  • 哇,太棒了,这对我进行许多进一步的调试任务很有帮助。谢谢
  • 如果我错了请纠正我,但基本上我把它放在我要放的地方 } else { echo "true"; }
  • 我建议把它放在文件的开头,紧跟在“
【解决方案2】:

关于您的HOWEVER 日期问题。您的 'date()' 被解析为字符串而不是函数,并且您尚未定义参数。改为-

$query = mysql_query("INSERT INTO blog SET date='".date('Y-m-d')."', title='$blogtitle', author='$author', published='1', content='$blogcontent'");

或者使用 MySQL NOW()-

$query = mysql_query("INSERT INTO blog SET date=NOW(), title='$blogtitle', author='$author', published='1', content='$blogcontent'");

或者使用 MySQL CURDATE()-

$query = mysql_query("INSERT INTO blog SET date=CURDATE(), title='$blogtitle', author='$author', published='1', content='$blogcontent'");

另请参阅 - PHP function inside single quotes display is as text

【讨论】:

    【解决方案3】:

    根据这篇文章:NicEdit data not in POST,你需要这样的东西:

    <INPUT type=submit name="submit" value="Send" onclick="nicEditors.findEditor('blogContent').saveContent();">
    

    可能是这样的:

    var editor = new nicEditors.findEditor('blogContent');
    var content = editor.getContent();
    

    这应该为您指明正确的方向。如果您想要一个具体的答案,您需要将您的问题专门针对知道和/或使用 NicEdit 的人,因为您的问题与它 (NicEdit) 如何以非标准方式包装您的 DOM 密切相关。

    【讨论】:

    • 他的帖子在哪里说他正在使用 NiceEdit?
    • 在他说“我不确定这是否重要,但我正在使用来自 nicedit.com 的 textarea 框架”的部分。在您阅读您所评论的内容之前,请不要对答案投反对票。
    • 如果文档here 说他们似乎在说什么,将上面的onClick 事件方法添加到您的提交按钮应该可以工作,就像将该方法添加到表单onSubmit 事件一样
    • 嘿@ChrisOstmo 感谢您的回答!在更新我的帖子之前我没有回答你。但你的权利,我会尝试一下,看看它会把我带到哪里。谢谢!
    【解决方案4】:

    对于日期问题,你可以使用mysql currdate 之类的函数

       mysql_query("INSERT INTO blog SET date='DATE: Auto CURDATE()', title='$blogtitle', author='$author', published='1', content='$blogcontent'");
    

    Nicedit 试试这个

       try this code
       <html>
       <head>
        <script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
        <script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>
       </head>
    
       <body>
        <form action='postblog.php' method='post'>
         <input class="inputMed" type="text" name="blogtitle"> <br />
         <textarea style="width: 600px;" name="blogcontent"></textarea> <br />
         <input type="submit" name="postblog" id="postblog" value="Post Blog" />
       </form>
      </body>
      </html> 
    

    【讨论】:

    • 对于 NicEdit,您是否在 html 中使用多个文本区域?请发布整个 html 代码,因为我多次使用 nicEdit,它工作正常。
    • 当我切换到 MySQLi 并使用准备好的语句时,我的编码发生了很大变化,我宁愿打开另一个主题。我找到了有关此问题的更多有用信息,我附加了所有显示不同形式的 div。现在已经尝试了许多不同的 textarea 插件。而且没有一个在工作,所以我确定这是我附加了我的 div 并且它只是无法识别的事实,因为我注意到的一件事是,在我使用过的所有插件中,它们都没有让我真正点击任何按钮.将尽快发布链接
    猜你喜欢
    • 2019-06-18
    • 2013-05-26
    • 1970-01-01
    • 2011-03-21
    • 2013-06-03
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多