将文本区域保存到 mySQL 数据库字段 PHP

发布于 2024-11-29 15:44:18 字数 836 浏览 0 评论 0原文

您好,我正在使用 openWYSIWYG 作为文本区域的文本编辑器。然后,我尝试将文本区域的内容发布到数据库中的字段。

这是我到目前为止的代码 -

<?php
$text = $_GET['Comments']; 



mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
mysql_select_db("databasename") or die ('Data error:' . mysql_error());

$query="INSERT INTO KeepData (player_data)VALUES ('$text')";

mysql_query($query) or die ('Error updating database' . mysql_error());




?> 

我可以连接到数据库,当我单击“提交”时,它会在该字段中添加一个空白条目?我该如何获取它以保留所有格式化数据?

非常感谢

更新

 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <textarea id="Comments" name="Comments">
 example text
 </textarea>
<input type="submit" name="mysubmit" value="Save Post" />
 </form>

DIM3NSION

Hi i am using openWYSIWYG as a text editor for a text area. I then am trying to post the contents of the text area to a field in my database.

This is the code i have so far -

<?php
$text = $_GET['Comments']; 



mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
mysql_select_db("databasename") or die ('Data error:' . mysql_error());

$query="INSERT INTO KeepData (player_data)VALUES ('$text')";

mysql_query($query) or die ('Error updating database' . mysql_error());




?> 

I can connect to the database, and when i click submit it adds a blank entry into the field? how would i get it so it keeps all the formatted data?

Many thanks

update

 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <textarea id="Comments" name="Comments">
 example text
 </textarea>
<input type="submit" name="mysubmit" value="Save Post" />
 </form>

DIM3NSION

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

八巷 2024-12-06 15:44:18

尝试如下操作:

<?php
    if ($_POST['submit']) {
        mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
        mysql_select_db("databasename") or die ('Data error:' . mysql_error());
        $text = mysql_real_escape_string($_POST['comments']); 
        $query="INSERT INTO KeepData (player_data) VALUES ('$text')";
        mysql_query($query) or die ('Error updating database' . mysql_error());
    }
?>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <textarea name="comments">Example Comment</textarea>
    <input name="submit" type="submit" value="submit" />
</form>

Try something like the following:

<?php
    if ($_POST['submit']) {
        mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
        mysql_select_db("databasename") or die ('Data error:' . mysql_error());
        $text = mysql_real_escape_string($_POST['comments']); 
        $query="INSERT INTO KeepData (player_data) VALUES ('$text')";
        mysql_query($query) or die ('Error updating database' . mysql_error());
    }
?>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <textarea name="comments">Example Comment</textarea>
    <input name="submit" type="submit" value="submit" />
</form>
零度° 2024-12-06 15:44:18

您必须将格式编码版本保存在隐藏文本区域的其他位置(与 StackOverflow 上的此处非常相似,如果您输入 **text** ,它将显示为 text,在数据库,他们可能将其保存为 **text** 并使用 PHP 呈现它。

保存格式化版本后,当您从数据库获取数据时使用 PHP 呈现它。

You must save an format coded version elsewhere on a hidden textarea (much like here on StackOverflow, if you type **text** it will come out as text, in the database, they probably save it as **text** and render it with PHP.

Once the formatted version is saved, render it with PHP when you get the data from the database.

放手` 2024-12-06 15:44:18

您的表单是 POSTing 还是 GETing(您在帖子中提到了 POSTing)?您有 $_GET['Comments'],但如果您的表单操作是 POST,则需要使用 $_POST['Comments'];

如果在分配 $text 之后添加 echo $text;exit; ,您会看到什么吗?

Is your form POSTing or GETing (you said POSTing in your post)? You have $_GET['Comments'], but if your form's action is POST, you need to use $_POST['Comments'];

if you add echo $text;exit; after you assign $text, do you see anything?

错々过的事 2024-12-06 15:44:18

您应该使用 mysql_escape_string 函数,因为 mysql 注入和 if文本包含 ' 你会得到错误。

You should use mysql_escape_string function because of mysql injection and if text contains ' you'll get error.

黯然 2024-12-06 15:44:18
  1. 检查您是否有
    。如果默认只是使用 get
  2. 检查您的 wisywig 是否具有 name='Comments' 属性。
  3. 使用mysql_real_escape_string转义$text。它可以包含 SQL 非法符号,例如 '。此函数使用 \ 转义它们
  4. (建议)不要使用 mysql_*,它已在 PHP 5.3 中弃用并将被删除。
  5. (建议)将用户输入附加到 sql 查询始终存在 SQL 注入的风险。使用准备好的语句
  1. Check if your have <form action='get'>. If it is just <form> get used by default
  2. Check that your wisywig have name='Comments' attribute.
  3. Escape the $text with mysql_real_escape_string. It can contain SQL-illegal symbols, like '. This function escapes them with \
  4. (recommendation) do not use mysql_*, it is deprecated of PHP 5.3 and will be removed.
  5. (recommendation) appending user input to sql query is always a risk of SQL-injection. Use prepared statements
江湖正好 2024-12-06 15:44:18

只需添加 onclick 事件,例如

<button onclick=" $('#txtEditor').val($('.Editor-editor').html());" type="Publish" id="Publish" name="Publish" class="btn btn-primary">Publish</button>

记住 #txtEditor 必须与表单 id 匹配,这效果很好,并注意 .html 会将其保存到带有颜色的数据库中,如果您添加了任何内容(即所见即所得功能),

那么对于发送到数据库的 php 代码,请执行类似

$anything = ($_POST['txtEditor']);

$anything 的操作,将其用作变量,不要忘记txtEidtor 是表单 ID。这样你的所见即所得就可以启动并工作了。

just add the onclick event something like

<button onclick=" $('#txtEditor').val($('.Editor-editor').html());" type="Publish" id="Publish" name="Publish" class="btn btn-primary">Publish</button>

remember the #txtEditor has to match with the form id, this works well, and note the .html will save it to database with the color,Bold and many more effect if you added any (that is the wysiwyg fuction)

then for your php code that send to database, do something like this

$anything = ($_POST['txtEditor']);

$anything you which to use as variable,dont forget the txtEidtor is the form id. with this your wysiwyg is up and working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文